zoukankan      html  css  js  c++  java
  • poj 2983 Is the Information Reliable? (差分约束)

      1  http://poj.org/problem?id=2983
      2  判断是否存在负权回路,差分约束条件 有
      3   A 到B的距离一定是 X
      4   所以要 表示出来就是 insert(A,B,-x);
      5     insert(B,A,x); 
      6 #include<stdio.h>
      7 #include<vector>
      8 #include<iostream>
      9 #include<queue>
     10 #include<string.h>
     11 using namespace std;
     12 const int N=2000;
     13 int n;
     14 struct node
     15 {
     16 
     17     int y;
     18     int w;
     19 };
     20 vector<node>p[N];
     21 queue<int>Q;
     22 int vis[N],dis[N],time[N];
     23 void init()
     24 {
     25     memset(vis,0,sizeof(vis));
     26     memset(time,0,sizeof(time));
     27 
     28     for(int i=0;i<=n;i++){dis[i]=999999;}
     29     dis[0]=0;
     30     vis[0]=1;
     31     time[0]=1;
     32 }
     33 void insert(int x,int y,int z)
     34 {
     35     node q;
     36     q.y=y;
     37     q.w=z;
     38     p[x].push_back(q);
     39 }
     40 int  spfa(int x)
     41 {
     42     init();
     43     while(!Q.empty())Q.pop();
     44     Q.push(0);
     45     while(!Q.empty())
     46     {
     47 
     48         int k=Q.front();
     49         Q.pop();
     50         vis[k]=0;//出来之后还可能再进
     51 
     52         for(int i=0;i<p[k].size();i++)
     53         {
     54              int   a=p[k][i].y;
     55              int len=p[k][i].w;
     56             if(dis[a]>dis[k]+len)
     57             {
     58 
     59                 dis[a]=dis[k]+len;
     60 
     61                if(!vis[a])
     62                {
     63                    Q.push(a);
     64                    vis[a]=1;
     65                    time[a]++;
     66                    if(time[a]>n)return 0;
     67                }
     68             }
     69         }
     70     }
     71     return 1;
     72 
     73 
     74 }
     75 int main()
     76 {
     77     int i,x,y,z,m;
     78     char str[3];
     79     while(~scanf("%d%d",&n,&m))
     80     {
     81         getchar();
     82         for(i=0;i<=N;i++)p[i].clear();
     83 
     84         for(i=0;i<m;i++)
     85         {
     86             scanf("%s",str);
     87             if(str[0]=='P')
     88             {
     89                  scanf("%d%d%d",&x,&y,&z);
     90                  insert(x,y,-z);
     91                  insert(y,x,z);
     92             }
     93             else
     94             {
     95                 scanf("%d%d",&x,&y);
     96                  insert(x,y,-1);
     97             }
     98 
     99 
    100         }
    101         for(i=1;i<=n;i++)
    102         {
    103             insert(0,i,0);
    104         }
    105         int f;
    106         f=spfa(0);
    107 
    108 
    109 
    110        if(f)printf("Reliable\n");
    111        else printf("Unreliable\n");
    112 
    113     }
    114 }
  • 相关阅读:
    手动访问和传参
    子路由
    matlab 不同尺度的矩阵存储
    贝叶斯决策
    vim的使用
    linux另一种安装方式
    Linux中profile、bashrc、bash_profile之间的区别和联系
    emacs编辑器的使用
    关于鼠标不敏感导致自以为ubuntu很怪的问题
    各种可以远程
  • 原文地址:https://www.cnblogs.com/acSzz/p/2470517.html
Copyright © 2011-2022 走看看