首页 技术 正文
技术 2022年11月14日
0 收藏 459 点赞 3,387 浏览 3267 个字

Wormholes

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 39078   Accepted: 14369

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..NM (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, FF farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: NM, and W 
Lines 2..M+1 of each farm: Three space-separated numbers (SET) 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 (SET) 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题意:每个农场有N各区域,连接所有区域的是M个双向路径和W个单向时空隧道,从S->E若为路径则花费T秒,若为时空隧道则倒退T秒。问是否可以从某点出发,转一圈回来,回到出发时刻之前。
思路:因为时空隧道实现倒退,所以将其权值设为负值,利用ford判断是否存在负环。
#include"cstdio"
#include"cstring"
using namespace std;
const int MAXN=;
const int INF=0x3fffffff;
struct Edge{
int from,to,cost;
}es[MAXN];
int N,M,W;
int E;
int d[MAXN];
bool ford(int s)
{
for(int i=;i<=N;i++) d[i]=INF;
d[s]=; int n=N;
while(n--)
{
bool update=false;
for(int i=;i<E;i++)
{
Edge e=es[i];
if(d[e.from]!=INF&&d[e.to]>d[e.from]+e.cost)
{
d[e.to]=d[e.from]+e.cost;
update=true;
}
}
if(!update) break; } if(n==-) return true;
else return false;
}
int main()
{
int F;
scanf("%d",&F);
while(F--)
{
E=;
scanf("%d%d%d",&N,&M,&W);
for(int i=;i<M;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
es[E].from=u,es[E].to=v,es[E++].cost=c;
es[E].from=v,es[E].to=u,es[E++].cost=c;
}
for(int i=;i<W;i++)
{
int u,v,c;
scanf("%d%d%d",&u,&v,&c);
es[E].from=u,es[E].to=v,es[E++].cost=-c;//倒退c秒
} if(ford()) printf("YES\n");
else printf("NO\n");
} return ;
}

spfa+前向星可解决重边问题

#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
const int MAXN=;
const int INF=0x3f3f3f3f;
struct Edge{
int v,w,next;
}es[];
int head[MAXN],tot;
void addedge(int u,int v,int w)
{
es[tot].v=v;
es[tot].w=w;
es[tot].next=head[u];
head[u]=tot++;
}
int d[MAXN],vis[MAXN],cnt[MAXN];
int n,m,k;
bool spfa(int s)
{
for(int i=;i<=n;i++)
{
d[i]=INF;
vis[i]=;
cnt[i]=;
}
d[s]=;
queue<int> que;
que.push(s);
vis[s]=;
cnt[s]++;
while(!que.empty())
{
int u=que.front();que.pop();
vis[u]=;
for(int i=head[u];i!=-;i=es[i].next)
{
Edge e=es[i];
if(d[e.v]>d[u]+e.w)
{
d[e.v]=d[u]+e.w;
if(!vis[e.v])
{
vis[e.v]=;
que.push(e.v);
cnt[e.v]++;
if(cnt[e.v]>=n) return true;
}
}
}
}
return false;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
memset(head,-,sizeof(head));
tot=;
scanf("%d%d%d",&n,&m,&k);
for(int i=;i<m;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,w);
addedge(v,u,w);
}
for(int i=;i<k;i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
addedge(u,v,-w);
}
if(spfa()) printf("YES\n");
else printf("NO\n");
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,104
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,581
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,428
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,835
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,918