-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathuva-11367.cpp
86 lines (70 loc) · 2 KB
/
uva-11367.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
//uva 11367
//Full Tank?
#include <iostream>
#include <climits>
#include <vector>
#include <queue>
#include <map>
using namespace std;
int Dijkstra(vector <vector <pair <int, int> > > & Graph, vector <int> & price, int source, int destin, int capacity);
typedef struct state{
int node;
int extra;
int cost;
state(int n, int e, int c)
:node(n), extra(e), cost(c)
{}
bool operator<(const state & a)const{
return cost > a.cost;
}
}state;
int main(void)
{
int n, m;
cin >> n >> m;
vector <int> price(n);
for(int i = 0; i < n; ++i)
cin >> price[i];
vector <vector <pair <int, int> > > Graph(n);
for(int i = 0; i < m; ++i){
int u, v, d;
cin >> u >> v >> d;
Graph[u].push_back(make_pair(v, d));
Graph[v].push_back(make_pair(u, d));
}
int q;
cin >> q;
while(q--){
int capacity, source, destination;
cin >> capacity >> source >> destination;
int result = Dijkstra(Graph, price, source, destination, capacity);
if(result == INT_MAX)
cout << "impossible" << endl;
else
cout << result << endl;
}
return 0;
}
int Dijkstra(vector <vector <pair <int, int> > > & Graph, vector <int> & price, int source, int destin, int capacity)
{
vector < vector <int> > min_cost(Graph.size(), vector <int> (capacity + 1, INT_MAX));
priority_queue <state> Heap;
Heap.push(state(source, 0, 0));
min_cost[source][0] = 0;
while(!Heap.empty()){
state front = Heap.top();
Heap.pop();
if(front.node == destin)
return front.cost;
if(front.extra < capacity && front.cost + price[front.node] < min_cost[front.node][front.extra + 1]){
min_cost[front.node][front.extra + 1] = front.cost + price[front.node];
Heap.push(state(front.node, front.extra + 1, min_cost[front.node][front.extra + 1]));
}
for(auto d : Graph[front.node])
if(front.extra >= d.second && front.cost < min_cost[d.first][front.extra - d.second]){
min_cost[d.first][front.extra - d.second] = front.cost;
Heap.push(state(d.first, front.extra - d.second, min_cost[d.first][front.extra - d.second]));
}
}
return INT_MAX;
}