King - UVa 515 差分约束系统

76 篇文章 0 订阅

King

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 $S_i = \{a_{s_i}, a_{s_i+1}, \dots, a_{s_i+n_i}\}$ of a sequence $S = \{a_1, a_2, \dots, a_n\}$. The king thought a minute and then decided, i.e. he set for the sum $a_{s_i} + a_{s_i+1} + \dots + a_{s_i+n_i}$ of each subsequence Si an integer constraint ki (i.e. $a_{s_i} + a_{s_i+1} + \dots + a_{s_i+n_i} < k_i$ or $a_{s_i} + a_{s_i+1} + \dots + a_{s_i+n_i} > k_i$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 \leŸ 100$  is length of the sequence  S  and  $0 < m \leŸ 100$  is the number of subsequences  S i . Next  m  lines contain particular decisions coded in the form of quadruples  s i n i o i k i , where  o i  represents operator > (coded as  gt ) or operator < (coded as  lt ) respectively. The symbols  s i n i  and  k i  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

题意:需要你自己去构造一个序列,满足他给定的要求,即从s到s+n的数的和大于或小于给定的k。问是否存在这样的序列。

思路:首先将序列变成X0,X1,X2...Xn的形式,表示前i项的和,那么每个约束条件我们都可以变成Xi-Xj<=k的形式,然后构图,使Xj指向Xi的有向边的权值为k,另外再找到一个点n+1,使得其到所有点的有向边的权值为0。如果这个图没有负环存在的话,那么就是有解的。

AC代码如下:

#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
using namespace std;
int n,m,d[110],vis[110],p[110],INF=1e9;
char s[10];
queue<int> qu;
struct node
{
    int v,w,next;
}edge[510];
int h[110],tot;
void AddEdge(int u,int v,int w)
{
    edge[++tot].v=v;
    edge[tot].w=w;
    edge[tot].next=h[u];
    h[u]=tot;
}
void spfa(int s)
{
    int i,j,k,u,v,w,f;
    memset(vis,0,sizeof(vis));
    memset(p,0,sizeof(p));
    for(i=0;i<=n;i++)
       d[i]=INF;
    d[s]=0;
    while(!qu.empty())
      qu.pop();
    qu.push(s);vis[s]=1;
    while(!qu.empty())
    {
        u=qu.front();
        qu.pop();
        for(f=h[u];f;f=edge[f].next)
        {
            v=edge[f].v;
            w=edge[f].w;
            if(d[u]+w<d[v])
            {
                d[v]=d[u]+w;
                if(vis[v]==0)
                {
                    qu.push(v);
                    vis[v]=1;
                    if(++p[v]>n)
                    {
                        printf("successful conspiracy\n");
                        return;
                    }
                }
            }
        }
        vis[u]=0;
    }
    printf("lamentable kingdom\n");
}
int main()
{
    int i,j,k,a,b,u,v;
    while(~scanf("%d",&n) && n>0)
    {
        scanf("%d",&m);
        memset(h,0,sizeof(h));tot=0;
        n++;
        for(i=1;i<=m;i++)
        {
            scanf("%d%d%s%d",&a,&b,s,&k);
            if(s[0]=='g')
              AddEdge(a+b,a-1,-k-1);
            else
              AddEdge(a-1,a+b,k-1);
        }
        for(i=0;i<=n;i++)
           AddEdge(n,i,0);
        spfa(n);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值