LightOJ - 1074 Extended Traffic——spfa判负环

网友投稿 498 2022-11-28

LightOJ - 1074 Extended Traffic——spfa判负环

LightOJ - 1074 Extended Traffic——spfa判负环

这题判负环后需要用dfs把整个负环标记一下,WA到爆炸的同学尝试把I64改成ll或者直接用int

#include #include #include #include #include using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 250;bool vis[maxn], judge[maxn];int T, N, M, Q, busyness[maxn], tot, head[maxn], num[maxn], dis[maxn];struct Edge { int to, val, next;}edge[maxn * maxn];int cal(int x, int y) { return (x - y) * (x - y) * (x - y); }void init() { tot = 0; for (int i = 1; i <= N; i++) head[i] = -1, judge[i] = false;}void addedge(int u, int v, int val) { tot++; edge[tot].to = v; edge[tot].val = val; edge[tot].next = head[u]; head[u] = tot;}void dfs(int u) { if (judge[u]) return; judge[u] = true; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to; dfs(v); }}void spfa(int s) { for (int i = 1; i <= N; i++) dis[i] = INF, vis[i] = false, num[i] = 0; dis[s] = 0, vis[s] = true, num[s] = 1; queue q; q.push(s); while (!q.empty()) { int u = q.front(); q.pop(); vis[u] = false; for (int i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].to, val = edge[i].val; if (dis[v] > dis[u] + val) { dis[v] = dis[u] + val; if(!vis[v]) { vis[v] = true; num[v]++; q.push(v); if (num[v] >= N) { dfs(v); return; } } } } }}int main() { scanf("%d", &T); for (int kase = 1; kase <= T; kase++) { scanf("%d", &N); init(); for (int i = 1; i <= N; i++) scanf("%d", &busyness[i]); scanf("%d", &M); for (int i = 1; i <= M; i++) { int u, v; scanf("%d %d", &u, &v); int val =cal(busyness[v], busyness[u]); addedge(u, v, val); } spfa(1); printf("Case %d:\n", kase); scanf("%d", &Q); for (int i = 1; i <= Q; i++) { int temp; scanf("%d", &temp); if (judge[temp] || dis[temp] == INF || dis[temp] < 3) { printf("?\n"); } else { printf("%d\n", dis[temp]); } } } return 0;}

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:POJ 3159 Candies——差分约束
下一篇:POJ 1611 The Suspects——并查集
相关文章

 发表评论

暂时没有评论,来抢沙发吧~