poj 2983 Is the Information Reliable?

来源:互联网 发布:育才行知小学对口初中 编辑:程序博客网 时间:2024/05/21 07:54

这题参考了别人的思路:

由于P  A  B  X 指“确定AB的距离(边权)为X

P  A  B  X得到的差分系统为

dist[A] - dist[B] >= X  &&  dist[A] - dist[B] <= X

等价于
dist[B] <= dist[A] - X  &&  dist[A] <= dist[B] + X

 if(dist[B] > dist[A]-X) 松弛:dist[B] = dist[A]-X


由于 V  A  B指“只知道AB的距离(边权)至少为1

V  A  B得到的差分系统为
dist[A] >= dist[B] +1

等价于
dist[B] <= dist[A] -1 
if(dist[B] > dist[A] -1) 松弛:dist[B] = dist[A] -1

#include <iostream>using namespace std; const int inf=1000000000;class{public:    int s,e;}edge[200001];int N; int M;int dist[1001]; int w[200001];  int main(int i,int j){    while(cin>>N>>M)    {        memset(dist,0,sizeof(dist));        int pe=0;        for(i=0;i<M;i++)        {            char pv;            int a,b,x;            getchar();            scanf("%c",&pv);if(pv=='P')            {                scanf("%d%d%d",&a,&b,&x);                edge[pe].s=a;                edge[pe].e=b;                w[pe++]=x;                edge[pe].s=b;                edge[pe].e=a;                w[pe++]=-x;            }            else if(pv=='V')             {                scanf("%d%d",&a,&b);                edge[pe].s=a;                edge[pe].e=b;                w[pe++]=1;            }        }        bool sign;         for(j=0;j<N;j++)        {            sign=false;            for(i=0;i<pe;i++)                if(dist[ edge[i].e ] > dist[ edge[i].s ] - w[i])                {                    dist[ edge[i].e ] = dist[ edge[i].s ] - w[i];                    sign=true;                }              if(!sign)                break;        }        if(sign)cout<<"Unreliable"<<endl; //存在负权环        else            cout<<"Reliable"<<endl;   //不存在负权环     }     return 0; }


原创粉丝点击