poj3259Wormholes(bellman判断负环的问题最短路)

本文介绍了一种利用特殊路径——虫洞实现时间旅行的方法。在一个包含多个区域与虫洞的农场中,通过特定路径组合可以达成回到过去的效果。具体探讨了如何通过算法验证这种可能性。

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

<div class="ptt" lang="en-US" style="text-align: center; font-size: 18pt; font-weight: bold; color: blue;">Wormholes</div><div class="plm" style="text-align: center;font-size:14px;"><table align="center"><tbody><tr><td><strong>Time Limit:</strong> 2000MS</td><td width="10px"> </td><td><strong>Memory Limit:</strong> 65536K</td></tr><tr><td><strong>Total Submissions:</strong> 43765</td><td width="10px"> </td><td><strong>Accepted:</strong> 16071</td></tr></tbody></table></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Description</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;"><p>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 <em>N</em> (1 ≤ <em>N</em> ≤ 500) fields conveniently numbered 1..<em>N</em>, <em>M</em> (1 ≤ <em>M</em> ≤ 2500) paths, and <em>W</em> (1 ≤ <em>W</em> ≤ 200) wormholes.</p><p>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 :) .</p><p>To help FJ find out whether this is possible or not, he will supply you with complete maps to <em>F</em> (1 ≤ <em>F</em> ≤ 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.</p></div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Input</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;">Line 1: A single integer, <em>F</em>. <em>F</em> farm descriptions follow. 
Line 1 of each farm: Three space-separated integers respectively: <em>N</em>, <em>M</em>, and <em>W</em> 
Lines 2..<em>M</em>+1 of each farm: Three space-separated numbers (<em>S</em>, <em>E</em>, <em>T</em>) that describe, respectively: a bidirectional path between <em>S</em> and <em>E</em> that requires <em>T</em> seconds to traverse. Two fields might be connected by more than one path. 
Lines <em>M</em>+2..<em>M</em>+<em>W</em>+1 of each farm: Three space-separated numbers (<em>S</em>, <em>E</em>, <em>T</em>) that describe, respectively: A one way path from <em>S</em> to <em>E</em> that also moves the traveler back <em>T</em> seconds.</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Output</p><div class="ptx" lang="en-US" style="font-family: "Times New Roman", Times, serif;font-size:14px;">Lines 1..<em>F</em>: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).</div><p class="pst" style="font-size: 18pt; font-weight: bold; color: blue;">Sample Input</p><pre class="sio" style="font-family: "Courier New", Courier, monospace;font-size:14px;">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





#include<stdio.h>
#include<string.h>
using namespace std;
#define INF 999999
struct node
{
    int s,e;
    int c;
}p[100000];

int m,n,w;
int v,e;
int dis[100000];
int Bellman_ford(int s)
{
    int i,j,flag;
    struct node n;

    for(i=1;i<=v;i++)  dis[i]=(i==s)?0:INF;//初始化
  //printf("+++%d  %d\n",v,e);
    for(i=0;i<v;i++)
    {
        for(j=0;j<e;j++)
        {
           n = p[j];
         //  printf("+_+_+_+%d %d %d\n",n.s,n.e,n.c);
           if(dis[n.e] > dis[n.s] + n.c)
           {
             //  printf("----%d\n",i);
               dis[n.e] = dis[n.s] + n.c;
               if(i==v-1) return 1;//因为如果存在负环的话,那么就会无限松弛,如果不存在 就会最多松弛(n-1)次  就是点的个数减一;
           }
        }
    }
    return 0;
}
int main()
{
    int t,i,j;

    int ss,ee,cc;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d%d%d",&n,&m,&w);

            v=n;
            e=0;
            for(i = 0; i < m; i++)
            {

                scanf("%d%d%d",&ss,&ee,&cc);
                p[e].s=ss;
                p[e].e=ee;
                p[e++].c=cc;
                p[e].s=ee;
                p[e].e=ss;
                p[e++].c=cc;


            }

            for(i=0; i < w; i++)
            {
                scanf("%d%d%d",&ss,&ee,&cc);
                p[e].s=ss;
                p[e].e=ee;
                p[e++].c=-cc;
            }

        if(Bellman_ford(1)) printf("YES\n");
        else printf("NO\n");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值