POJ 3635 Full Tank?

题目链接: http://poj.org/problem?id=3635

----------------------------------------------------------------------------------------------------------------------

把操作的费用作为边的长度 把实际的边的长度转化为点之间的关系 是一道很有意思的题目

已经得到终点最小距离后立即退出会快点

另外注意内存计算要细心(或者直接粗略计算后多开10%)

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <queue>
 6 using namespace std;
 7 const int N = 11e4, E = 22e5, mod = 101, inf = 1e9 + 10;
 8 struct node
 9 {
10     int num, d;
11     node(){}
12     node(int x, int y)
13     {
14         num = x;
15         d = y;
16     }
17 };
18 int oil[1010], dist[N], used[N];
19 int firste[N], nexte[E], v[E], w[E];
20 int n, m, e = 1, t;
21 bool operator < (const node &aa, const node &bb)
22 {
23     return aa.d > bb.d;
24 }
25 priority_queue<node>q;
26 void build(int x, int y, int z)
27 {
28     nexte[++e] = firste[x];
29     firste[x] = e;
30     v[e] = y;
31     w[e] = z;
32 }
33 void dijkstra(int c, int s, int f)
34 {
35     for(int i = 0; i < n; ++i)
36         for(int j = 0; j <= c; ++j)
37             dist[i * mod + j] = inf;
38     dist[s * mod] = 0;
39     q.push(node(s * mod, 0));
40     int u, di;
41     while(!q.empty())
42     {
43         u = q.top().num;
44         di = q.top().d;
45         q.pop();
46         if(used[u] == t || u % mod > c)
47             continue;
48         if(u == f * mod)
49             break;
50         used[u] = t;
51         for(int p = firste[u]; p; p = nexte[p])
52             if(dist[v[p]] > di + w[p])
53             {
54                 dist[v[p]] = di + w[p];
55                 q.push(node(v[p], dist[v[p]]));
56             }
57     }
58     while(!q.empty())
59         q.pop();
60     if(dist[f * mod] < inf)
61         printf("%d\n", dist[f * mod]);
62     else
63         puts("impossible");
64 }
65 int main()
66 {
67     scanf("%d%d", &n, &m);
68     for(int i = 0; i < n; ++i)
69     {
70         scanf("%d", &oil[i]);
71         for(int j = 0; j < 100; ++j)
72             build(i * mod + j, i * mod + j + 1, oil[i]);
73     }
74     int x, y, z;
75     while(m--)
76     {
77         scanf("%d%d%d", &x, &y, &z);
78         for(int i = z; i <= 100; ++i)
79         {
80             build(x * mod + i, y * mod + i - z, 0);
81             build(y * mod + i, x * mod + i - z, 0);
82         }
83     }
84     memset(used, -1, sizeof used);
85     scanf("%d", &t);
86     int c, s, f;
87     while(t--)
88     {
89         scanf("%d%d%d", &c, &s, &f);
90         dijkstra(c, s, f);
91     }
92     return 0;
93 }

 

posted @ 2016-03-30 00:07 sagitta 阅读(...) 评论(...) 编辑 收藏