poj 2983 Is the Information Reliable

http://poj.org/problem?id=2983

假设以最北为超级源点

对于 P north south x 代表south 在 north 的南边 x 光年处

则 north 在south 南边的 -x 光年处

如果矛盾的话 则及存在负环 也存在正环 我们把-x 这种情况也保存 是为了下面

V north south 它是至少 1 光年 如果它使问题矛盾的话 则可利用上面保存的 -x 的情况

让我们发现正环

所以综合以上 :P 情况 x 和-x 情况都保存,v 只保存 x=1

然后用Bellman-Ford 查找是否有正环即可

代码如下

#include<iostream>

#include<cmath>

#include<string>

#include<algorithm>

#include<queue>

#include<cstring>

#include<cstdio>



using namespace std;

const int N=1005;

const int M=100005;

struct node

{

    int north,south,x;

}mem[M*2];

int dis[N];

int main()

{

   int n,m;

   while(scanf("%d %d",&n,&m)!=EOF)

   {

       char ctemp;

       int I=0;

       for(int i=0;i<m;++i)

       {

           getchar();

           scanf("%c",&ctemp);

           if(ctemp=='P')

           {

               scanf("%d %d %d",&mem[I].north,&mem[I].south,&mem[I].x);++I;

               mem[I].south=mem[I-1].north;mem[I].north=mem[I-1].south;mem[I].x=-mem[I-1].x;++I;

           }

           else

           {

               scanf("%d %d",&mem[I].north,&mem[I].south); mem[I].x=1;++I;

           }

       }

       memset(dis,0,sizeof(dis));

       bool OK=false;

       for(int w=0;w<n;++w)

       {

           OK=true;

           for(int i=0;i<I;++i)

           {

               if(dis[mem[i].south]<dis[mem[i].north]+mem[i].x)

               {

                   OK=false;dis[mem[i].south]=dis[mem[i].north]+mem[i].x;

               }

           }



           if(OK==true)

           break;

       }

       if(OK==true)

       printf("Reliable\n");

       else

       printf("Unreliable\n");

   }

   return 0;

}

 

你可能感兴趣的:(format)