UVa 558 Wormholes / Bellman-Ford
来源:互联网 发布:2841财神一码数据分析 编辑:程序博客网 时间:2024/05/18 00:25
有个人要回到过去看大爆炸 就判断有没有负环 有负环就possible
Bellman-Ford 或者SPFA判断负环
#include <stdio.h>struct node{int s;int e;int w;}a[2010];int n,m;int dis[1010];bool BF(){int i,j,k;int x,y,z;for(i = 0;i < n; i++)dis[i] = 999999999;dis[0] = 0;for(i = 0;i < n-1; i++){for(j = 0;j < m; j++){x = a[j].s;y = a[j].e;z = a[j].w;if(dis[y] > dis[x] + z)dis[y] = dis[x] + z;}}for(j = 0;j < m; j++)// 如果松弛了n-1次还能松弛 说明有负环 {x = a[j].s;y = a[j].e;z = a[j].w;if(dis[y] > dis[x] + z){return false;}}return true; }int main(){int t,i;scanf("%d",&t);while(t--){scanf("%d %d",&n,&m);for(i = 0;i < m; i++)scanf("%d %d %d",&a[i].s,&a[i].e,&a[i].w);if(!BF()){puts("possible");}else{puts("not possible");}}return 0;}
下面是SPFA
#include <cstdio>#include <vector>#include <queue>#include <cstring>using namespace std;const int MAX = 1010;struct node{int e;int w;};int n,m;int dis[MAX];int cnt[MAX];bool vis[MAX];vector <node> a[MAX];bool SPFA(){int i;queue <int> q;memset(vis,false,sizeof(vis));memset(cnt,0,sizeof(cnt));for(i = 0;i < n; i++)dis[i] = 999999999;dis[0] = 0;q.push(0);while(!q.empty()){int x = q.front();q.pop();vis[x] = false;int len = a[x].size();for(i = 0; i < len; i++){node t = a[x][i];if(dis[t.e] > dis[x] + t.w){dis[t.e] = dis[x] + t.w;if(!vis[t.e]){q.push(t.e);vis[t.e] = true;cnt[t.e]++;if(cnt[t.e] > n)return false;}}}}return true;}int main(){int t,i,x,y,z;scanf("%d",&t);while(t--){scanf("%d %d",&n,&m);for(i = 0;i < n; i++)a[i].clear();while(m--){scanf("%d %d %d",&x,&y,&z);a[x].push_back((node){y,z});}if(!SPFA())puts("possible");elseputs("not possible");}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判断负环)
- uva 558 Wormholes (Bellman-Ford算法判断负环)
- Bellman-Ford || SPFA :Wormholes
- Wormholes(bellman-Ford)
- UVa 558 Wormholes 判断负权环 对比bellman-ford和spfa效率
- pku 3259 Wormholes(bellman-ford)
- POJ 3259 Wormholes(bellman-ford)
- POJ 3259 Wormholes Bellman-Ford
- poj 3259 Wormholes(Bellman-Ford)
- POJ3259《Wormholes》方法:Bellman-ford
- POJ 3259 Wormholes (Bellman-ford)
- POJ 3259 Wormholes 【bellman-ford】
- poj 3259 Wormholes (Bellman-ford)
- poj3259 Wormholes (Bellman-Ford算法)
- VC CString与char*相互转换
- php简单对象与数组的转换
- 在Scientific Linux 6.4(64位)上安装Gitlab 6.1
- myelipcse 不编译 的问题2: 我也不知道为什么
- 写弹窗
- UVa 558 Wormholes / Bellman-Ford
- 【算法】求二进制中1的个数
- HMM(三)
- json格式化查看好工具
- QTP脚本编写常用技巧
- 网络游戏服务器编程01 大纲
- ubuntu下怎样防止rm删除后恢复问题
- AdaBoost(四)
- 豆瓣广告系统持续发布相关的实践—豆瓣网_张伟