POJ 3259 最短路 判负环
2013-08-18 18:15
183 查看
SPFA 判负环 某个点访问n次
#include <set> #include <map> #include <cmath> #include <queue> #include <stack> #include <string> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; typedef long long LL; const double PI = acos(-1.0); template <class T> inline T MAX(T a, T b){if (a > b) return a;return b;} template <class T> inline T MIN(T a, T b){if (a < b) return a;return b;} const int N = 111; const int M = 11111; const LL MOD = 1000000007LL; const int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1}; const int INF = 0x3f3f3f3f; int dist[555]; struct node { int v, w, next; }edge[6666]; int head[555], cnt; int n, m, vis[555]; int inq[555]; inline void addnode(int u, int v, int w) { edge[cnt].v = v; edge[cnt].next = head[u]; edge[cnt].w = w; head[u] = cnt++; } bool solve() { queue < int > q; fill(dist, dist + n + 1, INF); memset(vis, 0, sizeof(vis)); memset(inq, 0, sizeof(inq)); dist[1] = 0; vis[1]++; inq[1]++; int u, i, j, k, v; q.push(1); while (!q.empty()) { u = q.front(); q.pop(); inq[u]--; for (i = head[u]; ~i; i = edge[i].next) { v = edge[i].v; if (dist[u] + edge[i].w < dist[v]) { dist[v] = dist[u] + edge[i].w; if (!inq[v]) {q.push(v); vis[v]++; inq[v]++; if (vis[v] >= n) return true;} } } } return false; } int main() { int T; scanf("%d", &T); while (T--) { int u, v, w, k, i; memset(head, -1, sizeof(head)); cnt = 0; scanf("%d%d%d", &n, &m, &k); for (i = 0; i < m; ++i) { scanf("%d%d%d", &u, &v, &w); addnode(u, v, w); addnode(v, u, w); } for (i = 0; i < k; ++i) { scanf("%d%d%d", &u, &v, &w); addnode(u, v, -w); } int flag = 0; if (solve()) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- POJ 3259 Wormholes(floyd求负环,最短路)
- POJ-3259 Wormholes(最短路,bellman求负环)
- poj 3259 最短路判负环 spfa算法和Bellman_ford算法
- POJ - 3259 Wormholes(SPFA最短路判断负环)
- POJ:3259-Wormholes(最短路判断负环)
- POJ-3259 Wormholes【单源最短路判负环】
- 最短路 SPFA 判断负环 静态邻接表(链式前向星) HDU 2544 最短路 POJ 3259 Wormholes
- poj 3259 Wormholes 图论 最短路 判负环
- POJ 3259——Wormholes——————【最短路、SPFA、判负环】
- POJ 3259 Wormholes (最短路 SPFA 判断负环)
- Poj(3259),SPFA,判负环
- POJ 3259 Wormholes 贝尔曼福特算法判负环
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】
- Wormholes( POJ 3259)(Bellman-Ford+SPFA)(判断是否有负权环)(最短路模板)
- POJ 3259 Wormholes(最短路)
- POJ 3259 Wormholes (判断负环,SPFA或Bellman-Ford都可)
- POJ 3259 Wormholes Bellman-Ford找负环
- Wormholes POJ - 3259 最短路之找负圈(Bellman-Ford)
- poj 3259 Wormholes【spfa判断负环】
- POJ 3259 Wormholes【bellman_ford判断负环——基础入门题】