hdu3592 & poj1716 & zoj4028(差分约束)

World Exhibition

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1957    Accepted Submission(s): 971


Problem Description
Nowadays, many people want to go to Shanghai to visit the World Exhibition. So there are always a lot of people who are standing along a straight line waiting for entering. Assume that there are N (2 <= N <= 1,000) people numbered 1..N who are standing in the same order as they are numbered. It is possible that two or more person line up at exactly the same location in the condition that those visit it in a group.

There is something interesting. Some like each other and want to be within a certain distance of each other in line. Some really dislike each other and want to be separated by at least a certain distance. A list of X (1 <= X <= 10,000) constraints describes which person like each other and the maximum distance by which they may be separated; a subsequent list of Y constraints (1 <= Y <= 10,000) tells which person dislike each other and the minimum distance by which they must be separated.

Your job is to compute, if possible, the maximum possible distance between person 1 and person N that satisfies the distance constraints.
 

Input
First line: An integer T represents the case of test.

The next line: Three space-separated integers: N, X, and Y. 

The next X lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= N. Person A and B must be at most C (1 <= C <= 1,000,000) apart.

The next Y lines: Each line contains three space-separated positive integers: A, B, and C, with 1 <= A < B <= C. Person A and B must be at least C (1 <= C <= 1,000,000) apart.
 

Output
For each line: A single integer. If no line-up is possible, output -1. If person 1 and N can be arbitrarily far apart, output -2. Otherwise output the greatest possible distance between person 1 and N.
 

Sample Input
 
 
14 2 11 3 82 4 152 3 4
 

Sample Output
 
 
19
 

Author
alpc20
 

题意:数轴上有n个点,给出这n个点的几个约束--其中x个:a1点和b1点最大距离小于c1,y个:a2点和b2点的最小距离大于c2,求1和n的最大距离。

思路:差分约束模板题。

将b-a<=c转换为b<=a+c,建一条从a到b的价值为c的边,b-a>=c转换为a<=b-c,建一条从b到a的价值为-c的边。这样1到n的最大距离转换为图中1到n的最短路。

基于spfa的差分约束。

#include <stdio.h>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  
#include <queue>  
using namespace std;  
const int maxn=300001;  
const int inf =0x7ffffff;  
struct edge  
{  
    int from,to,w,next;  
}e[1000001];  
int head[maxn];  
int vis[maxn];  
int dist[maxn];  
int n,x,y,cnt;  
int num[maxn];
bool flag;
void add(int i,int j,int w)  
{  
    e[cnt].from=i;  
    e[cnt].to=j;  
    e[cnt].w=w;  
    e[cnt].next=head[i];  
    head[i]=cnt++;  
}  
void spfa(int s)  
{  
    queue <int> q;  
    for(int i=1;i<=n;i++)  
    dist[i]=inf;  
    memset(vis,false,sizeof(vis));  
    q.push(s);  
    dist[s]=0;  
    while(!q.empty())  
    {  
        int u=q.front();  
        q.pop();  
        vis[u]=false;  
        for(int i=head[u];i!=-1;i=e[i].next)  
        {  
            int v=e[i].to;  
            if(dist[v]>dist[u]+e[i].w)  
            {  
                dist[v]=dist[u]+e[i].w;  
                if(!vis[v])  
                {  
                    vis[v]=true;  
                    q.push(v); 
                    if(++num[v]>n)
                    {
                        flag=false;
                        return;
                    } 
                }  
            }  
        }  
    }  
}  
int main()  
{  
    int a,b,c,s,e;  
    int t;
    scanf("%d",&t);
    while(t--)
    {
        memset(num,0,sizeof(num));
        flag=true;
        scanf("%d%d%d",&n,&x,&y);  
        cnt=0;  
        memset(head,-1,sizeof(head));  
        while(x--)  
        {  
            scanf("%d%d%d",&a,&b,&c);  
            add(a,b,c);  
        }  
        while(y--)
        {
            scanf("%d%d%d",&a,&b,&c);
            add(b,a,-c);
        }
        s=1;e=n;
        spfa(s); 
        if(!flag)printf("-1\n"); 
        else if(dist[e]==inf) printf("-2\n");  
        else printf("%d\n",dist[e]);  
    }
}



Integer Intervals
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 15469 Accepted: 6550

Description

An integer interval [a,b], a < b, is a set of all consecutive integers beginning with a and ending with b. 
Write a program that: finds the minimal number of elements in a set containing at least two different integers from each interval.

Input

The first line of the input contains the number of intervals n, 1 <= n <= 10000. Each of the following n lines contains two integers a, b separated by a single space, 0 <= a < b <= 10000. They are the beginning and the end of an interval.

Output

Output the minimal number of elements in a set containing at least two different integers from each interval.

Sample Input

4
3 6
2 4
0 2
4 7

Sample Output

4


题意:给定n个区间,现在要求你求出一个最小整数集合,满足所有给出的区间中至少包含集合中两个元素。

思路:差分约束。设s[i]表示前[0,i]区间内包含的集合中元素数,那么对于区间[a,b]可以得出s[b]>=s[a-1]+2,另外又有s[i-1]>=s[i]-1,s[i]>=s[i-1]。

然后同hdu3592一样建边,但是由于这次为大于号,所以应当求最长路,所以把边值取反,再利用spfa,最后结果取反就好。

#include <stdio.h>  
#include <cstring>  
#include <iostream>  
#include <algorithm>  
#include <queue>  
using namespace std;  
const int maxn=10005;  
const int inf =0x7ffffff;  
struct edge  
{  
    int from,to,w,next;  
}e[1000001];  
int head[maxn];  
int vis[maxn];  
int dist[maxn];  
int n,m,cnt,r;  
void add(int i,int j,int w)  
{  
    e[cnt].from=i;  
    e[cnt].to=j;  
    e[cnt].w=w;  
    e[cnt].next=head[i];  
    head[i]=cnt++;  
}  
void spfa(int s)  
{  
    queue <int> q;  
    for(int i=1;i<=10001;i++)  
    dist[i]=inf;  
    memset(vis,false,sizeof(vis));  
    q.push(s);  
    dist[s]=0;  
    while(!q.empty())  
    {  
        int u=q.front();  
        q.pop();  
        vis[u]=false;  
        for(int i=head[u];i!=-1;i=e[i].next)  
        {  
            int v=e[i].to;  
            if(dist[v]>dist[u]+e[i].w)  
            {  
                dist[v]=dist[u]+e[i].w;  
                if(!vis[v])  
                {  
                    vis[v]=true;  
                    q.push(v); 
                }  
            }  
        }  
    }  
}  
int main()  
{  
    int a,b,s,e;  
    scanf("%d",&m);  
    cnt=0;  
    memset(head,-1,sizeof(head));  
    r=0;
    while(m--)  
    {  
        scanf("%d%d",&a,&b);  
        add(a,b+1,-2);  
        r=max(r,b+1);
    }  
    for(int i=1;i<=r;i++)
    {
        add(i,i-1,1);
        add(i-1,i,0);
    }
    s=0;e=r;
    spfa(s);    
    printf("%d\n",-dist[e]);  
}


ZOJ Problem Set - 4028
LIS

Time Limit: 1 Second       Memory Limit: 65536 KB       Special Judge

DreamGrid is learning the LIS (Longest Increasing Subsequence) problem and he needs to find the longest increasing subsequence of a given sequence  of length .

Recall that

  • A subsequence  of length  is a sequence satisfying  and .

  • An increasing subsequence  is a subsequence satisfying .

DreamGrid defines the helper sequence  where  indicates the maximum length of the increasing subsequence which ends with . In case you don't know how to derive the helper sequence, he provides you with the following pseudo-code which calculates the helper sequence.

procedure lis_helper(: original sequence)
{Let  be the length of the original sequence,
 be the -th element in sequence , and 
be the -th element in sequence }
for  := 1 to 
     := 1
    for  := 1 to ( - 1)
        if  and 
             :=  + 1
return  { is the helper sequence}

DreamGrid has derived the helper sequence using the program, but the original sequence  is stolen by BaoBao and is lost! All DreamGrid has in hand now is the helper sequence and two range sequences  and  indicating that  for all .

Please help DreamGrid restore the original sequence which is compatible with the helper sequence and the two range sequences.

Input

There are multiple test cases. The first line of the input contains an integer , indicating the number of test cases. For each test case:

The first line contains an integer  (), indicating the length of the original sequence.

The second line contains  integers  () seperated by a space, indicating the helper sequence.

For the following  lines, the -th line contains two integers  and  (), indicating the range sequences.

It's guaranteed that the original sequence exists, and the sum of  of all test cases will not exceed .

Output

For each test case output one line containing  integers separated by a space, indicating the original sequence. If there are multiple valid answers, print any of them.

Please, DO NOT print extra spaces at the end of each line, or your solution may be considered incorrect!

Sample Input
4
6
1 2 3 2 4 3
0 5
2 4
3 3
1 2
3 5
1 5
5
1 2 1 3 1
100 200
200 300
200 400
400 500
100 500
7
1 2 3 1 1 4 2
0 3
0 3
0 3
0 3
0 3
0 3
0 3
2
1 1
1 2
2 3
Sample Output
1 2 3 2 5 3
200 300 200 500 200
0 1 2 0 0 3 1
2 2
 
 
 
 
题意:给你一组最长上升子序列的dp区间值,然后给你每个点的取值范围,求可行的原序列

首先满足l[i] <= a[i] <= r[i],  可以建一个源点 n+1,那么有

a[n+1] - a[i] <= -l[i],  a[i] - a[n+1] <= r[i]

对于之前出现过a[i]的点 j,要使a[i]>=a[j](保证没法转移),  有

a[j] - a[i] <= 0

还有对于从上一个a[i]-1转移过来的点 j, 要使a[i] > a[j],有

a[j] - a[i] <= -1

跑一边最短路,d[i] 即为所求
 
 
 
 
 
 
#include <stdio.h>    
#include <cstring>    
#include <iostream>    
#include <algorithm>    
#include <queue>    
using namespace std;    
typedef long long ll;
const int maxn=100005;    
const ll inf =0x7f7f7f7f;    
struct edge    
{    
    int to,next;
    ll w;    
}e[1000001];    
int head[maxn];    
int vis[maxn];    
ll dist[maxn];    
int n,x,y,cnt;    
int last[100005]; 
bool flag;  
void add(int i,int j,ll w)    
{    
//    e[cnt].from=i;    
    e[cnt].to=j;    
    e[cnt].w=w;    
    e[cnt].next=head[i];    
    head[i]=cnt++;    
}    
void spfa(int s)    
{    
    queue <int> q;    
    for(int i=1;i<=n;i++)    
    dist[i]=inf;    
    memset(vis,false,sizeof(vis));    
    q.push(s);    
    dist[s]=0;    
    while(!q.empty())    
    {    
        int u=q.front();    
        q.pop();    
        vis[u]=false;    
        for(int i=head[u];i!=-1;i=e[i].next)    
        {    
            int v=e[i].to;    
            if(dist[v]>dist[u]+e[i].w)    
            {    
                dist[v]=dist[u]+e[i].w;    
                if(!vis[v])    
                {    
                    vis[v]=true;    
                    q.push(v);   
                }    
            }    
        }    
    }    
}    
int main()    
{    
    int s;    
    int t;  
    scanf("%d",&t);  
    while(t--)  
    {  
        cnt=0;    
        memset(head,-1,sizeof(head));
        memset(last,0,sizeof(last));
        flag=true;  
        scanf("%d",&n);    
        s=n+1;
        int a;
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a);
            if(last[a])
            {
                add(last[a],i,0);
            }
            if(a)
            {
                add(i,last[a-1],-1);
            }
            last[a]=i;
        } 
        ll l,r;
        for(int i=1;i<=n;i++)
        {
            scanf("%lld%lld",&l,&r);
            add(i,s,-l);
            add(s,i,r);
        }
          
        spfa(s);      
        for(int i=1;i<n;i++)printf("%lld ",dist[i]);
        printf("%lld\n",dist[n]);
    }  
}






  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值