POJ
来源:互联网 发布:上位机编程语言 编辑:程序博客网 时间:2024/06/10 13:09
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.
Input
Line 1: A single integer, F. F farm descriptions follow.Line 1 of each farm: Three space-separated integers respectively: N, M, and WLines 2.. M+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.Lines M+2.. M+ W+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Lines 1.. F: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).
Sample Input
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
Sample Output
NOYES
Hint
For farm 1, FJ cannot travel back in time.For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this.
【题意】题目不难,讲的是一个人有n个农场,农场里有m条无向的道路,连接两个农场,从两个中的任意一个到达另一个都要一段时间,农场里还有w个虫洞,也是连接两个农场,但是从第一个农场到第二个农场需要的时间为负数,现在问你能不能选择出一个农场使得从该农场出发后回到该农场的时间为负数。其中道路是双向的,但是虫洞是单项的,如果有一个农场满足该条件就输出YES,否则为NO。样例有多组。
【分析】题目简单,就是判断所给的图中是否有负权回路,如果存在那么一定存在所给的农场,就输出yes。
【代码】
#include <cstdio>#include <cstring>#include <cstdlib>#include <vector>#include <queue>#include <cmath>#include <iostream>#include <algorithm>#include <vector>using namespace std;typedef struct{ int to,val;//分别表示一条边的终点和这条边的权值}edge;vector < edge > mat[505];//统计和当前点相连的边,mat【i】表示以i为起点的边int n,m,w;int change[505];//统计改变次数int value[505];//当前价值queue <edge> ans;bool spfa(void){ memset(change,0,sizeof(change)); fill(value,value+504,0x5fffffff);//一系列初始化的操作 while(!ans.empty()) ans.pop();//因为多组输入,所以要清空 edge x,y; x.to=1,x.val=0; value[x.to]=0;//初始化第一条边 change[1]=1; ans.push(x); while(!ans.empty()) { x=ans.front(); ans.pop(); int len=mat[x.to].size(); for(int i=0;i<len;i++)//每次取出和x.to相连的边 { y=mat[x.to][i];//取出后的y这条边的起点是x.to,终点是y.to,权值是y.val if(value[y.to]>value[x.to]+y.val)//如果这条边终点的权值可以减小,那么更新它 { value[y.to]=value[x.to]+y.val; change[y.to]++;//用来统计这个点的改变次数 ans.push(y);//因为这点改变了,所以和这个点相连的边就要重新统计 if(change[y.to]==n)//如果某个点被更新了n次说明这个图中存在负权回路,所以就输出yes return true; } } } return false;}int main (){ //freopen("in.txt","r",stdin); int T; scanf("%d",&T); while(T--) { for(int i=0;i<505;i++) mat[i].clear(); scanf("%d%d%d",&n,&m,&w); int a,b,c; edge x; for(int i=0;i<m;i++) { scanf("%d%d%d",&a,&b,&c); x.to=b,x.val=c; mat[a].push_back(x); x.to=a; mat[b].push_back(x); } for(int i=0;i<w;i++) { scanf("%d%d%d",&a,&b,&c); x.to=b,x.val=-c; mat[a].push_back(x);//注意虫洞是有向的,道路是无向的 } if(spfa()) printf("YES\n"); else printf("NO\n"); } return 0;}
0 0
- POJ
- poj
- POJ
- POJ
- poj
- poj
- POJ
- POJ
- poj
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- POJ
- hdu 4288 线段树+离散化+离线处理
- mongodb nosql 大数据提高查询速度-索引
- 初学Map
- 欢迎使用CSDN-markdown编辑器
- sql优化总结 / sql中decode(...)函数的用法
- POJ
- swift——复合类型——tuple
- Python-pandas模块数据处理
- Java 1.5,1.6,1.7,1.8新特性整理
- 51nod 1118 机器人走方格 & 1119 机器人走方格 V2(排列组合)
- not found while looking for property
- 工业设计公司对工业设计的讲解
- HDU 1014 Uniform Generator
- swift——复合类型——String