标签:
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
Sample Output
lamentable kingdom
Source
Central Europe 1997
题目大意:一个国王通过一个序列来做决定,他有几段段连续子序列的和的条件,知道
各段子序列的和是大于还有小于某个数(这个数已知)。问:是否能找到这样的序列。
转换一下,就是N个数组成一个序列,已知M段几个连续的数组成的子序列构成的不等
式,求这几个不等式构成的不等式组是否有解。
例如:序列{A1,A2,A3,A4,A5,…,An},有M个不等式,比如说:
Ai + A(i+1) + A(i+2) + A(i+3) + A(i+4) + … + A(i+t) < Ki 或者是
Ai + A(i+1) + A(i+2) + A(i+3) + A(i+4) + … + A(i+t) > Ki
题目输入格式说明:
第1行:N M,表示N个数构成一个序列,有M段子段。
接下来1~M行:s n o k,s表示该子段的第一个数是序列的第几个数,即As;n表示从As
开始往后到A(s+n),即子段为As + A(s+1) + A(s+2) + … + A(s+n) ;o是一个字符串,
字符串为gt代表‘>‘,为lt代表‘<‘;最后k表示不等式右边的值。
数据举例说明:
1 2 gt 0 表示:a1 + a2 + a3 > 0
2 2 lt 2 表示: a2 + a3 + a4 < 2
lamentable kingdom最后如果所有不等式均满足条件,则输出"lamentable kingdom",不
满足则输出"successful conspiracy"。
思路:典型的差分约束系统。将所有的不等式转换一下。
设Si为序列前n项和。Si = A1 + A2 + A3 + … + Ai
那么 :
a b gt c 转换为: Sa - S(a+b) <= -c-1
a b lt c 转换为: S(a+b) - S(a-1) <= c-1
根据差分约束系统,加入这些有向边。之后用SPFA算法求最短路径,并判断有无负环出现。
若有负环出现,则不等式组不成立。若没有负环,则不等式组有解。
参考博文:http://blog.csdn.net/wangjian8006/article/details/7956848
#include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<queue> using namespace std; const int MAXM = 220; const int MAXN = 110; const int INF = 0xffffff0; struct EdgeNode { int to; int w; int next; }Edges[MAXM]; int Dist[MAXN],vis[MAXN],Head[MAXN],outque[MAXN]; bool SPFA(int s,int N) { for(int i = 0; i <= N+1; ++i) Dist[i] = INF; memset(vis,0,sizeof(vis)); memset(outque,0,sizeof(outque)); queue<int> Q; Q.push(s); vis[s] = 1; Dist[s] = 0; while( !Q.empty()) { int u = Q.front(); Q.pop(); outque[u]++; if(outque[u] > N) return false; vis[u] = 0; for(int i = Head[u]; i != -1; i = Edges[i].next) { int Temp = Dist[u] + Edges[i].w; if(Temp < Dist[Edges[i].to]) { Dist[Edges[i].to] = Temp; if( !vis[Edges[i].to]) { vis[Edges[i].to] = 1; Q.push(Edges[i].to); } } } } return true; } int main() { int N,M,a,b,c; char ch[4]; while(~scanf("%d",&N) && N) { scanf("%d",&M); memset(Head,-1,sizeof(Head)); for(int i = 0; i < M; ++i) { scanf("%d%d%s%d",&a,&b,ch,&c); if(ch[0] == 'g') { int u = a + b + 1; int v = a; int w = -c-1; Edges[i].to = v; Edges[i].w = w; Edges[i].next = Head[u]; Head[u] = i; } else { int u = a; int v = a + b + 1; int w = c - 1; Edges[i].to = v; Edges[i].w = w; Edges[i].next = Head[u]; Head[u] = i; } } for(int i = 1; i <= N+1; ++i) { int u = 0; int v = i; int w = 0; int k = M+i-1; Edges[k].to = v; Edges[k].w = w; Edges[k].next = Head[u]; Head[u] = k; } if(SPFA(0,N+1)) printf("lamentable kingdom\n"); else printf("successful conspiracy\n"); } return 0; }
POJ1364 HDU1531 King【SPFA】【差分约束】
标签:
原文地址:http://blog.csdn.net/lianai911/article/details/43161493