bfs+优先队列+邻接链表
细节说明见http://www.xuebuyuan.com/321339.html
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;
#define MAXN 1005
int N, M, cnt;
int oil_price[MAXN];
int dist[MAXN][MAXN];
struct Node{
int v, w;
int next;
};
Node node[MAXN * 20];
int head[MAXN];
struct Point{
int u;
int rest_oil;
int cost;
};
int vis[MAXN][105];
bool operator < (const Point& a, const Point& b) {
return a.cost > b.cost;
}
void add_edge(int u, int v, int c) {
node[cnt].v = v;
node[cnt].next = head[u];
node[cnt].w = c;
head[u] = cnt ++;
node[cnt].v = u;
node[cnt].next = head[v];
node[cnt].w = c;
head[v] = cnt ++;
}
void BFS(int capacity, int s, int e) {
memset(vis, 0, sizeof(vis));
priority_queue<Point>Q;
Point start;
start.u = s;
start.rest_oil = 0;
start.cost = 0;
Q.push(start);
while(!Q.empty()) {
Point hd = Q.top();
Q.pop();
int u = hd.u;
vis[u][hd.rest_oil] = 1;
if(u == e) {
printf("%d\n", hd.cost);
return;
}
if(hd.rest_oil < capacity && !vis[u][hd.rest_oil + 1]) {
Point t;
t.u = u;
t.rest_oil = hd.rest_oil + 1;
t.cost = hd.cost + oil_price[u];
Q.push(t);
}
for(int i = head[u]; i != -1; i = node[i].next) {
if(hd.rest_oil >= node[i].w && !vis[node[i].v][hd.rest_oil - node[i].w]) {
Point t;
t.u = node[i].v;
t.rest_oil = hd.rest_oil - node[i].w;
t.cost = hd.cost;
Q.push(t);
}
}
}
printf("impossible\n");
}
int main() {
freopen("poj3635.txt", "r", stdin);
int u, v, c;
while(scanf("%d%d", &N, &M) != EOF) {
cnt = 0;
memset(head, -1, sizeof(head));
for(int i = 0; i < N; ++ i) {
scanf("%d", &oil_price[i]);
}
while(M --) {
scanf("%d%d%d", &u, &v, &c);
add_edge(u, v, c);
}
int q, capacity, s, e;
scanf("%d", &q);
while(q --) {
scanf("%d%d%d", &capacity, &s, &e);
BFS(capacity, s, e);
}
}
return 0;
}