UVa 558 Wormholes (判负圈+Bellman-Ford算法)
来源:互联网 发布:stm32jlink 烧录软件 编辑:程序博客网 时间:2024/05/21 06:49
#include<cstdio>#include<algorithm>#include<cstring>#include<cmath>#include<iostream>#include<queue>#include<vector>#include<set>#include<climits>#include<map>#include<string>using namespace std;const int INF=100000000;const int maxn=1000+10;const int maxm=2000+10;int n,m;int first[maxn];int d[maxn];int u[maxm],v[maxm],w[maxm],nnext[maxm];int V;//稀疏图的邻接表void read_graph(){scanf("%d%d",&n,&m);for(int i=0;i<n;i++)first[i]=-1;for(int e=0;e<m;e++){scanf("%d%d%d",&u[e],&v[e],&w[e]);nnext[e]=first[u[e]];first[u[e]]=e;}}//Bellman-Ford算法bool Bellman_Ford(){queue<int> q;bool inq[maxn];for(int i=0;i<n;i++)d[i]=(i==0 ? 0:INF);V=0;memset(inq,0,sizeof(inq));q.push(0);while(!q.empty()){int x=q.front();q.pop();inq[x]=false;for(int e=first[x];e!=-1;e=nnext[e])if(d[v[e]]>d[x]+w[e]){d[v[e]]=d[x]+w[e];V++;if(V==n)return true; //更新次数超过n-1次,有负圈if(!inq[v[e]]){inq[v[e]]=true;q.push(v[e]);}}}return false;}int main(){int c;scanf("%d",&c);while(c--){read_graph();if(Bellman_Ford())printf("possible\n");else printf("not possible\n");}return 0;}
0 0
- UVa 558 Wormholes (判负圈+Bellman-Ford算法)
- Uva 558 - Wormholes//Bellman-Ford
- UVa 558 Wormholes / Bellman-Ford
- uva 558 Wormholes (Bellman-Ford算法判断负环)
- UVA - 558 Wormholes (Bellman-ford)
- poj3259 Wormholes (Bellman-Ford算法)
- uva 558 - Wormholes(Bellman Ford判断负环)
- POJ3259--Wormholes--Bellman-Ford算法经典题
- poj 3259 Wormholes(Bellman-ford 算法)
- POj 3259-Wormholes(Bellman-Ford算法)
- HDU 3259 Wormholes(Bellman-Ford算法)
- HDU 3259 Wormholes(Bellman-Ford算法)
- Bellman-Ford || SPFA :Wormholes
- Wormholes(bellman-Ford)
- UVa 558 Wormholes 判断负权环 对比bellman-ford和spfa效率
- POJ3259 Wormholes (Bellman-Ford最短路径算法)
- POJ 3259 Wormholes (Bellman-Ford算法的运用)
- poj 3259 Wormholes(最短路Bellman-Ford算法)
- Java之String类
- HDU 5363 Key Set(快速幂)
- [POJ 1947] Rebuilding Roads 树形dp
- Prim
- linux svn代码版本回退
- UVa 558 Wormholes (判负圈+Bellman-Ford算法)
- 3687-Labeling Balls-反向拓扑排序
- codeforces 567D One-Dimensional Battle Ships (map维护)
- 暑期校园生活中的那些烦心事!
- Shell编程基础
- java集合框架(map集合)
- hibernate SQL Error: 8152, SQLState: 22001
- poj-3687 Labeling Balls
- Lua笔记7-闭包2