poj1364 King

地址:http://poj.org/problem?id=1364

题目:

King

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 13466   Accepted: 4793

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

Central Europe 1997

思路:

  从前缀和的角度考虑,每个给出的条件都是一个不等式。

  如: 1 2 gt 0 => s3-s1>0 => s3-s1>=1

    2 2 lt 2 => s4-s2<2 => s4-s2<=1

  这样问题变成了判断这个不等式组是否成立的问题了,也是差分约束问题。

  跑个 最长路,看有没有环就好了。为了防止图不连通,增加一个超级原点,从原点到其他点建一条权值为0的边。

  对了,别用vector存边,会T。

 1 #include <cstdio>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <queue>
 5 using namespace std;
 6
 7 #define MP make_pair
 8 #define PB push_back
 9 typedef long long LL;
10 typedef pair<int,int> PII;
11 const double eps=1e-8;
12 const double pi=acos(-1.0);
13 const int K=1e6+7;
14 const int mod=1e9+7;
15
16 struct node
17 {
18     int to,v,next;
19 }edge[K];
20 int tot,head[K];
21 void add(int x,int y,int z)
22 {
23     edge[tot].to=y,edge[tot].v=z,edge[tot].next=head[x];
24     head[x]=tot++;
25 }
26 int n,m,vis[K],dis[K],cnt[K];
27 int spfa(int st)
28 {
29     queue<int>q;
30     q.push(st),vis[st]=1,dis[st]=1,cnt[st]=1;
31     while(q.size())
32     {
33         int u=q.front();
34         vis[u]=0;q.pop();
35         for(int i=head[u];~i;i=edge[i].next)
36         {
37             int v=edge[i].to,w=edge[i].v;
38             if(dis[v]<dis[u]+w)
39             {
40                 dis[v]=dis[u]+w;
41                 if(vis[v])  continue;
42                 if(cnt[v]>n) return 1;
43                 q.push(v),vis[v]=1,cnt[v]++;
44             }
45         }
46     }
47     return 0;
48 }
49 int main(void)
50 {
51     char ss[20];
52     while(~scanf("%d%d",&n,&m)&&n)
53     {
54         tot=0;
55         for(int i=0;i<=n+1;i++)
56             head[i]=-1,vis[i]=0,dis[i]=0,cnt[i]=0;
57         for(int i=1,u,v,w;i<=m;i++)
58         {
59             scanf("%d%d%s%d",&u,&v,ss,&w);
60             if(ss[0]==‘g‘)  add(u-1,u+v,w+1);
61             else add(u+v,u-1,1-w);
62         }
63         for(int i=0;i<=n;i++)
64             add(n+1,i,0);
65         if(spfa(n+1))
66             printf("successful conspiracy\n");
67         else
68             printf("lamentable kingdom\n");
69     }
70     return 0;
71 }
时间: 2024-10-10 20:18:24

poj1364 King的相关文章

POJ1364 King 【差分约束】

King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 9977   Accepted: 3711 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

poj1364 King --- 差分约束

这是我见过最扯淡的题面之一. 题读了差不多一半我都觉得我这题肯定读不懂了,到最后终于看到重点了靠! 就是个差分约束大水题!毫无新意! 扯些什么皇后想生孩子!生了男孩是个弱智!父王很担心!这些有的没的有意思吗!! 题目就是给一个序列,告诉你 a b gt/lt c 表示从a起的b+1个数之和大于/小于c 就根据这个列不等式,要把> 或 <关系换成>= <= 就减一就可以了 列出不等式: S[a-1]-S[a+b]<=-c-1 S[a+b]-S[a-1]<=c-1 需要注意

poj图论解题报告索引

最短路径: poj1125 - Stockbroker Grapevine(多源最短路径,floyd) poj1502 - MPI Maelstrom(单源最短路径,dijkstra,bellman-ford,spfa) poj1511 - Invitation Cards(单源来回最短路径,spfa邻接表) poj1797 - Heavy Transportation(最大边,最短路变形,dijkstra,spfa,bellman-ford) poj2240 - Arbitrage(汇率问题,

差分约束系统(转)

1.问题定义 差分约束系统属于线性规划问题.在一个差分约束系统中,线性规划矩阵A的每一行包含一个1和一个-1,A的所有其他元素都为0.因此,由Ax≤b给出的约束条件是m个差分约束集合,其中包含n个未知元.每个约束条件为如下形式的简单线性不等式:xj-xi≤bk(1≤i, j≤n,1≤k≤m).如下图5维向量x满足8个不等式的差分约束,我们可以把未知量x1,x2,x3,x4,x5化为如下8个不等式: 我们可以发现当x=(x1,x2,…,xn)是差分约束系统Ax≤b的一个解,d为任意常数.则x+d=

POJ1364 HDU1531 King【SPFA】【差分约束】

King Time Limit: 1000MSMemory Limit: 10000K Total Submissions: 10528Accepted: 3872 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.'' A

【差分约束】POJ1364/LG UVA515 king

这道题是个差分约束 King Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14901 Accepted: 5248 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 sou

POJ2728 Desert King

Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to

hdu 5640 King&#39;s Cake(模拟)

Problem Description It is the king's birthday before the military parade . The ministers prepared a rectangle cake of size n×m(1≤n,m≤10000) . The king plans to cut the cake himself. But he has a strange habit of cutting cakes. Each time, he will cut

[SCOI2005]互不侵犯King

1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec  Memory Limit: 162 MB Submit: 4255  Solved: 2458 [Submit][Status][Discuss] Description 在N×N的棋盘里面放K个国王,使他们互不攻击,共有多少种摆放方案.国王能攻击到它上下左右,以及左上 左下右上右下八个方向上附近的各一个格子,共8个格子. Input 只有一行,包含两个数N,K ( 1 <=N <=9, 0 <=