[POJ]Wormholes——Bellman、SPFA

Wormholes

Time Limit: 2000MS
Memory Limit: 65536K

一、原文

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

二、题意

有个农民他有 n 块田,然后现在给你 m 组田与田之间的距离关系,田与田之间所给出的路径都是双向的,但是有的田里有虫洞,它是可以把你送回到原来的 t 秒钟之前的位置的。这个农民想尝试一下,看自己能不能回到虫洞将他送回的目的地出发之前,比如说天块 3 有一个能到田块 1 的虫洞,穿越时间是3,也就是说但你走到田块 3 的位置时,你不能再向前走了,该虫洞会自动将你送回到通往田块 1 ,送的时间是 3 ,那也就是说如果我田块 3 达到田块 1需要的时间等于或大于 3 的话,那么就意味着这个人就回不去 1 了。
先输入一个数表示的是测试的组数,再输入三个数 n,m,w,有 n 块田,下面还会给出 m 个田与田之间的关系,然后 w 就是虫洞的数量了,然后给出的是虫洞的起始位置终点位置以及穿越所需要的时间。

三、分析

本题属于常规题,基本思想就是判断是否有负环。采用bellman或者spfa都可以。
解释下专有名词:
松弛就是dis[u]+cost[v]<dis[v],经过u点到v比直接到v成本更低,就更新dis[v]的过程。
解释为什么需要n-1次循环来松弛?
在这里插入图片描述
即v-1次,可以保证每层都松弛完成。

如果还有不清楚的地方,请移步:
关于图算法的介绍
https://blog.csdn.net/lyly1995/article/details/87868128

用bellman:

#include <iostream>
#include <queue>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
#include <cmath>
#define inf 0x3f3f3f3f
using namespace std;
int T,n,m,w,a,b;
int u[10000],v[10000],t[10000];
bool bellman_ford()
{
    int i,j;
    int dis[1000];
    for(i=1; i<=n; i++)
    {
        dis[i]=inf;
    }
    dis[1]=0;
    for(i=1; i<=n-1; i++) //最差的情况经过n-1次松弛你能遍历所有边,让所有边的权值发生变化
    {
        for(j=1; j<=m+w; j++)  //每轮操作都要遍历所有边,不论是双循环还是这样的单循环
        {
            if((dis[v[j]]>dis[u[j]]+t[j])&&j<=m)
            {
                dis[v[j]]=dis[u[j]]+t[j];  //这些更新就叫做松弛操作
            }
            else if((dis[u[j]]>dis[v[j]]+t[j])&&j<=m)
            {
                dis[u[j]]=dis[v[j]]+t[j];
            }
            else if((dis[v[j]]>dis[u[j]]-t[j])&&j>m)
            {
                dis[v[j]]=dis[u[j]]-t[j];
            }
        }
    }
    for(j=1; j<=m+w; j++)
    {
        if((dis[v[j]]>dis[u[j]]+t[j])&&j<=m)
        {
            return 1;
        }
        else if((dis[u[j]]>dis[v[j]]+t[j])&&j<=m)
        {
            return 1;
        }
        else if((dis[v[j]]>dis[u[j]]-t[j])&&j>m)
        {
            return 1;
        }
    }
    return 0;
}
int main()
{
    int i;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d %d %d",&n,&m,&w);
        for(i=1; i<=m+w; i++)
        {
            scanf("%d %d %d",&u[i],&v[i],&t[i]);
        }
        if(bellman_ford())
        {
            printf("YES\n");
        }
        else
        {
            printf("NO\n");
        }
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/lyly1995/article/details/87880885