poj 3259 Wormholes 最短路

简介:

     bellman判环即可


/*
author:jxy
lang:C/C++
university:China,Xidian University
**If you need to reprint,please indicate the source**
Build Time:2013-05-07-22.01

*/
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <queue>
#define INF 1E9
using namespace std;
int n,m,w;
int first[505];
int U[7000];
int next[7000];
int edge[7000];
int cnt;

void build(int v,int u,int s)
{
    next[cnt]=first[v];
    first[v]=cnt;
    edge[cnt]=s;
    U[cnt]=u;
    cnt++;
}

queue<int> q;
int dis[505];
int in[505];
int bellman()
{
    bool inq[505];
    memset(inq,0,sizeof(inq));
    memset(dis,127,sizeof(dis));
    memset(in,0,sizeof(in));
    while(!q.empty())q.pop();
    q.push(1);
    in[1]=1;
    dis[1]=0;
    int v,u,i,t;
    while(!q.empty())
    {
        v=q.front();q.pop();
        inq[v]=0;
        for(i=first[v];i!=-1;i=next[i])
        {
            u=U[i];
            if(dis[u]>(t=dis[v]+edge[i]))
            {
                dis[u]=t;
                if(inq[u])continue;
                inq[u]=1;
                in[u]++;
                if(in[u]>n)return 1;
                q.push(u);
            }
        }
    }
    return 0;
}



int main()
{
    int T;
    scanf("%d",&T);
    while(T--)
    {
        memset(first,-1,sizeof(first));
        cnt=0;
        scanf("%d%d%d",&n,&m,&w);
        int i,u,v,s;
        for(i=0;i<m;i++)
        {
            scanf("%d%d%d",&v,&u,&s);
            build(v,u,s);
            build(u,v,s);
        }
        for(i=0;i<w;i++)
        {
            scanf("%d%d%d",&v,&u,&s);
            build(v,u,-s);
        }

        if(bellman())puts("YES");
        else puts("NO");
    }
}


jxy
+关注
目录
打赏
0
0
0
0
2
分享
相关文章
POJ-2253,Frogger(最短路问题)
POJ-2253,Frogger(最短路问题)
POJ-1611,The Suspects(并查集
POJ-1611,The Suspects(并查集
POJ 2370 Democracy in danger(简单贪心)
Democracy in danger Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 3388   Accepted: 2508 Description In one of the...
974 0
poj supermaket (贪心)
http://poj.org/problem?id=1456 #include #include #include using namespace std; struct nod { int a; int d; }; bool cmp(nod x,nod y) { return x.
713 0
并查集-poj-1182
poj-1182-食物链 //2014.4.11 HDOJ携程编程大赛预赛第二场第一题 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形。A吃B, B吃C,C吃A。  现有N个动物,以1-N编号。每个动物都是A,B,C中的一种,但是我们并不知道它到底是哪一种。  有人用两种说法对这N个动物所构成的食物链关系进行描述:  第一种说法是"1
1057 0