Wormholes-(Bellman-Frod)

来源:互联网 发布:数据分析实战 文字版 编辑:程序博客网 时间:2024/06/09 17:13

Wormholes

Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
Total Submission(s) : 71   Accepted Submission(s) : 22
Problem 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, <i>F</i>. <i>F</i> farm descriptions follow. <br>Line 1 of each farm: Three space-separated integers respectively: <i>N</i>, <i>M</i>, and <i>W</i> <br>Lines 2..<i>M</i>+1 of each farm: Three space-separated numbers (<i>S</i>, <i>E</i>, <i>T</i>) that describe, respectively: a bidirectional path between <i>S</i> and <i>E</i> that requires <i>T</i> seconds to traverse. Two fields might be connected by more than one path. <br>Lines <i>M</i>+2..<i>M</i>+<i>W</i>+1 of each farm: Three space-separated numbers (<i>S</i>, <i>E</i>, <i>T</i>) that describe, respectively: A one way path from <i>S</i> to <i>E</i> that also moves the traveler back <i>T</i> seconds.
 

Output
Lines 1..<i>F</i>: For each farm, output "YES" if FJ can achieve his goal, otherwise output "NO" (do not include the quotes).
 

Sample Input
23 3 11 2 21 3 42 3 13 1 33 2 11 2 32 3 43 1 8
 

Sample Output
NOYES

用Bellman-Frod判断是否有负回路,我本来是对每个点都判断,但是就判断一个(1点)也能AC

代码:

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;#define inf 0x7ffffff;struct paths{    int s,e,t;}p[5210];int n,m,w,dis[505];int main(){    int f,i,j,k,temp;    scanf("%d",&f);    while(f--)    {        scanf("%d%d%d",&n,&m,&w);        for(i=1;i<=m;i++)        {            scanf("%d%d%d",&p[i].s,&p[i].e,&p[i].t);            p[i+m].s=p[i].e; p[i+m].e=p[i].s; p[i+m].t=p[i].t;        }        k=2*m+w;        for(i=2*m+1;i<=k;i++)        {            scanf("%d%d%d",&p[i].s,&p[i].e,&temp);            p[i].t=-temp;        }        m=k;        bool flag;        for(j=1;j<=n;j++)                dis[j]=inf;            dis[1]=0;        for(j=1;j<=n;j++)  //j<=n和j<=n-1都是对的        {            flag=false;            for(k=1;k<=m;k++)            {                if(dis[p[k].s]>p[k].t+dis[p[k].e])                    {dis[p[k].s]=p[k].t+dis[p[k].e];flag=true;}            }            if(!flag) break;        }        flag=false;        for(k=1;k<=m;k++)        {            if(dis[p[k].s]>p[k].t+dis[p[k].e])                {flag=true; break;}        }        if(flag) printf("YES\n");        else printf("NO\n");    }    return 0;}


代码2(每个点都进行判断):

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;#define inf 0x7ffffff;struct paths{    int s,e,t;}p[5210];int n,m,w,dis[505];int main(){    int f,i,j,k,temp;    scanf("%d",&f);    while(f--)    {        scanf("%d%d%d",&n,&m,&w);        for(i=1;i<=m;i++)        {            scanf("%d%d%d",&p[i].s,&p[i].e,&p[i].t);            p[i+m].s=p[i].e; p[i+m].e=p[i].s; p[i+m].t=p[i].t;        }        k=2*m+w;        for(i=2*m+1;i<=k;i++)        {            scanf("%d%d%d",&p[i].s,&p[i].e,&temp);            p[i].t=-temp;        }        m=k;        bool flag=false;        for(i=1;i<=n;i++)        {            for(j=1;j<=n;j++)                dis[j]=inf;            dis[i]=0;            for(j=1;j<=n;j++)  //j<=n和j<=n-1都是对的            {                bool fff=false;                for(k=1;k<=m;k++)                {                    if(dis[p[k].s]>p[k].t+dis[p[k].e])                        {dis[p[k].s]=p[k].t+dis[p[k].e]; fff=true;}                }                if(!fff) break;  //没有这句会超时            }            for(k=1;k<=m;k++)            {                if(dis[p[k].s]>p[k].t+dis[p[k].e])                    {flag=true; break;}            }            if(flag) break;        }        if(flag) printf("YES\n");        else printf("NO\n");    }    return 0;}

一个不错的题解:http://www.cnblogs.com/jackge/archive/2013/04/23/3038494.html


原创粉丝点击