poj1201 差分约束系统/system of different contraints

用s[i]表示小于等于i的元数个数

可得约束条件为:s[node.from]-s[node.to]<-c,隐含的为0<=s[i]-s[i-1]<=1

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int MAXN=50010;
struct cnode
{
  int from;
  int to;
  int w;
}node[MAXN];
int d[MAXN];
int n;
 int mmin;
  int mmax;
 bool bellman_ford()
{
  memset(d,0,sizeof(d));
  bool flag=1;
  bool f;
  int i;
  while(flag)
    {
      flag=0;
      for(i=0;i<n;i++)
    {
      if(d[node[i].from]>d[node[i].to]+node[i].w)//from-to<c
        {
          d[node[i].from]=d[node[i].to]+node[i].w;
          flag=1;
        }
    }
     
 
      for(i=mmin;i<=mmax;i++)
    {
      if(d[i-1]+1<d[i])//0<=d[i]-d[i-1]<=1
    {
      d[i]=d[i-1]+1;
      f=1;
    }
    }
     
 
  for(i=mmax;i>=mmin;i--)
    {
      if(d[i-1]>d[i])
    {
      d[i-1]=d[i];
      f=1;
    }
    }
 
    }
  return true;

 
}
int main()
{
    int FROM,TO,W;
  int i;
  while(scanf("%d",&n)!=EOF)
    {
      mmin=800000000;
      mmax=-100;
      for(i=0;i<n;i++)
    {
      scanf("%d %d %d",&FROM,&TO,&W);
      node[i].from=FROM-1;
      node[i].to=TO;
      node[i].w=-W;
      if(FROM<mmin)
        {
          mmin=FROM;
        }
      if(TO>mmax)
        {
          mmax=TO;
        }
     
    }
      bellman_ford();
      printf("%d/n",d[mmax]-d[mmin-1]);
      return 0;
    }
 
  return 0;
}

附题目:

Intervals
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 13145 Accepted: 4860

Description

You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn.
Write a program that:
reads the number of intervals, their end points and integers c1, ..., cn from the standard input,
computes the minimal size of a set Z of integers which has at least ci common elements with interval [ai, bi], for each i=1,2,...,n,
writes the answer to the standard output.

Input

The first line of the input contains an integer n (1 <= n <= 50000) -- the number of intervals. The following n lines describe the intervals. The (i+1)-th line of the input contains three integers ai, bi and ci separated by single spaces and such that 0 <= ai <= bi <= 50000 and 1 <= ci <= bi - ai+1.

Output

The output contains exactly one integer equal to the minimal size of set Z sharing at least ci elements with interval [ai, bi], for each i=1,2,...,n.

Sample Input

5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1

Sample Output

6

你可能感兴趣的:(Integer,BI,input,each,output,Intervals)