1715: [Usaco2006 Dec]Wormholes 虫洞

Description

John在他的农场中闲逛时发现了许多虫洞。虫洞可以看作一条十分奇特的有向边,并可以使你返回到过去的一个时刻(相对你进入虫洞之前)。John的每个农场有M条小路(无向边)连接着N (从1..N标号)块地,并有W个虫洞。其中1<=N<=500,1<=M<=2500,1<=W<=200。 现在John想借助这些虫洞来回到过去(出发时刻之前),请你告诉他能办到吗。 John将向你提供F(1<=F<=5)个农场的地图。没有小路会耗费你超过10000秒的时间,当然也没有虫洞回帮你回到超过10000秒以前。

Input

* Line 1: 一个整数 F, 表示农场个数。

* Line 1 of each farm: 三个整数 N, M, W。

* Lines 2..M+1 of each farm: 三个数(S, E, T)。表示在标号为S的地与标号为E的地中间有一条用时T秒的小路。

* Lines M+2..M+W+1 of each farm: 三个数(S, E, T)。表示在标号为S的地与标号为E的地中间有一条可以使John到达T秒前的虫洞。

Output

* Lines 1..F: 如果John能在这个农场实现他的目标,输出"YES",否则输出"NO"。

Sample Input

2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8

Sample Output

NO
YES

HINT

 

Source

 
 
做F次spfa判负环即可。。。
 1 #include<iostream>
 2 #include<cstdlib>
 3 #include<cmath>
 4 #include<cstring>
 5 #include<cstdio>
 6 #include<algorithm>
 7 #include<string>
 8 #include<map>
 9 #include<queue>
10 #include<vector>
11 #include<set>
12 #define mod 1000000007
13 #define inf 1000000000
14 #define maxn 30005
15 #define maxm 30005*2
16 #define eps 1e-10
17 #define ll long long
18 #define for0(i,n) for(int i=0;i<=(n);i++)
19 #define for1(i,n) for(int i=1;i<=(n);i++)
20 #define for2(i,x,y) for(int i=(x);i<=(y);i++)
21 #define for3(i,x,y) for(int i=(x);i>=(y);i--)
22 #define for4(i,x) for(int i=head[x],y=e[i].go;i;i=e[i].next,y=e[i].go)
23 using namespace std;
24 struct edge{
25     int go,next,w;
26 }e[maxm];
27 int n,f,m,t,d[maxn],v[maxn],head[maxn],tot;
28 bool mark[maxn],flag;
29 int read(){
30     int x=0,f=1;char ch=getchar();
31     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
32     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
33     return x*f;
34 }
35 void insert(int x,int y,int z){
36     e[++tot].go=y;e[tot].next=head[x];e[tot].w=z;head[x]=tot;
37 }
38 void ins(int x,int y,int z){
39     insert(x,y,z);insert(y,x,z);
40 }
41 void spfa(int x){
42     if(mark[x]){flag=1;return ;}
43     mark[x]=1;
44     for(int i=head[x],y;i;i=e[i].next)
45         if(d[x]+e[i].w<d[y=e[i].go]){
46             d[y]=d[x]+e[i].w;
47             spfa(y);
48             if(flag)return;
49         }
50     mark[x]=0;
51 }
52 bool check(){
53     for1(i,n)d[i]=mark[i]=0;
54     flag=0;
55     for(int i=1;i<=n;i++){
56         spfa(i);
57         if(flag)return 1;
58     }
59     return 0;
60 }
61 int main(){
62     //freopen("input.txt","r",stdin);
63     //freopen("output.txt","w",stdout);
64     f=read();
65     for1(i,f){
66         n=read();m=read();t=read();
67         tot=0;
68         memset(head,0,sizeof(head));
69         memset(e,0,sizeof(e));
70         for1(j,m){
71             int u=read(),v=read(),ww=read();
72             ins(u,v,ww);
73         }
74         for1(j,t){
75             int u=read(),v=read(),ww=read();
76             insert(u,v,-ww);
77         }
78         if(check())printf("YES\n");
79         else printf("NO\n");
80     }
81     return 0;
82 }
View Code

 

posted @ 2016-03-14 10:45  HTWX  阅读(128)  评论(0编辑  收藏  举报