题目:http://poj.org/problem?id=3635
用优先级队列+dijkstra
//#include<bits/stdc++.h>
//#define DEBUG
#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<queue>
#include<map>
#include<vector>
#include<set>
#include<cmath>
#include<cstdlib>
#include<algorithm>
#include<stack>
using namespace std;
typedef long long ll;
typedef unsigned int ui;
const int maxn = 1005;
const ll mod = 1e9 + 7;
const double pi = acos(-1);
int n, m, cap, s, t, ans;
int a[maxn];
struct P
{
int to, next, c;
}edge[maxn * 20];
int head[maxn], cnt;
inline void add(int u, int v, int c)
{
edge[cnt].to = v;
edge[cnt].c = c;
edge[cnt].next = head[u];
head[u] = cnt++;
}
struct node
{
int v, oi, cost;
bool operator>(node A) const
{
return cost > A.cost;
}
};
bool vis[maxn][maxn];
int dis[maxn][maxn];
void BFS()
{
memset(vis, 0, sizeof(vis));
memset(dis, 0x3f, sizeof(dis));
dis[s][0] = 0;
priority_queue<node, vector<node>, greater<node> > PQ;
PQ.push({s, 0, 0});
while(!PQ.empty())
{
node cur = PQ.top();
PQ.pop();
if(vis[cur.v][cur.oi]) continue;
vis[cur.v][cur.oi] = 1;
if(cur.v == t)
{
cout << cur.cost << endl;
return;
}
int u = cur.v;
if(cur.oi + 1 <= cap && !vis[u][cur.oi + 1] && dis[u][cur.oi + 1] > dis[u][cur.oi] + a[u])
{
dis[u][cur.oi + 1] = dis[u][cur.oi] + a[u];
PQ.push({u, cur.oi + 1, cur.cost + a[u]});
}
for(int i = head[u]; ~i; i = edge[i].next)
{
int v = edge[i].to;
int d = edge[i].c;
if(cur.oi >= d && !vis[v][cur.oi - d] && dis[v][cur.oi - d] > cur.cost)
{
dis[v][cur.oi - d] = cur.cost;
PQ.push({v, cur.oi - d, cur.cost});
}
}
}
cout << "impossible" << endl;
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
while(cin >> n >> m)
{
memset(head, -1, sizeof(head));
cnt = 0;
int u, v, c;
for(int i = 0; i < n; i++)
cin >> a[i];
while(m--)
{
cin >> u >> v >> c;
add(u, v, c);
add(v, u, c);
}
int q;
cin >> q;
while(q--)
{
cin >> cap >> s >> t;
BFS();
}
}
}