您的位置:首页 > 其它

POJ 3635 Full Tank?

2016-03-30 00:07 253 查看
题目链接: http://poj.org/problem?id=3635

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

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

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

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

#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int N = 11e4, E = 22e5, mod = 101, inf = 1e9 + 10;
struct node
{
int num, d;
node(){}
node(int x, int y)
{
num = x;
d = y;
}
};
int oil[1010], dist
, used
;
int firste
, nexte[E], v[E], w[E];
int n, m, e = 1, t;
bool operator < (const node &aa, const node &bb)
{
return aa.d > bb.d;
}
priority_queue<node>q;
void build(int x, int y, int z)
{
nexte[++e] = firste[x];
firste[x] = e;
v[e] = y;
w[e] = z;
}
void dijkstra(int c, int s, int f)
{
for(int i = 0; i < n; ++i)
for(int j = 0; j <= c; ++j)
dist[i * mod + j] = inf;
dist[s * mod] = 0;
q.push(node(s * mod, 0));
int u, di;
while(!q.empty())
{
u = q.top().num;
di = q.top().d;
q.pop();
if(used[u] == t || u % mod > c)
continue;
if(u == f * mod)
break;
used[u] = t;
for(int p = firste[u]; p; p = nexte[p])
if(dist[v[p]] > di + w[p])
{
dist[v[p]] = di + w[p];
q.push(node(v[p], dist[v[p]]));
}
}
while(!q.empty())
q.pop();
if(dist[f * mod] < inf)
printf("%d\n", dist[f * mod]);
else
puts("impossible");
}
int main()
{
scanf("%d%d", &n, &m);
for(int i = 0; i < n; ++i)
{
scanf("%d", &oil[i]);
for(int j = 0; j < 100; ++j)
build(i * mod + j, i * mod + j + 1, oil[i]);
}
int x, y, z;
while(m--)
{
scanf("%d%d%d", &x, &y, &z);
for(int i = z; i <= 100; ++i)
{
build(x * mod + i, y * mod + i - z, 0);
build(y * mod + i, x * mod + i - z, 0);
}
}
memset(used, -1, sizeof used);
scanf("%d", &t);
int c, s, f;
while(t--)
{
scanf("%d%d%d", &c, &s, &f);
dijkstra(c, s, f);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: