Bath Valley 1645-Sequence (Differential Constraint System)

Posted by herreram on Sun, 19 May 2019 22:00:03 +0200

Title Description

There is a sequence of integers, each of which is different. We don't know how long it is (that is, the number of integers), but we know how many integers are at least in some intervals. We use intervals (Li,Ri,Ci) to describe how many Ci numbers in this sequence come from intervals [Li,Ri]. Give a number of such intervals, and ask the length of this sequence of integers is the least. How much can it do?

Input and output format

Input format:

 

The first line is an integer N, representing the number of intervals.

Next, N lines, three integers per line (Li,Ri,Ci), describe an interval.

[Data Scale]

N<=1000,0<=Li<=Ri<=1000,1<=Ci<=Ri-Li+1

 

Output format:

 

Only one number represents the minimum length of the sequence of integers.

 

Input and Output Samples

Input sample #1:copy

4
4 5 1
6 10 3
7 10 3
5 6 1

Output sample #1:copy

4

 

 

I've seen this algorithm before, but I haven't done it yet. Today it's an introductory problem.

Differential constrained system is the most difficult to model, how to construct the connection graph according to the known inequalities.

 

Because the title requires the minimum value, we need to find the longest path in the constructed graph.

Suppose there are intervals [L, R] and at least x numbers. We can get the inequality R-L+1>= x, that is, we need to construct an addedge(L,R+1,x).

Of course, the graph constructed according to the known conditions may be disconnected, so we need to find the hidden inequality conditions in the title to make the modeled graph connected and to find the longest path.

Hidden inequalities?

When the left and right intervals are equal, we can get an inequality of 0<=R-L+1<=1, so we can construct two edges addedge(L,R+1,0), addedge(R+1,L,-1).

In this way, we guarantee the connectivity of graphs.

 

In the beginning, the idea was to move the left 1 of the inequality to the right to construct the edge of addedge(L,R,x-1), but that was wrong.

After thinking for a long time, we can only find such counterexamples. In hidden inequalities, if we add edges like this, we still can not guarantee the connectivity of graphs.

I don't know if it's right to think so. If there's a big man passing by, please give me some advice and prove it.

 

Write two codes, used to like to use vectors to simulate adjacency tables, and later like the chain forward star, this time it should be the last use of vectors.

 

/*#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<queue>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define memset(a,v)  memset(a,v,sizeof(a))
#define X (sqrt(5)+1)/2.0  //Wythoff
#define Pi acos(-1)
#define e  2.718281828459045
#define eps 1.0e-8
using namespace std;
typedef long long int LL;
typedef pair<int,int>pa;
const int MAXL(1000);
const int INF(0x3f3f3f3f);
const int mod(1e9+7);
int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
struct node
{
    int ed,w;
} nod;
vector<node>v[MAXL+50];
int vis[MAXL+50];
int dis[MAXL+50];
void addedge(int x,int y,int w)
{
    v[x].push_back(node{y,w});
}
int SPFA(int u,int ma)
{
    queue<int>q;
    memset(vis,0);
    memset(dis,-INF);
    vis[u]=1;
    dis[u]=0;
    q.push(u);
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        vis[t]=0;
        for(int i=0; i<v[t].size(); i++)
        {
            int x=v[t][i].ed;
            if(dis[x]<dis[t]+v[t][i].w)
            {
                dis[x]=dis[t]+v[t][i].w;
                if(!vis[x])
                {
                    vis[x]=1;
                    q.push(x);
                }
            }
        }

    }
    return dis[ma];
}
int main()
{
    int n;
    scanf("%d",&n);
    int mi=INF,ma=-INF;
    for(int i=1; i<=n; i++)
    {
        int x,y,w;
        scanf("%d%d%d",&x,&y,&w);
        addedge(x,y+1,w);
        mi=min(mi,x);
        ma=max(ma,y+1);
    }
    for(int i=mi;i<=ma;i++)
    {
        addedge(i,i+1,0);
        addedge(i+1,i,-1);
    }
    int ans=SPFA(mi,ma);
    cout<<ans<<endl;
}
*/





#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<iomanip>
#include<algorithm>
#include<vector>
#include<queue>
#define max(a,b)   (a>b?a:b)
#define min(a,b)   (a<b?a:b)
#define swap(a,b)  (a=a+b,b=a-b,a=a-b)
#define memset(a,v)  memset(a,v,sizeof(a))
#define X (sqrt(5)+1)/2.0  //Wythoff
#define Pi acos(-1)
#define e  2.718281828459045
#define eps 1.0e-8
using namespace std;
typedef long long int LL;
typedef pair<int,int>pa;
const int MAXL(1e6);
const int INF(0x3f3f3f3f);
const int mod(1e9+7);
int dir[4][2]= {{-1,0},{1,0},{0,1},{0,-1}};
struct node
{
    int to,next,w;

}edge[MAXL+50];
int head[MAXL+50];
int vis[MAXL+50];
int dis[MAXL+50];
int cnt=0;
void addedge(int x,int y,int w)
{
    edge[cnt].to=y;
    edge[cnt].next=head[x];
    edge[cnt].w=w;
    head[x]=cnt++;
}
int SPFA(int u,int ma)
{
    queue<int>q;
    memset(vis,0);
    memset(dis,-INF);
    vis[u]=1;
    dis[u]=0;
    q.push(u);
    while(!q.empty())
    {
        int t=q.front();
        q.pop();
        vis[t]=0;
        for(int i=head[t]; ~i; i=edge[i].next)
        {
            int x=edge[i].to;
            if(dis[x]<dis[t]+edge[i].w)
            {
                dis[x]=dis[t]+edge[i].w;
                if(!vis[x])
                {
                    vis[x]=1;
                    q.push(x);
                }
            }
        }

    }
    return dis[ma];
}
int main()
{
    memset(head,-1);
    int n;
    scanf("%d",&n);
    int mi=INF,ma=-INF;
    for(int i=1; i<=n; i++)
    {
        int x,y,w;
        scanf("%d%d%d",&x,&y,&w);
        addedge(x,y+1,w);
        mi=min(mi,x);
        ma=max(ma,y+1);
    }
    for(int i=mi;i<=ma;i++)
    {
        addedge(i,i+1,0);
        addedge(i+1,i,-1);
    }
    int ans=SPFA(mi,ma);
    cout<<ans<<endl;
}