【差分约束】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 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

T

这题至今我仍是玄学状态,用栈写的spfa会wa,改成队列就A了,然后呢洛谷上不知道怎么了就是wa,等待大佬指点ing

这道题和其他的差分约束区别就是这道题是建完图判断负环

思路还是很简单的就不赘述了;

代码在此

 1 #include<cstdio>
 2 #include<queue>
 3 #include<cstring>
 4 #define N 110
 5 using namespace std;
 6 struct star{int to,nxt,val;}edge[10*N];
 7 int dis[N],head[N],time[N];
 8 int cnt=1,n,a,b,c,m;
 9 char s[3];
10 bool in[N];
11 inline void add(int u,int v,int w)
12 {
13     edge[cnt].nxt=head[u];
14     edge[cnt].to=v;
15     edge[cnt].val=w;
16     head[u]=cnt++;
17 }
18 int spfa(int u)
19 {
20     queue<int>q;
21     q.push(u);
22     dis[u]=0,in[u]=1,time[u]++;
23     while(!q.empty())
24     {
25         int now=q.front();q.pop();in[now]=0;
26         for(int i=head[now];i!=-1;i=edge[i].nxt)
27         {
28             int to=edge[i].to;
29             if(dis[to]<dis[now]+edge[i].val)
30             {
31                 dis[to]=dis[now]+edge[i].val;
32                 if(!in[to])
33                 {
34                     q.push(to);in[to]=1;
35                     if(++time[to]>n)return 0;
36                 }
37
38             }
39         }
40     }
41     return 1;
42 }
43 int main()
44 {
45     //freopen("tt.ak","r",stdin);
46 while(scanf("%d",&n) && n)
47 {
48     scanf("%d",&m);
49     memset(head,-1,sizeof(head));
50     memset(dis,~0x7f,sizeof(dis));
51     memset(time,0,sizeof(time));
52     memset(in,0,sizeof(in));
53     cnt=1;
54     for(int i=1;i<=m;i++)
55     {
56         scanf("%d%d%s%d",&a,&b,s,&c);
57         if(s[0]==‘g‘)
58             add(a-1,b+a,(c+1));
59         else
60             add(a+b,a-1,-c+1);
61      }
62      for(int i=0;i<=n;i++)add(n+1,i,0);
63      if(spfa(n+1))puts("lamentable kingdom");
64      else puts("successful conspiracy");
65 }
66     return 0;
67 }
68 /*
69 4 2
70 1 2 gt 0
71 2 2 lt 2
72 1 2
73 1 0 gt 0
74 1 0 lt 0
75 0
76 */

原文地址:https://www.cnblogs.com/Qin-Wei-Kai/p/10164038.html

时间: 2024-08-06 02:56:57

【差分约束】POJ1364/LG UVA515 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 1364 King --差分约束第一题

题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析:典型差分约束题,变换,令Ti = SUM(Xj) (0<=j<=i).  则表达式(1)可以看做T(a+b)-T(a-1) > k,也就是T(a-1)-T(a+b) < -k,又因为全是整数,所以T(a-1)-T(a+b) <= -k-1.  同理,(2)看做T(a+b)-T(

poj 1364 King(差分约束)(中等)

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

POJ 1364 King 差分约束 找负环

嘛,虽然是一道水题+模板题,不过还是学到了很多东西的,记录一下. 首先题目给出的不等式是小于,但是差分约束系统只能处理小于等于的情况,所以要转化成小于等于的进行处理.对于整数处理方法非常简单= = 然后是找负环的情况,其实不需要考虑图连不连通,只要一开始就把所有的点的d置成0,然后都push进队列里面就好了. PS:这种方法同样可以用在处理多源点最短路问题上. #include <cstdio> #include <cstring> #include <cmath> #

SGU 298 King Berl VI 差分约束 并使得min(dis[n]-dis[1])

题目链接:点击打开链接 题意: 给定n个点m条约束. 下面输出 u v x 表示: dis[u] - dis[v] >= x 然后建图就是 u->v 边权为-x 输出一个解满足 -10000<= dis[i] <= 10000. 若有多解输出一个 dis[n] - dis[1] 最小的解. 思路: 正图求个最大解,反图求个最小解. 对于一个点的 最大解<最小解,则差分约束无解. 题目要求dis[n]-dis[1]最小,那就令dis[n]为其最小解,dis[1]为其最大解,再s

【差分约束】POJ1201/LG SP116 Intervals

题意翻译 区间取数 题目描述 有n个区间,在区间[ai,bi]中至少取任意互不相同的ci个整数.求在满足n个区间的情况下,至少要取多少个正整数. 输入输出格式 输入格式 多组数据. 第一行的一个整数T表示数据个数.对于每组数据,第一行包含一个整数nn(11<=nn<=5000050000)表示区间数.以下nn行描述区间.输入的第(i+1)行包含三个整数ai,bi,ci,由空格分开.其中0<=ai<=bi<=50000,1<=ci<=bi-ai+1. 输出格式 对于

hdu 1364(差分约束)

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

【转载】差分约束

一直不知道差分约束是什么类型题目,最近在写最短路问题就顺带看了下,原来就是给出一些形如x-y<=b不等式的约束,问你是否满足有解的问题 好神奇的是这类问题竟然可以转换成图论里的最短路径问题,下面开始详细介绍下 比如给出三个不等式,b-a<=k1,c-b<=k2,c-a<=k3,求出c-a的最大值,我们可以把a,b,c转换成三个点,k1,k2,k3是边上的权,如图 由题我们可以得知,这个有向图中,由题b-a<=k1,c-b<=k2,得出c-a<=k1+k2,因此比较