H - Wormholes
2016-07-13 00:10
323 查看
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.
题意:给出普通的点,边和权值,以及虫洞的点,边,权值,问你是否能从某一点出发最后回到出发点,并且回到去的时间要在出发前。
先建边,普通边正值,虫洞边负值,接着对每一个点跑一次最短路(spfa),如果存在负环,就是成立的情况。
代码:
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.
题意:给出普通的点,边和权值,以及虫洞的点,边,权值,问你是否能从某一点出发最后回到出发点,并且回到去的时间要在出发前。
先建边,普通边正值,虫洞边负值,接着对每一个点跑一次最短路(spfa),如果存在负环,就是成立的情况。
代码:
#include <cstdio> #include <algorithm> #include <cstring> #include <queue> #include <map> #include <iostream> #include <vector> #include <cmath> #include <cstdlib> using namespace std; const int MAXN=505; const int MAXM=800005; bool vis[MAXN]; int cnt[MAXN]; int dir[MAXN]; struct Edge { int v; double w; }; vector<Edge> edge[MAXN]; void add(int u,int v,int w) { Edge tmp; tmp.v=v; tmp.w=w; edge[u].push_back(tmp); } bool spfa(int from,int n) { memset(vis,false,sizeof(vis)); memset(dir,0x3f,sizeof(dir)); memset(cnt,0,sizeof(cnt)); dir[from]=0; vis[from]=true; queue<int>que; while(!que.empty()) { que.pop(); } que.push(from); while(!que.empty()) { int now=que.front(); que.pop(); vis[now]=false; for(int i=0;i<edge[now].size();i++) { if(dir[edge[now][i].v]>edge[now][i].w+dir[now]) { dir[edge[now][i].v]=edge[now][i].w+dir[now]; if(!vis[edge[now][i].v]) { que.push(edge[now][i].v); vis[edge[now][i].v]=true; if(++cnt[edge[now][i].v]>n) return false; } } } } return true; } int main (void) { int num; cin>>num; while(num--) { int N,M,W; int s,e,t; for(int i=1;i<=N;i++) { edge[i].clear(); } scanf("%d %d %d",&N,&M,&W); for(int i=1;i<=M;i++) { scanf("%d %d %d",&s,&e,&t); add(s,e,t); add(e,s,t); } for(int i=0;i<W;i++) { scanf("%d %d %d",&s,&e,&t); add(s,e,-t); } bool re; for(int i=1;i<=N;i++) { re=spfa(i,N); if(re==false) { break; } } if(re==false) printf("YES\n"); else printf("NO\n"); } return 0; }
相关文章推荐
- 多边形和虚线矩形框
- 如何修改 Total commander 配置文件的路径
- NYOJ 925 最小生成树(按权值大小排序生成树)+查重函数unique
- UDP,TCP,SCTP使用场合
- 基本数据类型在不同字长机器上所占的位数
- 【荐2】Total Commander 7.57 配置选项 个性化设置备份,,,开启时如何自动最大化???(二)
- SCU2016-04 G题 01trie树第k大,树上异或路径
- 自定义无内存泄漏的Handler内部类
- 谈谈PE和ELF结构中的那些小事
- 装饰者模式vs适配器模式
- 【荐1】Total Commander 7.57 个人使用设置 及 常用快捷键 备忘
- 【greenDAO3】 项目搭建与增删改查操作
- Android基础之LaunchMode
- Java集成Weka做逻辑回归(Logistic Regression)
- C语言指针的易错点
- 开源流媒体云视频平台EasyDarwin中EasyCMS服务是如何进行命令转发和消息路由的
- 开源流媒体云视频平台EasyDarwin中EasyCMS服务是如何进行命令转发和消息路由的
- E - Frogger
- TCP/IP编程之getsockopt/setsockopt函数详解
- 微服务系统中的服务发现机制