您的位置:首页 > 服装鞋帽 > 女装 > pku 3259 Wormholes

pku 3259 Wormholes

luyued 发布于 2011-06-26 08:54   浏览 N 次  

Time Limit: 2000MS Memory Limit: 65536KTotal Submissions: 9675 Accepted: 3417

Description


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 W
Lines 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

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


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.

Source


USACO 2006 December Gold

#include
#include
#include
#include
using namespace std;
typedef long long i64;
const int M=505;
const int oo=0x7fffffff;
struct node {
int to, next, cap;
} edge[M*M*2];

int head[M], Q[M*M],mark[M],cnt[M],dist[M];
int n,e,ee;
int src,tot;
void add(int a, int b, int c) { //因为有^1所以一定要连续写在一起
//一般都看成有向的,最后的d为0, 裂点的题,才那部分看成无向的,c=d=流量
edge[tot].to = b, edge[tot].cap = c, edge[tot].next = head[a], head[a] = tot++;
}
bool spfa(){
for(int i=1; i<=n; i++) {
dist[i]=oo;
mark[i]=0;
cnt[i]=0;
}
dist[src]=0;
int l=0,h=0,k,y;
Q[l++] = src;
mark[src]=1;
cnt[src]++;
while (h < l) { //cnt 辅助数组,每个点进入大于等于N此,有负环
k = Q[h++];
mark[k]=0;
for (int i = head[k]; i != -1; i = edge[i].next) {
y = edge[i].to;
if (dist[y]>dist[k]+edge[i].cap) {
dist[y] = dist[k]+edge[i].cap;
if(!mark[y]){ //避免重复入列
mark[y]=1;
cnt[y]++;
if(cnt[y]>=n) return 1; //因为负环,队列空间要很大,所以动态写好
Q[l++] = y;

}
}
}
if(dist[src]<0) return 1;
}
return 0;
}
int main(){
int T;
cin>>T;
while(T--){
scanf("%d%d%d",&n,&e,&ee);
tot=0;
src=1;
for(int i=1; i<=n; i++) head[i]=-1;
for(int i=1; i<=e; i++){
int u,w,v;
scanf("%d%d%d",&u,&w,&v);
add(u,w,v);
add(w,u,v);
}
for(int i=1; i<=ee; i++){
int u,w,v;
scanf("%d%d%d",&u,&w,&v);
add(u,w,-v);
}
if(spfa()) printf("YES\n");
else printf("NO\n");
}
}

图文资讯
广告赞助商