差分约束系统 ZQUOJ 21452&&POJ 1364 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 file 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 file contains the lines corresponding to the blocks in the input file. 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 file corresponding to the last "null" block of the input file.
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
有关差分约束系统的知识请参考:http://www.cppblog.com/abilitytao/archive/2011/10/13/94061.html
分析:对题目中给定的si,ni,ki,和一个给定的序列S[1....N],如果(si,ni,gt,ki),意思就是存在约束条件S[si]+S[si+1]+...S[si+ni] > ki,如果(si,ni,lt,ki),意思就是存在约束条件S[si]+S[si+1]+...S[si+ni] < ki,判断所给的约束条件有无解,有解就输出lamentable kingdom,无解就输出successful conspiracy。
为了将这些约束条件转化为差分约束,不妨设T[x] = S[1]+S[2]+....S[x];
那么上面两条式子就可以转化为 T[si+ni] - T[si-1] > ki
T[si+ni] - T[si-1] < ki
又差分约束系统的求解只能针对>=或<=,无法求解>的系统,还好ki是整数,可以很容易将<转化为<=
T[si-1] - T[si+ni] <= -ki - 1
T[si+ni] - T[si-1] <= ki - 1
剩下的就是普通的差分约束系统的求解了。
建图用SPFA求解以s为源点的单元最短路径,这里有个技巧,就是如果使用SPFA的时候不想添加附加节点s的话,可以再初始化的时候把所有的点都加入队列中,初始化dist数组全为0,相当与源点的边权值,其实就是相当于源点s入队,开始算法后s出队更新得到的队列,因为没有边指向s,所以后面的更新不会涉及s,即在后面的计算中都不会用到s。
AC代码:
1 #include<stdio.h> 2 #include<string.h> 3 typedef struct 4 { 5 int v,w,next; 6 }Node; 7 Node e[105]; 8 int n,m,g; 9 int first[105],dist[105],cnt[105],vis[105]; 10 void add(int u,int v,int w) 11 { 12 e[g].v=v; 13 e[g].w=w; 14 e[g].next=first[u]; 15 first[u]=g++; 16 } 17 int SPFA() 18 { 19 int i,j,front,rear,top,queue[5000]; 20 memset(vis,0,sizeof(vis)); 21 memset(cnt,0,sizeof(cnt)); 22 memset(dist,0,sizeof(dist)); 23 front=rear=0; 24 for(i=1;i<=n;i++) 25 { 26 queue[rear++]=i; 27 vis[i]=1; 28 } 29 while(front<rear) 30 { 31 top=queue[front++]; 32 vis[top]=0; 33 cnt[top]++; 34 if(cnt[top]>n) 35 return 0; 36 for(i=first[top];i!=-1;i=e[i].next) 37 { 38 j=e[i].v; 39 if(dist[j]>dist[top]+e[i].w) 40 { 41 dist[j]=dist[top]+e[i].w; 42 if(!vis[j]) 43 { 44 vis[j]=1; 45 queue[rear++]=j; 46 } 47 } 48 } 49 } 50 return 1; 51 } 52 int main() 53 { 54 int i,ni,si,ki; 55 char oi[5]; 56 while(scanf("%d",&n)&&n) 57 { 58 scanf("%d",&m); 59 g=0; 60 memset(first,-1,sizeof(first)); 61 for(i=1;i<=m;i++) 62 { 63 scanf("%d%d%s%d",&si,&ni,oi,&ki); 64 if(oi[0]=='g') 65 add(ni+si,si-1,-ki-1); 66 else 67 add(si-1,ni+si,ki-1); 68 } 69 if(SPFA()) 70 printf("lamentable kingdom\n"); 71 else 72 printf("successful conspiracy\n"); 73 } 74 return 0; 75 }
posted on 2012-08-17 00:51 jumpingfrog0 阅读(259) 评论(0) 编辑 收藏 举报