zoukankan      html  css  js  c++  java
  • poj2983 Is the Information Reliable?

    也是典型的差分约束的题目。

    对于有提供两点间精确距离的情况要在两点间添加两条互相反向的边。

    用bellman-ford判断图中是否有负环即可。

    #include<iostream>
    #include<stdio.h>
    #include<string.h>
    #define MAXD   5000
    using namespace std;
    int N,M,u[MAXD*100],v[MAXD*100],w[MAXD*100],head[MAXD],next[MAXD*100],e,dis[MAXD];
    
    void add(int x,int y,int z)
    {
        u[e]=x;
        v[e]=y;
        w[e]=z;
        next[e]=head[x];
        head[x]=e++;
    }
    
    int bellman()
    {
       int k,i,j;
       for(k=1;k<=N;k++)
       {
           for(i=0;i<e;i++)
           {
               if(dis[v[i]]>dis[u[i]]+w[i])
               dis[v[i]]=dis[u[i]]+w[i];
           }
       }
       for(i=0;i<e;i++)
       {
            if(dis[v[i]]>dis[u[i]]+w[i])
            return 0;
       }
       return 1;
    }
    int main()
    {
        //freopen("test.txt","r",stdin);
        while(scanf("%d%d",&N,&M)!=EOF)
        {
            int i,j,a,b,x;
            char ch[2];
            e=0;
            memset(head,-1,sizeof(head));
            for(i=1;i<=M;i++)
            {
                scanf("%s",ch);
                if(ch[0]=='P')
                {
                    scanf("%d%d%d",&a,&b,&x);
                    add(b,a,x);
                    add(a,b,(-1)*x);
                }
                else
                {
                    scanf("%d%d",&a,&b);
                    add(a,b,-1);
                }
            }
            if(bellman())
            printf("Reliable\n");
            else
            printf("Unreliable\n");
        }
        return 0;
    }
  • 相关阅读:
    HDU 1348 Wall
    HDU 2202 最大三角形
    HDU 2215 Maple trees
    HDU 1147 Pick-up sticks
    HDU 1392 Surround the Trees
    风语时光
    HDU 1115 Lifting the Stone
    HDU 1086 You can Solve a Geometry Problem too
    HDU 2108 Shape of HDU
    HDU 3360 National Treasures
  • 原文地址:https://www.cnblogs.com/longlongagocsu/p/2868372.html
Copyright © 2011-2022 走看看