POJ3259(经典bellman判断负权)
2016-08-08 15:13
302 查看
POJ3259
题意:有n个虫洞,问有从一些虫洞穿行过程中因为时间的快慢能遇到自己吗?相对论。
其实题很简单,就是问有没有负权的存在,若有将会一直松弛。
题意:有n个虫洞,问有从一些虫洞穿行过程中因为时间的快慢能遇到自己吗?相对论。
其实题很简单,就是问有没有负权的存在,若有将会一直松弛。
#include<iostream> using namespace std; #define INF 0x3f3f3f3f #define M 5200 int dist[M]; int n,m,f; int all; class Node //一般用结构体或者是类表示起点终点 { public: int s,e,t; }node[M]; int bellman() { for(int i = 1;i <= n; i++) dist[i] = INF; for(int i = 0;i < n - 1; i++){ bool flag = false; for(int j = 0;j < all; j++){ if(dist[node[j].e] > dist[node[j].s] + node[j].t){ //松弛每一条边 flag = true; dist[node[j].e] = dist[node[j].s] + node[j].t; } } if(!flag) break; } for(int i = 0;i < all; i++) if(dist[node[i].e] > dist[node[i].s] + node[i].t) //判断负环 return true; return false; } int main() { int T; cin>>T; while(T--) { all = 0; cin>>n>>m>>f; for(int i = 0;i < m; i++){ //正权 int a,b,time; cin>>a>>b>>time; node[all].s = a; node[all].e = b; node[all].t = time; all++; node[all].s = b; node[all].e = a; node[all].t = time; all++; } for(int i = 0;i < f; i++){ //负权 int a,b,time; cin>>a>>b>>time; node[all].s = a; node[all].e = b; node[all].t = -time; all++; } if(bellman()) cout<<"YES"<<endl; else cout<<"NO"<<endl; } return 0; }
相关文章推荐
- POJ3259(Wormholes)(Bellman-Ford判断负权图 )
- bellman判断负权回路--poj3259
- PKU 3259 Wormholes - 判断负权回路 Bellman-Ford
- hdu 1317 XYZZY(bellman_ford判断环,有负权)
- Wormholes( POJ 3259)(Bellman-Ford+SPFA)(判断是否有负权环)(最短路模板)
- POJ3259 判断是否存在负权环
- poj3259 Wormholes 图的负权回路判定,Bellman_Ford
- UVa 558 Wormholes 判断负权环 对比bellman-ford和spfa效率
- poj1860(Bellman-Ford变形,含负权的单源最短路径,判断是否有正权环)
- POJ 3259 Wormholes (Bellman-Ford/SPFA 判断是否存在负权环)
- 最短路的Bellman-Ford算法 【判断有无负权环】
- POJ 3259 Wormholes (Bellman-Ford/SPFA 判断是否存在负权环)
- poj3259 Wormholes(bellman负权处理)
- POJ3259--Wormholes--Bellman-Ford算法经典题
- POJ 3259 Wormholes (Bellman-Ford/SPFA 判断是否存在负权环)
- POJ 3259 Wormholes (Bellman-Ford/SPFA 判断是否存在负权环)
- poj3259 Wormholes 最短路 spfa 负权环判断
- poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)
- 差分约束系统之Bellman_Ford与Spfa判断负权回路
- poj 3259 bellman-ford算法 判断是否存在负权回路