POJ-3259 Wormholes
来源:互联网 发布:什么是seo是什么 编辑:程序博客网 时间:2024/06/10 00:37
题目链接:https://vjudge.net/problem/POJ-3259
有n个农场,有m条路(双向)连接这些农场,又有w条虫洞(单向且负权)连接农场
问是否可以在某一点出发,然后在出发时间前回到出发点
简单来说就是找负环,这里用bellman-ford算法
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespace std;const int N=500+5;const int M=5200+5;const int inf=0x7f7f7f7f;struct edge{int a,b;int c;edge(){}edge(int aa,int bb,int cc){a=aa;b=bb;c=cc;}}e[M];int n,m,w;int d[N],cnt;bool BF(){memset(d,inf,sizeof(d));d[1]=0;for(int i=1;i<n;i++){bool flag=false;for(int j=0;j<cnt;j++){if(d[e[j].b]>d[e[j].a]+e[j].c){d[e[j].b]=d[e[j].a]+e[j].c;flag=true;}}if(!flag) return false;}for(int j=0;j<cnt;j++)if(d[e[j].b]>d[e[j].a]+e[j].c)return true;return false;}int main(){int T;scanf("%d",&T);int a,b,c;while(T--){cnt=0;scanf("%d%d%d",&n,&m,&w);for(int i=0;i<m;i++){scanf("%d%d%d",&a,&b,&c);e[cnt++]=edge(a,b,c);e[cnt++]=edge(b,a,c);}for(int i=0;i<w;i++){scanf("%d%d%d",&a,&b,&c);e[cnt++]=edge(a,b,-c);}if(BF()) printf("YES\n");else printf("NO\n");}return 0;}
阅读全文
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
- 带参数表单填报表提交后自动刷新页面
- TabLayout的一些使用
- 《AV Foundation 开发秘籍》读书笔记(三)
- linux如何判断服务器是否被入侵
- docker远程api的使用
- POJ-3259 Wormholes
- UVa
- python
- 机器学习中的高斯过程
- 菜鸟教程中TCP/IP教程Word版
- 遇见《凯哥自媒体赚钱秘籍》—原来这才是我最想要的日子
- Windows 下编译 OpenSSL 32&64
- 阿里资深专家专访:流量清洗与人工智能
- 微信支付:curl出错,错误码:60