Wormholes-(Bellman-Frod)
来源:互联网 发布:数据分析实战 文字版 编辑:程序博客网 时间:2024/06/09 17:13
Wormholes
Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 71 Accepted Submission(s) : 22
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ's farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.
As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .
To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
NOYES
用Bellman-Frod判断是否有负回路,我本来是对每个点都判断,但是就判断一个(1点)也能AC
代码:
#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;#define inf 0x7ffffff;struct paths{ int s,e,t;}p[5210];int n,m,w,dis[505];int main(){ int f,i,j,k,temp; scanf("%d",&f); while(f--) { scanf("%d%d%d",&n,&m,&w); for(i=1;i<=m;i++) { scanf("%d%d%d",&p[i].s,&p[i].e,&p[i].t); p[i+m].s=p[i].e; p[i+m].e=p[i].s; p[i+m].t=p[i].t; } k=2*m+w; for(i=2*m+1;i<=k;i++) { scanf("%d%d%d",&p[i].s,&p[i].e,&temp); p[i].t=-temp; } m=k; bool flag; for(j=1;j<=n;j++) dis[j]=inf; dis[1]=0; for(j=1;j<=n;j++) //j<=n和j<=n-1都是对的 { flag=false; for(k=1;k<=m;k++) { if(dis[p[k].s]>p[k].t+dis[p[k].e]) {dis[p[k].s]=p[k].t+dis[p[k].e];flag=true;} } if(!flag) break; } flag=false; for(k=1;k<=m;k++) { if(dis[p[k].s]>p[k].t+dis[p[k].e]) {flag=true; break;} } if(flag) printf("YES\n"); else printf("NO\n"); } return 0;}
代码2(每个点都进行判断):
#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;#define inf 0x7ffffff;struct paths{ int s,e,t;}p[5210];int n,m,w,dis[505];int main(){ int f,i,j,k,temp; scanf("%d",&f); while(f--) { scanf("%d%d%d",&n,&m,&w); for(i=1;i<=m;i++) { scanf("%d%d%d",&p[i].s,&p[i].e,&p[i].t); p[i+m].s=p[i].e; p[i+m].e=p[i].s; p[i+m].t=p[i].t; } k=2*m+w; for(i=2*m+1;i<=k;i++) { scanf("%d%d%d",&p[i].s,&p[i].e,&temp); p[i].t=-temp; } m=k; bool flag=false; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) dis[j]=inf; dis[i]=0; for(j=1;j<=n;j++) //j<=n和j<=n-1都是对的 { bool fff=false; for(k=1;k<=m;k++) { if(dis[p[k].s]>p[k].t+dis[p[k].e]) {dis[p[k].s]=p[k].t+dis[p[k].e]; fff=true;} } if(!fff) break; //没有这句会超时 } for(k=1;k<=m;k++) { if(dis[p[k].s]>p[k].t+dis[p[k].e]) {flag=true; break;} } if(flag) break; } if(flag) printf("YES\n"); else printf("NO\n"); } return 0;}
一个不错的题解:http://www.cnblogs.com/jackge/archive/2013/04/23/3038494.html
- Wormholes-(Bellman-Frod)
- POJ 3259 Bellman-frod
- Wormholes(bellman-Ford)
- 蓝桥杯--最短路 -- Djikstra,Bellman-Frod,SPFA
- poj 3259 Wormholes(SPFA || Bellman-Ford)
- SDUT-VJ-Wormholes(Bellman算法)
- POJ 3259 Wormholes(Bellman-Ford)
- poj 3259 Wormholes (Bellman-ford)
- UVA - 558 Wormholes (Bellman-ford)
- POJ 3259 Wormholes(Bellman-Ford判负环)
- poj 3259 Wormholes(Bellman-Ford)
- POJ 3259 Wormholes (SPFA&&BellMan Ford)
- poj3259 Wormholes(bellman负权处理)
- POJ 3259Wormholes(最短路bellman)
- POJ 3259 Wormholes (Bellman Ford判负环)
- POJ - 3259----Wormholes(Bellman-Ford)
- HDU 3259 Wormholes(Bellman-Ford算法)
- HDU 3259 Wormholes(Bellman-Ford算法)
- 临时表空间操作总结
- pat1035
- js中的逻辑与(&&)和逻辑或(||)
- Maven第三篇【Maven术语、pom.xml介绍】
- UVA 11019 Matrix Matcher (hash+kmp)
- Wormholes-(Bellman-Frod)
- SHUOJ 方格涂色
- Oracle数据库插入多条数据,主键自增
- [第六季]6.更复杂的正则表达式验证
- 设计模式(一)
- 在ubantu下安装搜狗
- ajax接收遍历处理json格式数据
- codeforces838A-Binary Blocks
- solr6 安装及部署到tomcat(OS X)