zoukankan      html  css  js  c++  java
  • 【HDOJ1531】【差分约束+添加超级源点】

    http://acm.hdu.edu.cn/showproblem.php?pid=1531

    King

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
    Total Submission(s): 1992    Accepted Submission(s): 928

    Problem Description
    Once, in one kingdom, there was a queen and that queen was expecting a baby. The queen prayed: ``If my child was a son and if only he was a sound king.'' After nine months her child was born, and indeed, she gave birth to a nice son. 
    Unfortunately, as it used to happen in royal families, the son was a little retarded. After many years of study he was able just to add integer numbers and to compare whether the result is greater or less than a given integer number. In addition, the numbers had to be written in a sequence and he was able to sum just continuous subsequences of the sequence. 

    The old king was very unhappy of his son. But he was ready to make everything to enable his son to govern the kingdom after his death. With regards to his son's skills he decided that every problem the king had to decide about had to be presented in a form of a finite sequence of integer numbers and the decision about it would be done by stating an integer constraint (i.e. an upper or lower limit) for the sum of that sequence. In this way there was at least some hope that his son would be able to make some decisions. 

    After the old king died, the young king began to reign. But very soon, a lot of people became very unsatisfied with his decisions and decided to dethrone him. They tried to do it by proving that his decisions were wrong. 
    Therefore some conspirators presented to the young king a set of problems that he had to decide about. The set of problems was in the form of subsequences Si = {aSi, aSi+1, ..., aSi+ni} of a sequence S = {a1, a2, ..., an}. The king thought a minute and then decided, i.e. he set for the sum aSi + aSi+1 + ... + aSi+ni of each subsequence Si an integer constraint ki (i.e. aSi + aSi+1 + ... + aSi+ni < ki or aSi + aSi+1 + ... + aSi+ni > ki resp.) and declared these constraints as his decisions. 
    After a while he realized that some of his decisions were wrong. He could not revoke the declared constraints but trying to save himself he decided to fake the sequence that he was given. He ordered to his advisors to find such a sequence S that would satisfy the constraints he set. Help the advisors of the king and write a program that decides whether such a sequence exists or not.
     
    Input
    The input consists of blocks of lines. Each block except the last corresponds to one set of problems and king's decisions about them. In the first line of the block there are integers n, and m where 0 < n <= 100 is length of the sequence S and 0 < m <= 100 is the number of subsequences Si. Next m lines contain particular decisions coded in the form of quadruples si, ni, oi, ki, where oi represents operator > (coded as gt) or operator < (coded as lt) respectively. The symbols si, ni and ki have the meaning described above. The last block consists of just one line containing 0.
     
    Output
    The output contains the lines corresponding to the blocks in the input. A line contains text successful conspiracy when such a sequence does not exist. Otherwise it contains text lamentable kingdom. There is no line in the output corresponding to the last ``null'' block of the input.
     
    Sample Input
    4 2 1 2 gt 0 2 2 lt 2 1 2 1 0 gt 0 1 0 lt 0 0
     
    Sample Output
    lamentable kingdom
    successful conspiracy
    题目大意:给了一些不等式,让验证是否存在一种情况使之成立。
    题目分析:典型的差分约束题,用SPFA判断构建好的图是否存在正环【如果跑最短路则看是否存在负环】。而不同于之前的差分约束,这些不等式构建出的图不一定连通,这时需要加一个超级源点【n+1】,并且该点到其余点【0~n】都要有一条权值为0的边,引入超级源点之后即和普通差分约束一样了,跑一下SPFA即可
     1 #include<iostream>
     2 #include<cstring>
     3 #include<cstdio>
     4 #include<queue>
     5 using namespace std;
     6 struct edge{
     7     int to;
     8     int next;
     9     int len;
    10 }qwq[225];
    11 queue<int>qaq;
    12 int edge_cnt,n,m,in[105],stk[105],dist[105],head[105];
    13 void add(int x,int y,int z)
    14 {
    15     qwq[edge_cnt].len = z;
    16     qwq[edge_cnt].to = y;
    17     qwq[edge_cnt].next=head[x];
    18     head[x]=edge_cnt++;
    19 }
    20 bool spfa()
    21 {
    22     while(!qaq.empty())
    23     {
    24         qaq.pop();
    25     }    
    26     dist[n+1]=0;
    27     qaq.push(n+1);
    28     stk[n+1]=1;
    29     in[n+1]++;
    30     while(!qaq.empty())
    31     {
    32         int orz=qaq.front();qaq.pop();
    33         stk[orz]=0;
    34         for(int i = head[orz] ; i != -1 ; i=qwq[i].next)
    35         {
    36             int v=qwq[i].to;
    37             if(dist[v]<dist[orz]+qwq[i].len)
    38             {
    39                 dist[v]=dist[orz]+qwq[i].len;
    40                 if(!stk[v])
    41                 {
    42                     in[v]++;
    43                     qaq.push(v);
    44                     stk[v]=1;
    45                     if(in[v]>n+1)
    46                     return false;
    47                 }
    48             }
    49         }
    50     }
    51     return true;
    52 }
    53 int main()
    54 {
    55 //    int n,m;
    56     scanf("%d",&n);
    57     while(n)
    58     {
    59         edge_cnt = 0;
    60         memset(dist,-1,sizeof(dist));
    61         memset(head,-1,sizeof(head));
    62         memset(in,0,sizeof(in));
    63         memset(stk,0,sizeof(stk));
    64         edge_cnt=0;
    65         scanf("%d",&m);
    66         while(m--)
    67         {
    68             int a,b,c;
    69             char d,e;
    70             scanf("%d %d %c%c %d",&a,&b,&d,&e,&c);
    71             if(d=='g')
    72             {
    73                 add(a-1,a+b,c+1);
    74             }
    75             else
    76             {
    77                 add(a+b,a-1,-c+1);
    78             }
    79             
    80         }
    81         for(int i = 0 ; i <= n ; i++)
    82         {
    83             add(n+1,i,0);
    84         }
    85         if(spfa())printf("lamentable kingdom
    ");
    86         else printf("successful conspiracy
    ");
    87         scanf("%d",&n);
    88     }
    89     return 0;
    90  } 
  • 相关阅读:
    [LintCode 614.] 二叉树的最长连续子序列 II
    [LintCode 90.] k数和 II
    [LintCode 1674.] 倒可乐
    [LintCode 797.] 到达一个数字
    [LintCode 1691.] 买卖股票的最佳时机V
    [LintCode 69. 242.] 二叉树的层次遍历
    [LintCode 229.] 栈排序
    [LeetCode 1671.] 玩游戏
    [LintCode 1668.] 区间最小覆盖
    (十)线程同步
  • 原文地址:https://www.cnblogs.com/MekakuCityActor/p/9022871.html
Copyright © 2011-2022 走看看