• POJ 1364 King


    King

    Time Limit: 1000ms
    Memory Limit: 10000KB
    This problem will be judged on PKU. Original ID: 1364
    64-bit integer IO format: %lld      Java class name: Main
     
    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

    Source

     
    解题:差分约束。
     
    设S = a1+a2+a3+...+an
     
    gt:S[a+b] - S[a-1] > C  ------> S[a+b] - S[a-1] >= C+1  -->  S[a+b] - (C+1) >= S[a-1]
     
    lt:S[a+b] - S[a-1] < C  -->  S[a+b] - S[a-1] <= C-1  -->  S[a-1] + C-1 >= S[a+b]
     
    建立超级源点n+1,n+1到其他的点的权值为0.进行spfa。
     
     1 #include <iostream>
     2 #include <cstdio>
     3 #include <cstring>
     4 #include <cmath>
     5 #include <algorithm>
     6 #include <climits>
     7 #include <vector>
     8 #include <queue>
     9 #include <cstdlib>
    10 #include <string>
    11 #include <set>
    12 #include <stack>
    13 #define LL long long
    14 #define pii pair<int,int>
    15 #define INF 0x3f3f3f3f
    16 using namespace std;
    17 struct arc{
    18     int to,w;
    19     arc(int x = 0,int y = 0){
    20         to = x;
    21         w = y;
    22     }
    23 };
    24 vector<arc>g[510];
    25 int n,m,d[510],cnt[500];
    26 bool in[510];
    27 queue<int>q;
    28 bool spfa(){
    29     for(int i = 0; i <= n; i++){
    30         d[i] = INF;
    31         cnt[i] = 0;
    32         in[i] = false;
    33         g[n+1].push_back(arc(i,0));
    34     }
    35     while(!q.empty()) q.pop();
    36     d[n+1] = 0;
    37     cnt[n+1] = 1;
    38     q.push(n+1);
    39     in[n+1] = true;
    40     while(!q.empty()){
    41         int u = q.front();
    42         q.pop();
    43         in[u] = false;
    44         for(int i = 0; i < g[u].size(); i++){
    45             if(d[g[u][i].to] > d[u]+g[u][i].w){
    46                 d[g[u][i].to] = d[u]+g[u][i].w;
    47                 if(!in[g[u][i].to]){
    48                     in[g[u][i].to] = true;
    49                     if(++cnt[g[u][i].to] > n) return false;
    50                     q.push(g[u][i].to);
    51                 }
    52             }
    53         }
    54     }
    55     return true;
    56 }
    57 int main() {
    58     int i,u,v,w;
    59     char str[5];
    60     while(scanf("%d",&n),n){
    61         scanf("%d",&m);
    62         for(i = 0; i < 500; i++) g[i].clear();
    63         for(i = 0; i < m; i++){
    64             scanf("%d %d %s %d",&u,&v,str,&w);
    65             if(str[0] == 'g')
    66                 g[u+v].push_back(arc(u-1,-w-1));
    67             else g[u-1].push_back(arc(u+v,w-1));
    68         }
    69         spfa()?puts("lamentable kingdom"):puts("successful conspiracy");
    70     }
    71     return 0;
    72 }
    View Code
  • 相关阅读:
    【Postgresql】set up
    【LSTM】Understanding-LSTMs
    【CTR】各公司方法
    【DL】stanford--cs20si--tensorflow
    Redis数据库入门教程
    用.htaccess文件实现URL重写
    php中urldecode()和urlencode()
    php中序列化与反序列化
    网站整合Ucenter详细流程
    ucenter 整合外部网站,实现登录等操作
  • 原文地址:https://www.cnblogs.com/crackpotisback/p/3959208.html
Copyright © 2020-2023  润新知