POJ 3259 Wormholes
来源:互联网 发布:常见算法的时间复杂度 编辑:程序博客网 时间:2024/06/05 13:36
POJ 3259 Wormholes
[★★☆☆☆]图论 最短路 bellman
题目大意:
本质就是求该图是否存在负环。也就是如何求出一个图是否含有负环。
要注意的是输入该题中的path是双向的,wormhole是单向的样例
输入:
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8输出:
NO
YES解题思路:
这道题好气啊,我优化的一个条件导致wa,还是太年轻。
但是我感觉这道题是错的。。。我天啊- -
还有就是我代码最后帖的那组样例,好多网上的代码都是错的,但是交上去能AC,迷。代码
#include <iostream>#include <algorithm>using namespace std;typedef long long ll;const int INF = 1e18 + 7;int N, M, W;struct edge { int start, end; ll time;};edge E[5500];int cte;ll d[600];bool bellman(int s) { if (d[s] != INF) return 0; // 优化 for (int i = 1; i <= N; i++) { d[i] = INF; } d[s] = 0; int i; for (i = 1; i <= N; i++) { bool update = 0; for (int j = 0; j < cte; j++) { edge e = E[j]; if (d[e.start] != INF && d[e.end] > d[e.start] + e.time) { d[e.end] = d[e.start] + e.time;// if (d[e.end] < 0) return 1; //错的优化。。 update = 1; } } if (!update) break; } for (int j = 0; j < cte; j++) { edge e = E[j]; if (d[e.start] != INF && d[e.end] > d[e.start] + e.time) { return 1; } }// if (i == N+1) return 1; return 0;}int main() { int F; cin >> F; while (F--) { cte = 0; cin >> N >> M >> W; for (int i = 1; i <= N; i++) { d[i] = INF; } for (int i = 0; i < M; i++) { int s, e, t; cin >> s >> e >> t; edge te; te.start = s; te.end = e; te.time = t; E[cte++] = te; te.start = e; te.end = s; E[cte++] = te; } for (int i = 0; i < W; i++) { int s, e, t; cin >> s >> e >> t; edge te; te.start = s; te.end = e; te.time = -t; E[cte++] = te; } bool fl = 0; for (int i = 1; i <= N; i++) { fl = bellman(i); if (fl) break; }// fl = bellman(1); if (fl) cout << "YES\n"; else cout << "NO\n"; } return 0;}/*15 3 12 3 103 4 104 5 105 3 21*/
0 0
- poj 3259 Wormholes //SPFA
- POJ 3259 Wormholes
- poj 3259 Wormholes
- POJ 3259 Wormholes
- POJ 3259 Wormholes
- Poj 3259 Wormholes
- poj 3259 Wormholes
- poj-3259-Wormholes
- POJ-3259-Wormholes
- POJ 3259 Wormholes bellman_ford
- poj 3259 Wormholes
- poj 3259 Wormholes
- POJ 3259 Wormholes
- POJ 3259 Wormholes
- POJ 3259 Wormholes
- poj 3259 Wormholes
- POj 3259 Wormholes
- POJ 3259 Wormholes
- NC渗透测试
- ORA-01795: 列表中的最大表达式数为1000的解决方法详解
- Python socket ssl 的server端和client端程序
- IE6兼容性问题及解决办法汇总
- 解决EditText多行编辑时在ScrollView中无法滚动的问题
- POJ 3259 Wormholes
- ios RSA加解密
- 搭建springMVC报错
- 透视变换畸变校正 双线性
- delete 和 delete []的真正区别
- mac安装protobuf
- Camera2进行扫码解析
- string、char*、char[]
- poj 2262 Goldbach's Conjecture -- 筛法求素数打表