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.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
現在假設有一個這樣的序列,S={a1,a2,a3,a4...ai...at}
其中ai=a*si,其實這句可以忽略不看
現在給出一個不等式,使得ai+a(i+1)+a(i+2)+...+a(i+n)
首先給出兩個數分別代表S序列有多少個,有多少個不等式
不等式可以這樣描述
給出四個參數第一個數i可以代表序列的第幾項,然後給出n,這樣前面兩個數就可以描述為ai+a(i+1)+...a(i+n),即從i到n的連續和,再給出一個符號和一個ki
當符號為gt代表‘>’,符號為lt代表‘<'
那麼樣例可以表示
1 2 gt 0
a1+a2+a3>0
2 2 lt 2
a2+a3+a4<2
最後問你所有不等式是否都滿足條件,若滿足輸出lamentable kingdom,不滿足輸出successful conspiracy,這裡要注意了,不要搞反了
解題思路:一個典型的差分約束,很容易推出約束不等式
首先設Si=a1+a2+a3+...+ai
那麼根據樣例可以得出
S3-S0>0---->S0-S3<=-1
S4-S1<2---->S4-S1<=1
因為差分約束的條件是小於等於,所以我們將ki-1可以得到一個等於號
那麼通式可以表示為
a b gt c
S[a-1]-s[a+b]<=-ki-1
a b lt c
S[a+b]-S[a-1]<=ki-1
那麼根據差分約束建圖,加入這些有向邊
gt: =-ki-1
lt: =ki-1
再根據bellman_ford判斷是否有無負環即可
若出現負環了則這個序列不滿足所有的不等式
//212K 0MS #include#include #include using namespace std; #define maxx 105 struct edge { int u,v,w; }edge[maxx]; int n,m; int dist[maxx]; bool bellman_ford() { memset(dist,0,sizeof(dist)); for(int i=0;i >n,n) { cin>>m; for(int i=0;i