Wormholes (POJ - 3259)

本文探讨了在给定的农场地图中,如何利用虫洞实现时间旅行的问题。通过分析路径和虫洞特性,采用Bellman-Ford算法检测是否存在导致时间倒流的负权环,从而实现农夫约翰的时光回溯梦想。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Wormholes (POJ - 3259)

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.. MW+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 backT 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.

题目翻译:

在探索他的许多农场时,农夫约翰发现了许多令人惊奇的虫洞。虫洞是非常特殊的,因为它是一个单向的路径,把你送到它的目的地,在你进入虫洞之前的时间!FJ的每个农场包括N(1≤N≤500)块,方便编号为1。N, M(1≤M≤2500)条路径,W(1≤W≤200)条虫洞。

由于FJ是一个狂热的时间旅行爱好者,他想做以下事情:从某个领域开始,通过一些路径和虫洞旅行,并在他最初出发前回到起始领域。也许他能遇见他自己:)。

为了帮助FJ知道这是否可能,他会提供给你他农场F(1≤F≤5)的完整地图。任何路径的运行时间都不会超过1万秒,也没有虫洞可以让FJ回到超过1万秒的时间。

输入

第1行:一个整数,后面是F - F farm描述。

每个farm的第1行:三个空格分隔的整数:N、M和W

行2 . .每个农场的M+1:三个空格分隔的数字(S, E, T),分别描述:S和E之间的双向路径,需要T秒的时间。两个字段可能由多个路径连接。

行M + 2 . .每个农场的M+ W+1:三个用空格分隔的数字(S, E, T)分别描述:从S到E的单向路径,也将旅行者向后移动了T秒。

输出

行1 . .F:对于每个农场,如果FJ能够达到他的目标,输出“YES”,否则输出“NO”(不包含引号)。

提示

对于农场1,FJ不能回到过去。

对于farm 2, FJ可以通过周期1->2->3->1回到时间上,在他离开前1秒回到他的起始位置。他可以从周期的任何地方开始来完成这个任务。

思路:要想回到原点,虫洞可以让你回到过去,就相当于一个负权,然后判断是否存在负环。我用了两种方法,Floyd超时了。。。。。。Bellmand-Ford完美a过。

Floyd超时代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#define inf 0x3f3f3f3f
#define MAX 510
using namespace std;
int dp[MAX][MAX];
int n,m,w,k;
int num=0;
int floyd()
{
    int i,j,k;
    for(k=1; k<=n; k++)
        for(i=1; i<=n; i++)
            for(j=1; j<=n; j++)
            {
                int t=dp[i][k]+dp[k][j];
                if(dp[i][j]>t)
                    dp[i][j]=t;
                if(dp[i][i]<0)
                    return 1;
            }
    return 0;
}
int main()
{
    int fj;
    scanf("%d",&fj);
    while(fj--)
    {
        int i,a,b,c;
        scanf("%d%d%d",&n,&m,&w);
        memset(dp,inf,sizeof(dp));
        for(i=1; i<=n; i++)
            dp[i][i]=0;
        for(i=1; i<=m; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            if(dp[a][b]>c)
                dp[a][b]=dp[b][a]=c;
        }
        for(i=1; i<=w; i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            dp[a][b]=-c;
        }
        num++;
        if(!floyd())
            printf("NO\n");
        else
            printf("YES\n");
    }
    return 0;
}

Bellmand-Ford AC代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <math.h>
#define inf 0x3f3f3f3f
#define MAX 10010
using namespace std;
int dis[MAX],bak[MAX],u[MAX],v[MAX],t[MAX],check,flag,n,m,w;
int main()
{
    int f;
    scanf("%d",&f);
    while(f--)
    {
        scanf("%d%d%d",&n,&m,&w);
        for(int i=1; i<=m; i++)
            scanf("%d%d%d",&u[i],&v[i],&t[i]);
        for(int i=m+1; i<=m+w; i++)
        {
            scanf("%d%d%d",&u[i],&v[i],&t[i]);
            t[i]=-t[i];
        }
        for(int i=1; i<=n; i++)
            dis[i]=inf;
        dis[1]=0;
        for(int k=1; k<n; k++)
        {
            for(int i=1; i<=n; i++)
                bak[i]=dis[i];
            for(int i=1; i<=m; i++)
            {
                if(dis[v[i]]>dis[u[i]]+t[i])
                    dis[v[i]]=dis[u[i]]+t[i];
                if(dis[u[i]]>dis[v[i]]+t[i])
                    dis[u[i]]=dis[v[i]]+t[i];
            }
            for(int i=m+1; i<=m+w; i++)
            {
                if(dis[v[i]]>dis[u[i]]+t[i])
                    dis[v[i]]=dis[u[i]]+t[i];
            }
            check=0;
            for(int i=1; i<=n; i++)
                if(bak[i]!=dis[i])
                {
                    check=1;
                    break;
                }
            if(check==0)
                break;
        }
        flag=0;
        for(int i=1; i<=m; i++)
            if(dis[v[i]]>dis[u[i]]+t[i])
                flag=1;
        if(flag)
            printf("YES\n");
        else
            printf("NO\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值