POJ 3635 Full Tank? 拆点建图+dijkstra
2016-08-21 18:49
357 查看
#include <cstdio> #include <cstring> #include <queue> using namespace std; #define fst first #define snd second typedef pair<int, int> pii; const int maxn=1005; const int maxv=133333; const int maxm=2333333; const int inf=0x3f3f3f3f; int tot; struct Edge { int to, cost, next; } edge[maxm]; int head[maxm]; void addedge(int u, int v, int w) { edge[tot].to=v; edge[tot].cost=w; edge[tot].next=head[u]; head[u]=tot++; } int V, n, m, K; int tank, s, t; int d[maxv], pri[maxn], u[maxm], v[maxm], w[maxm]; void dijkstra(int s) { priority_queue<pii, vector<pii>, greater<pii> > que; memset(d, 0x3f, sizeof d); d[s]=0; que.push(pii(0, s)); while (!que.empty()) { pii p=que.top(); que.pop(); int v=p.snd; if (d[v]<p.fst) continue; // 不加会T if (v==t*101) break; for (int i=head[v]; i!=-1; i=edge[i].next) { int to=edge[i].to, cost=edge[i].cost; if (v%101>tank) continue; if (d[to]>d[v]+cost) { d[to]=d[v]+cost; que.push(pii(d[to], to)); } } } } int main() { //freopen("in.txt", "r", stdin); while (scanf("%d%d", &n, &m)==2) { for (int i=0; i<n; i++) scanf("%d", &pri[i]); for (int i=0; i<m; i++) scanf("%d%d%d", &u[i], &v[i], &w[i]); tot=0; memset(head, -1, sizeof head); for (int i=0; i<n; i++) for (int j=0; j<100; j++) addedge(101*i+j, 101*i+j+1, pri[i]); for (int i=0; i<m; i++) for (int j=w[i]; j<=100; j++) { addedge(101*u[i]+j, 101*v[i]+j-w[i], 0); addedge(101*v[i]+j, 101*u[i]+j-w[i], 0); } scanf("%d", &K); for (int i=0; i<K; i++) { scanf("%d%d%d", &tank, &s, &t); dijkstra(s*101); if (d[t*101]==inf) puts("impossible"); else printf("%d\n", d[t*101]); } } return 0; }
相关文章推荐
- POJ 3635 Full Tank?
- POJ 3635 Full Tank?
- POJ-3635-Full Tank?
- poj 3635 Full Tank 最短路
- poj 3635(full tank?)
- POJ 3635 Full Tank?
- POJ 3635 Full Tank? (dp)
- [POJ 3635] Full Tank?
- POJ-3635 Full Tank? 变形最短路
- POJ 3635:Full Tank?
- poj 3635/hdu 1676 Full Tank? 车辆加油+最短路
- poj 3635 Full Tank? ( bfs+dp思想 )
- POJ 3635 Full Tank(最短路径变形 + 优先队列)
- POJ-3635-Full Tank?-spfa+ 优先队列
- poj 3635 Full Tank? ( 图上dp )
- poj 3635 Full Tank?
- POJ 3635 Full Tank?
- POJ 3635 Full Tank? (最短路变形,BFS+优先队列)
- poj 3635/hdu 1676 Full Tank? 车辆加油+最短路
- poj 3635 Full Tank? ( 图上dp )