码迷,mamicode.com
首页 > 其他好文 > 详细

【差分约束】POJ1364/LG UVA515 king

时间:2018-12-23 15:34:41      阅读:111      评论:0      收藏:0      [点我收藏+]

标签:this   whether   reg   viso   NPU   sum   val   spec   red   

这道题是个差分约束

技术分享图片
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 sons 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 kings 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 */

 

【差分约束】POJ1364/LG UVA515 king

标签:this   whether   reg   viso   NPU   sum   val   spec   red   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!