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
| #include <cstdio> #include <algorithm> #include <queue> #include <cstring> #define MAXN 1005 #define MAXE 10005 using namespace std;
int n, m, u, v, dis, e, s, c, Q, cnt, head[MAXN], val[MAXN], d[MAXN][110];
struct Node { int city, fuel, cost; Node(int x, int y, int z) { city=x; fuel=y; cost=z; } friend bool operator < (Node x, Node y) { return x.cost>y.cost; } }; struct Edge {int next, to, dis;} edge[MAXE*2];
void addedge(int from, int to, int dis) { edge[++cnt].next=head[from]; edge[cnt].to=to; edge[cnt].dis=dis; head[from]=cnt; }
void bfs(int s, int e, int v) { priority_queue<Node> q; memset(d, 0x3f, sizeof(d)); d[s][0]=0; q.push(Node(s, 0, 0)); while(!q.empty()) { int x=q.top().city, f=q.top().fuel, c=q.top().cost; q.pop(); if(x==e) {printf("%d\n", c); return;} if(f<v && d[x][f]+val[x]<d[x][f+1]) { d[x][f+1]=d[x][f]+val[x]; q.push(Node(x, f+1, d[x][f+1])); } for(int i=head[x]; i; i=edge[i].next) { int to=edge[i].to, dis=edge[i].dis; if(f>=dis && c<d[to][f-dis]) { d[to][f-dis]=c; q.push(Node(to, f-dis, d[to][f-dis])); } } } printf("impossible\n"); }
int main() { scanf("%d%d", &n, &m); for(int i=1; i<=n; i++) scanf("%d", &val[i]); for(int i=1; i<=m; i++) { scanf("%d%d%d", &u, &v, &dis); addedge(u+1, v+1, dis); addedge(v+1, u+1, dis); } scanf("%d", &Q); for(int i=1; i<=Q; i++) { scanf("%d%d%d", &c, &s, &e); bfs(s+1, e+1, c); } }
|