poj 2983 差分约束

来源:互联网 发布:php 整数保留2位小数 编辑:程序博客网 时间:2024/05/16 16:17
#include<iostream>
#include<cmath>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<map>
#define PI acos(-1.0)
typedef long long LL;
const int MAX=0xfffffff;
using namespace std;
const int mx=200020;


int n,m;
int cnt;
class node {
public:
    int u,v,w;
}edge[mx];
int dist[1010];
void bellman_ford( )
{
    for(int i=0;i<=n;i++)  dist[i]=MAX;
    bool ok;
    for(int i=1;i<n;i++)
    {
        ok=0;
        for(int j=0;j<cnt;j++)
            if(dist[edge[j].v]>dist[edge[j].u]-edge[j].w)
            {
                dist[edge[j].v]=dist[edge[j].u]-edge[j].w;
                ok=1;
            }
        //for(int j=0;j<cnt;j++)                      //不知道为什么自己加上这个条件就WA
        //    if(dist[edge[j].v]>dist[edge[j].u]-1)
        //    {
        //        dist[edge[j].v]=dist[edge[j].u]-1;
        //        ok=1;
        //    }
        if(!ok)  break;
    }
    if(ok)  printf("Unreliable\n");
    else printf("Reliable\n");
}
int main( )
{
    freopen("1.txt","r",stdin);
    while(scanf("%d %d",&n,&m)!=EOF)
    {
        cnt=0;
        while(m--)
        {
            char s[2];
            scanf("%s",s);
            int a,b,x;
            if(s[0]=='P')
            {
                scanf("%d %d %d",&a,&b,&x);
                edge[cnt].u=a,edge[cnt].v=b,edge[cnt++].w=x;
                edge[cnt].u=b,edge[cnt].v=a,edge[cnt++].w=-x;
            }
            else if(s[0]=='V')
            {
                scanf("%d %d",&a,&b);
                edge[cnt].u=a,edge[cnt].v=b,edge[cnt++].w=1;
            }
        }
        bellman_ford( );


    }
    return 0;
}
0 0