• POJ1364 King


     

    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

    Source

    正解:SPFA+差分约束系统

    解题报告:

      题目大意是给定一段区间的和小于或者大于某个值,然后问是否存在这种序列。

      考虑用点做差分约束的话感觉无从下手,于是我们可以想到用前缀和的形式,首末来加边。比如Sy-Sx-1<=z 则添加一条x-1到y的权值为z的边

      然后这道题比较水,我们只需要判断是否存在负权环就可以了。值得一提的是我们需要一开始就把所有结点加进队列,并且把所有的dis置为0就可以了。因为只要存在负权环就一定会不断入队,判断一下次数大于某个值就可以break了

     1 //It is made by jump~
     2 #include <iostream>
     3 #include <cstdlib>
     4 #include <cstring>
     5 #include <cstdio>
     6 #include <cmath>
     7 #include <algorithm>
     8 #include <ctime>
     9 #include <vector>
    10 #include <queue>
    11 #include <map>
    12 #ifdef WIN32   
    13 #define OT "%I64d"
    14 #else
    15 #define OT "%lld"
    16 #endif
    17 using namespace std;
    18 typedef long long LL;
    19 const int inf = (1<<30);
    20 const int MAXN = 10011;
    21 const int MAXM = 10011;
    22 int n,m;
    23 int dis[MAXN];
    24 int first[MAXN],to[MAXM],next[MAXM],w[MAXM];
    25 int ecnt;
    26 queue<int>Q;
    27 char ch[10];
    28 bool pd[MAXN];
    29 int cnt[MAXN];
    30 
    31 inline int getint()
    32 {
    33        int w=0,q=0;
    34        char c=getchar();
    35        while((c<'0' || c>'9') && c!='-') c=getchar();
    36        if (c=='-')  q=1, c=getchar();
    37        while (c>='0' && c<='9') w=w*10+c-'0', c=getchar();
    38        return q ? -w : w;
    39 }
    40 inline void Init(){
    41     memset(first,0,sizeof(first));
    42     ecnt=0;
    43     memset(pd,0,sizeof(pd));
    44     while(!Q.empty()) Q.pop();
    45     memset(cnt,0,sizeof(cnt));
    46 }
    47 
    48 inline bool spfa(){
    49     for(int i=0;i<=n;i++) Q.push(i),pd[i]=1;
    50     for(int i=0;i<=n;i++) dis[i]=0;
    51     while(!Q.empty()){
    52     int u=Q.front(); Q.pop(); pd[u]=0;
    53     for(int i=first[u];i;i=next[i]){
    54         int v=to[i];
    55         if(dis[v]>dis[u]+w[i]) {
    56         dis[v]=dis[u]+w[i];
    57         if(!pd[v]) {
    58             cnt[v]++;
    59             if(cnt[v]>=n) return false;
    60             Q.push(v); pd[v]=1;
    61         }
    62         }
    63     }
    64     }
    65     return true;
    66 }
    67 
    68 inline void solve(){
    69     while(true){
    70     n=getint(); 
    71     if(n==0) break;
    72     m=getint();
    73     Init();
    74     int x,y,z;
    75     for(int i=1;i<=m;i++) {
    76         x=getint();y=getint(); scanf("%s",ch); z=getint();
    77         if(ch[0]!='g') { next[++ecnt]=first[x-1];to[ecnt]=x+y;first[x-1]=ecnt;w[ecnt]=z-1; }
    78         else{  next[++ecnt]=first[x+y];to[ecnt]=x-1;first[x+y]=ecnt;w[ecnt]=-z-1; }
    79     }
    80     if(!spfa()) printf("successful conspiracy
    ");
    81     else printf("lamentable kingdom
    ");
    82     }
    83 }
    84 
    85 int main()
    86 {
    87   solve();
    88   return 0;
    89 }
  • 相关阅读:
    jquery 备忘笔记
    spring 集成 mybatis 后数据源初始化失败问题分析
    SpringMVC 资源国际化实现以及常见问题
    IE浏览器 get请求缓存问题
    Comparison method violates its general contract! 异常原因
    Security基础(六):部署Zabbix监控平台、配置及使用Zabbix监控系统、自定义Zabbix监控项目、实现Zabbix报警功能
    LVS负载均衡中arp_ignore和arp_annonuce参数配置
    iptables防火墙相关命令详解
    <三剑客> 老大:awk命令用法
    <三剑客> 老三:grep命令用法
  • 原文地址:https://www.cnblogs.com/ljh2000-jump/p/5547732.html
Copyright © 2020-2023  润新知