油箱POJ3635(tle)
思路
题干在这:POJ3635
优先队列BFS
但不知道为什么就超时了,可能是有细节没处理好,改天再回来改
tle代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#define MAX 1010
using namespace std;
int n, m;
int price[MAX];
int head[MAX];
int nexte[MAX];
int endp[20010];
int len[20010];
int ptot, etot;
void createdge(int u,int v,int d){
endp[++etot] = v;
nexte[etot] = head[u];
head[u] = etot;
len[etot] = d;
endp[++etot] = u;
nexte[etot] = head[v];
head[v] = etot;
len[etot] = d;
}
int c, s, e;
int cost[MAX][110];
int vis[MAX][110];
struct cas {
int pos;
int cos;
int fuel;
friend bool operator <(const cas& a,const cas& b) {
return a.cos > b.cos;
}
};
priority_queue<cas> q;
void bfs() {
while (q.size())
q.pop();
memset(cost, 0, sizeof(cost));
memset(vis, 0, sizeof(vis));
vis[s][0] = 1;
cost[s][0] = 0;
cas x = { s,0,0 };
q.push(x);
while (q.size()) {
cas work = q.top();
q.pop();
if (work.pos == e) {
printf("%d\n", work.cos);
return;
}
if (work.fuel < c&&!vis[work.pos][work.fuel+1]) {
cas n;
n.pos = work.pos;
n.cos = work.cos + price[work.pos];
n.fuel = work.fuel + 1;
vis[n.pos][n.fuel] = 1;
q.push(n);
}
for (int i = head[work.pos]; i != -1; i = nexte[i]) {
if (work.fuel >= len[i] && !vis[endp[i]][work.fuel - len[i]]) {
cas n;
n.pos = endp[i];
n.cos = work.cos;
n.fuel = work.fuel - len[i];
vis[n.pos][n.fuel] = 1;
q.push(n);
}
}
}
printf("impossible\n");
return;
}
int main() {
cin >> n >> m;
for (int i = 0; i < n; i++)
scanf_s("%d", &price[i]);
memset(head, -1, sizeof(head));
for (int i = 1; i <= m; i++) {
int u, v, d;
scanf_s("%d%d%d", &u, &v, &d);
createdge(u, v, d);
}
int q;
cin >> q;
while (q--) {
scanf_s("%d%d%d", &c, &s, &e);
bfs();
}
}