hdu 1364(差分约束)

King

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 12056   Accepted: 4397

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 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)<ki或者是ai+a(i+1)+a(i+2)+...+a(i+n)>ki首先给出两个数分别代表S序列有多少个,有多少个不等式不等式可以这样描述给出四个参数第一个数i可以代表序列的第几项,然后给出n,这样前面两个数就可以描述为ai+a(i+1)+...a(i+n),即从i到n的连续和,再给出一个符号和一个ki当符号为gt代表‘>’,符号为lt代表‘<‘那么样例可以表示1 2 gt 0a1+a2+a3>02 2 lt 2a2+a3+a4<2最后问你所有不等式是否都满足条件,若满足输出lamentable kingdom,不满足输出successful conspiracy,这里要注意了,不要搞反了

题解:每一个序列之和都可以用前缀和表示出来,比如说第一个测试用例,sum[3]-sum[0]>0,sum[4]-sum[1]<2,又因为差分约束系统必须是>=或者<=,这题又全是整数,所以我们可以通过加减一来得到标准差分约束式,这些点不一定是联通的,所以加个超级源点,然后判断负环.
#include <iostream>
#include <cstdio>
#include <string.h>
#include <queue>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long LL;
const int INF = 999999999;
const int N = 200;
struct Edge{
    int v,w,next;
}edge[10*N];
int head[N];
int tot ;
void init(){
    memset(head,-1,sizeof(head));
    tot = 0;
}
void addEdge(int u,int v,int w,int &k){
    edge[k].v = v,edge[k].w = w,edge[k].next = head[u],head[u]=k++;
}
int n,m;
int low[N],time[N];
bool vis[N];
bool spfa(int s){
    for(int i=0;i<=105;i++){
        vis[i] = false;
        low[i] = INF;
        time[i] = 0;
    }
    low[s] = 0;
    time[s]++;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        vis[u] = false;
        for(int k=head[u];k!=-1;k=edge[k].next){
            int v = edge[k].v,w=edge[k].w;
            if(low[v]>low[u]+w){
                low[v] = low[u]+w;
                if(!vis[v]){
                    vis[v] = true;
                    q.push(v);
                    if(time[v]++>n+1) return false;
                }
            }
        }
    }
    return true;
}
int main(){
    while(scanf("%d",&n)!=EOF,n){
        init();
        scanf("%d",&m);
        int super = 101;
        for(int i=1;i<=m;i++){
            int u,v,w;
            char op[5];
            scanf("%d%d%s%d",&u,&v,op,&w);
            v = u+v;
            if(op[0]==‘g‘){
                w+=1;
                addEdge(v,u-1,-w,tot);
            }else{
                w-=1;
                addEdge(u-1,v,w,tot);
            }
            addEdge(super,u-1,0,tot);
            addEdge(super,v,0,tot);
        }
        if(spfa(super)){
            printf("lamentable kingdom\n");
        }else{
            printf("successful conspiracy\n");
        }
    }
    return 0;
}
				
时间: 2024-12-15 06:57:45

hdu 1364(差分约束)的相关文章

hdu 3666(差分约束,手动栈解决超时问题)

THE MATRIX PROBLEM Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8016    Accepted Submission(s): 2092 Problem Description You have been given a matrix CN*M, each element E of CN*M is positive

POJ 1364[差分约束]

题目链接:[http://poj.org/problem?id=1364] 晕死了.但是也长知识了 题意:一个长度为n的序列:a[1].a[2].a[3]...a[n],然后给你一些约束条件:si.ni.gt||lt.ki表示:a[si].a[si+1]....a[si+ni]<or>ki,问你满足这些约束条件,字符串是否存在.存在输出:lamentable kingdom,否则输出:successful conspiracy 题解:对序列求前缀和,将约束条件改为:sum[si+ni]-sum

Instrction Arrangement (hdu 4109 差分约束)

Instrction Arrangement Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1395    Accepted Submission(s): 584 Problem Description Ali has taken the Computer Organization and Architecture course th

poj 1364差分约束

King Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 10206   Accepted: 3777 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 kin

hdu 1534(差分约束)

Schedule Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1715    Accepted Submission(s): 757Special Judge Problem Description A project can be divided into several parts. Each part shoul

hdu 差分约束题集

[HDU]1384 Intervals 基础差分约束★1529 Cashier Employment 神级差分约束★★★★ 1531 King 差分约束★1534 Schedule Problem 差分约束输出一组解★3440 House Man 比较好的差分约束★★3592 World Exhibition 简单★3666 THE MATRIX PROBLEM 中等★★4274 Spy's Work [先处理出欧拉序列,然后就是差分约束了...] [POJ]1201 Intervals1275

POJ 1364 King --差分约束第一题

题意:求给定的一组不等式是否有解,不等式要么是:SUM(Xi) (a<=i<=b) > k (1) 要么是 SUM(Xi) (a<=i<=b) < k (2) 分析:典型差分约束题,变换,令Ti = SUM(Xj) (0<=j<=i).  则表达式(1)可以看做T(a+b)-T(a-1) > k,也就是T(a-1)-T(a+b) < -k,又因为全是整数,所以T(a-1)-T(a+b) <= -k-1.  同理,(2)看做T(a+b)-T(

Hdu 3666 THE MATRIX PROBLEM(差分约束)

题目地址:http://acm.split.hdu.edu.cn/showproblem.php?pid=3666 思路:差分约束. 取对数将乘除转化为加减. L<=m[i][j]*a[i]/b[j]<=U log(L/m[i][j])<=log(a[i])-log(b[j])<=log(U/m[i][j]) 则 : log(a[i])<=log(b[j])+log(U/m[i][j]) log(b[j])<=log(a[i])+log(m[i][j]/L) SPFA判

Hdu 4594 Difference(奇圈判断+差分约束)

题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4598 思路:由题意可知两相连点ai符号一定相反,所以若存在奇圈则一定无解.染色,colour[i]==1表示为正,colour[i]==2表示为负.由于(b)条件为充要条件,所以对于图中的点| a[i]-a[j] | >= T,对于非图中点| a[i]-a[j] | < T,即| a[i]-a[j] | <= T-1 .所以图中点,若colour[i]==1,a[i]-a[j] >=