• hdu 三部曲1 Is the Information Reliable? 差分约束 bellman_ford算法


    Problem Description

    The galaxy war between the Empire Draco and the Commonwealth of Zibu broke out 3 years ago. Draco established a line of defense called Grot. Grot is a straight line with N defense stations. Because of the cooperation of the stations, Zibu’s Marine Glory cannot march any further but stay outside the line.

    A mystery Information Group X benefits form selling information to both sides of the war. Today you the administrator of Zibu’s Intelligence Department got a piece of information about Grot’s defense stations’ arrangement from Information Group X. Your task is to determine whether the information is reliable.

    The information consists of M tips. Each tip is either precise or vague.

    Precise tip is in the form of P A B X, means defense station A is X light-years north of defense station B.

    Vague tip is in the form of V A B, means defense station A is in the north of defense station B, at least 1 light-year, but the precise distance is unknown.

     
    Input

    There are several test cases in the input. Each test case starts with two integers N (0 < N ≤ 1000) and M (1 ≤ M ≤ 100000).The next M line each describe a tip, either in precise form or vague form.

     
    Output

    Output one line for each test case in the input. Output “Reliable” if It is possible to arrange N defense stations satisfying all the M tips, otherwise output “Unreliable”.

     
    Sample Input
    3 4 P 1 2 1 P 2 3 1 V 1 3 P 1 3 1 5 5 V 1 2 V 2 3 V 3 4 V 4 5 V 3 5
     
    Sample Output
    Unreliable
    Reliable
    ***************************************************************************************************************************
    差分约束问题  建图很重要。
    ***************************************************************************************************************************
     1 #include<iostream>
     2 #include<string>
     3 #include<cstring>
     4 #include<cstdio>
     5 #include<cctype>
     6 #include<queue>
     7 #include<stack>
     8 using namespace std;
     9 struct edge
    10 {
    11     int v,u,len;
    12 }e[200011];
    13 bool change;
    14 int dis[1021];
    15 int e_num,n,m;
    16 char c;
    17 int x,y,w;
    18 void add(int x,int y,int len)
    19 {
    20     e[e_num].v=x;
    21     e[e_num].u=y;
    22     e[e_num].len=len;
    23     e_num++;
    24 }
    25 bool bellman_ford()
    26 {
    27     for(int i=0;i<n;i++)
    28     {
    29         change=false;
    30        for(int j=0;j<e_num;j++)
    31         {
    32           if(dis[e[j].u]>dis[e[j].v]+e[j].len)
    33           {
    34               dis[e[j].u]=dis[e[j].v]+e[j].len;
    35               change=true;
    36           }
    37         }
    38     }
    39     return !change;
    40 }
    41 int main()
    42 {
    43     while(scanf("%d %d",&n,&m)==2)
    44     {
    45         int i,j,k;
    46         e_num=0;
    47         memset(dis,0,sizeof(dis));
    48         for(i=0;i<m;i++)
    49         {
    50           scanf("
    %c %d %d",&c,&x,&y);
    51           //满足x-y>=w&&y-x<=-w
    52           if(c=='P')
    53            {
    54             scanf("%d",&w);
    55             add(y,x,w);
    56             add(x,y,-w);
    57            }
    58           else
    59           {
    60             //满足x-y<=-1
    61             add(x,y,-1);
    62           }
    63         }
    64     if(bellman_ford())//如果存在负环,则互相矛盾,不可信
    65       printf("Reliable
    ");
    66      else
    67       printf("Unreliable
    ");
    68   }
    69      return 0;
    70 }
    View Code

    重在找关系(约束条件),建图

  • 相关阅读:
    分治法的经典问题——大整数相乘
    分治法的经典问题——大整数相乘
    L2-013. 红色警报(dfs)
    L2-013. 红色警报(dfs)
    L2-012. 关于堆的判断
    L2-012. 关于堆的判断
    二进制和位运算中的异或
    2015年天勤考研机试模拟赛 A 推断三角形
    怎样利用kettle官方社区查找关于carte服务的设置
    openCV中 libopencv-nonfree-dev的安装: undefined reference to `cv::initModule_nonfree()&#39;
  • 原文地址:https://www.cnblogs.com/sdau--codeants/p/3352266.html
Copyright © 2020-2023  润新知