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");
}
}
- 07-01· 禁教唐诗算术能还幼儿快
- 07-01· 2011年06月17日
- 07-01· 唐诗宋词英译:李商隐 筹
- 07-01· 仿评《唐诗1000首》第186首
- 07-01· 没事干的时候背背唐诗吧
- 07-01· [转载]唐诗中“斜”字该读
- 07-01· 湖南醴陵瓷业转型升级
- 07-01· 奇瑞风云2两厢黑色|2010款
- 07-01· 摩根士丹利华鑫摩根士丹
- 07-01· 摩根士丹利华鑫近期优选
- 07-01· 中金投行部大摩出售中金
- 07-01· 摩根士丹利招聘6月2日【实
- 07-01· 营养防病圣典
- 07-01· 《博伽梵歌原意》之第十
- 07-01· [不错]斑斓圣典---减肥中常
- 07-01· 武乐圣典《太极武当》:武
- 07-01· 铁血英雄-现阶段战功牌兑
- 07-01· 2011年06月10日【原创】南歌
- 07-01· 【淘宝网信息】- 2010年的
- 07-01· 深圳品牌女装有哪些?