Wormholes (poj 3259 SPFA || Bellman_Ford 判負環),wormholespoj

來源:互聯網
上載者:User

Wormholes (poj 3259 SPFA || Bellman_Ford 判負環),wormholespoj

Language:DefaultWormholes
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 33291   Accepted: 12130

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

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

Sample Output

NOYES

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

題意:John的農場裡field塊地,path條路串連兩塊地,hole個蟲洞,蟲洞是一條單向路,不但會把你傳送到目的地,而且時間會倒退Ts。我們的任務是知道會不會在從某塊地出發後又回來,看到了離開之前的自己。

很久之前做的最短路,今天又寫了一遍,複習了一下SPFA和Bellma_Ford,判斷負權值迴路。

代碼:

//Bellman_Ford#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 550#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b)  for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b)  for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v)   memset ((t) , v, sizeof(t))#define sf(n)       scanf("%d", &n)#define sff(a,b)    scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf          printf#define DBG         pf("Hi\n")typedef long long ll;using namespace std;struct Edge{    int u,v,w;}edge[maxn*10];int n,m,w,num;int dist[maxn];bool Bellman_Ford(){    int i,j;    FRL(i,0,n+2)        dist[i]=INF;    dist[1]=0;    FRL(i,1,n)    {        bool flag=false;        FRL(j,0,num)        {            int u=edge[j].u;            int v=edge[j].v;            if (dist[v]>dist[u]+edge[j].w)            {                flag=true;                dist[v]=dist[u]+edge[j].w;            }        }        if (!flag) return true;    }    FRL(i,0,num)        if (dist[ edge[i].v ]>dist[ edge[i].u ]+edge[i].w)            return false;    return true;}int main(){    int i,j,cas;    sf(cas);    while (cas--)    {        num=0;        int u,v,t;        sfff(n,m,w);        FRL(i,0,m)        {            sfff(u,v,t);            edge[num].u=u;            edge[num].v=v;            edge[num].w=t;            num++;            edge[num].u=v;            edge[num].v=u;            edge[num].w=t;            num++;        }        FRL(i,0,w)        {            sfff(u,v,t);            edge[num].u=u;            edge[num].v=v;            edge[num].w=-t;            num++;        }        if (Bellman_Ford())            pf("NO\n");        else            pf("YES\n");    }    return 0;}


//SPFA#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#pragma comment (linker,"/STACK:102400000,102400000")#define maxn 550#define MAXN 2005#define mod 1000000009#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6#define lson rt<<1,l,mid#define rson rt<<1|1,mid+1,r#define FRE(i,a,b)  for(i = a; i <= b; i++)#define FREE(i,a,b) for(i = a; i >= b; i--)#define FRL(i,a,b)  for(i = a; i < b; i++)#define FRLL(i,a,b) for(i = a; i > b; i--)#define mem(t, v)   memset ((t) , v, sizeof(t))#define sf(n)       scanf("%d", &n)#define sff(a,b)    scanf("%d %d", &a, &b)#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)#define pf          printf#define DBG         pf("Hi\n")typedef long long ll;using namespace std;struct Edge{    int v,w;    int next;}edge[maxn*10];int n,m,w,num;int head[maxn];int dist[maxn];bool inq[maxn];int cnt[maxn];void init(){    num=0;    mem(head,-1);    mem(inq,false);    mem(cnt,0);    mem(dist,INF);}void addedge(int u,int v,int w){    edge[num].v=v;    edge[num].w=w;    edge[num].next=head[u];    head[u]=num++;}bool SPFA(){    queue<int>Q;    inq[1]=true;    cnt[1]=1;    dist[1]=0;    while (!Q.empty()) Q.pop();    Q.push(1);    while (!Q.empty())    {        int u=Q.front(); Q.pop();        inq[u]=false;        if (cnt[u]>n)            return false;        for (int i=head[u];i!=-1;i=edge[i].next)        {            int v=edge[i].v;            if (dist[v]>dist[u]+edge[i].w)            {                dist[v]=dist[u]+edge[i].w;                if (!inq[v])                {                    inq[v]=true;                    cnt[v]++;                    Q.push(v);                }            }        }    }    return true;}int main(){    int i,j,cas;    sf(cas);    while (cas--)    {        int u,v,t;        init();        sfff(n,m,w);        FRL(i,0,m)        {            sfff(u,v,t);            addedge(u,v,t);            addedge(v,u,t);        }        FRL(i,0,w)        {            sfff(u,v,t);            addedge(u,v,-t);        }        if (SPFA())            pf("NO\n");        else            pf("YES\n");    }    return 0;}



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.