POJ 3259 Wormholes SPFA算法题解
来源:互联网 发布:阿里云创建自定义镜像 编辑:程序博客网 时间:2024/06/15 15:21
本题其实也可以使用SPFA算法来求解的,不过就一个关键点,就是当某个顶点入列的次数超过所有顶点的总数的时候,就可以判断是有负环出现了。
SPFA原来也是可以处理负环的。
不过SPFA这种处理负环的方法自然比一般的Bellman Ford算法要慢点了。
#include <stdio.h>#include <string.h>#include <limits.h>const int MAX_N = 501;const int MAX_M = 2501;const int MAX_W = 201;struct Edge{int src, des, wei, next;//Edge(int s, int d, int w) : src(s), des(d), wei(w) {}};Edge edge[(MAX_M<<1)+MAX_W];int dist[MAX_N];int head[MAX_N];bool vis[MAX_N];int qu[MAX_N];int cnt[MAX_N];int totalEdges;inline void insertEdge(int src, int des, int wei){edge[totalEdges].src = src, edge[totalEdges].des = des;edge[totalEdges].wei = wei; edge[totalEdges].next = head[src];head[src] = totalEdges++;}int N, M, W, F;bool cycleSPFA(){for (int i = 1; i <= N; i++) dist[i] = INT_MAX;dist[1] = 0;memset(vis, 0, sizeof(bool)*(N+1));memset(cnt, 0, sizeof(int)*(N+1));int top = 0, tail = 1;//循环队列的头和尾下标qu[top] = 1;vis[1] = true;cnt[1] = 1;while (top < tail){int u = qu[top%MAX_N];//自定义循环队列top++;vis[u] = false;for (int e = head[u]; e ; e = edge[e].next){int v = edge[e].des;if (dist[u]+edge[e].wei < dist[v]){dist[v] = dist[u]+edge[e].wei;if (!vis[v]){vis[v] = true;qu[tail%MAX_N] = v;tail++;cnt[v]++;//记录入列次数if (cnt[v] >= N) return true;}}}}return false;}int main(){int src, des, wei;scanf("%d", &F);while (F--){scanf("%d %d %d", &N, &M, &W);memset(head, 0, sizeof(int) * (N+1));totalEdges = 1;for (int i = 0; i < M; i++){scanf("%d %d %d", &src, &des, &wei);insertEdge(src, des, wei);insertEdge(des, src, wei);}for (int i = 0; i < W; i++){scanf("%d %d %d", &src, &des, &wei);insertEdge(src, des, -wei);}if (cycleSPFA()) puts("YES");else puts("NO");}return 0;}
1 0
- POJ 3259 Wormholes SPFA算法题解
- poj 3259 Wormholes spfa算法
- poj 3259 Wormholes //SPFA
- poj 3259 Wormholes SPFA
- POJ 3259 Wormholes(SPFA)
- Wormholes - POJ 3259 spfa
- poj 3259 Wormholes(spfa)
- POJ 3259:Wormholes 【SPFA】
- Wormholes POJ 3259【SPFA】
- poj 3259-- Wormholes(SPFA)
- POJ-3259 Wormholes(SPFA)
- Poj 3259 Wormholes ( SPFA
- POJ 3259 Wormholes spfa
- [POJ 3259] Wormholes [SPFA]
- poj 3259 Wormholes题解
- 【POJ 3259】Wormholes 【spfa判负环】
- poj 3259--Wormholes 【spfa判负环】
- POJ 3259-Wormholes-SPFA判负环
- Windows SDK 非模态对话框的消息处理
- Linux内核源码分析--zImage出生实录(Linux-3.0 ARMv7)
- Longest Substring Without Repeating Characters
- 7/13
- JAVA程序生成XML文件的格式规范化,缩进,美化。
- POJ 3259 Wormholes SPFA算法题解
- 详解UspLib库中如何使用Uniscribe
- python列表和QVariant
- HDU 1596 find the safest road(Dijkstra)
- Java强引用、 软引用、 弱引用、虚引用
- New package not yet registered with the system 解决方法
- 对YUV格式的详细描述,以及存储形式
- python3.2 学习资料
- 多边形裁剪一:Sutherland-Hodgman算法