poj 2983 Is the Information Reliable? (Bellman_Ford)

参考:http://blog.csdn.net/lyy289065406/article/details/6648688

#include <iostream>
using namespace std;
int n,m;//n太空站树木;m为tip数 
const int INF=0x177777;
bool sign;
int pe;
int dist[1001];//源点到各点的距离
int w[200001];//边权 
 struct{
        int s,e;
        }node[200001];
bool Bellman_Ford(){
      for(int i=0;i<n;i++){
            sign=false;
            for(int j=0;j<pe;j++){
                  if(dist[node[j].e]>dist[node[j].s]-w[j]){
                      dist[node[j].e]=dist[node[j].s]-w[j];
                      sign=true;                                         
                  }
            }
            if(!sign)
               break;             
      }         
         return sign;    
}
int main(){
 while(cin>>n>>m){
 getchar();
 pe=0;
 int a,b,x;
 char pv;
 memset(dist,0,sizeof(dist)); 
  for(int i=0;i<m;i++){
      scanf("%c",&pv);
      if(pv=='P'){
           scanf("%d%d%d",&a,&b,&x);
           getchar();
           node[pe].s=a;
           node[pe].e=b;
           w[pe++]=x;
           node[pe].s=b;
           node[pe].e=a;
           w[pe++]=-x;
      }else if(pv=='V'){
            scanf("%d%d",&a,&b);
            getchar();
            node[pe].s=a;
            node[pe].e=b;
            w[pe++]=1;
      }
       
  }
 sign=Bellman_Ford();
  if(sign)  
            cout<<"Unreliable"<<endl; //存在负权环  
        else  
            cout<<"Reliable"<<endl;   //不存在负权环  
}
 return 0;
 system("pause");   
} 
    原文作者:Bellman - ford算法
    原文地址: https://blog.csdn.net/chen_zhipeng/article/details/8034472
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞