差分约束---------区间

给定 n 个区间 [ai,biai,bi]和 n 个整数 cici。
你需要构造一个整数集合 Z,使得∀i∈[1,n]∀i∈[1,n],Z 中满足ai≤x≤biai≤x≤bi的整数 x 不少于 cici 个。
求这样的整数集合 Z 最少包含多少个数。
输入格式
第一行包含整数 n。
接下来n行,每行包含三个整数ai,bi,ciai,bi,ci。
输出格式
输出一个整数表示结果。
数据范围
1≤n≤500001≤n≤50000,

0≤ai,bi≤500000≤ai,bi≤50000,

1≤ci≤bi−ai+11≤ci≤bi−ai+1
输入样例:
5
3 7 3
8 10 3
6 8 1
1 3 1
10 11 1

输出样例:
6

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 50010, M = 150010;
int n;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
int q[N];
bool st[N];
void add(int a, int b, int c){
 e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
void spfa(){
 memset(dist, -0x3f, sizeof dist);
 dist[0] = 0;
 st[0] = true;
  int hh = 0, tt = 1;
 q[0] = 0;
 while(hh != tt){
  int t = q[hh ++];
  if (hh == N)   hh = 0;
  st[t] = false;
  for (int i = h[t]; ~i; i = ne[i]){
   int j = e[i];
   if (dist[j] < dist[t] + w[i]){
    dist[j] = dist[t] + w[i];
    if (!st[j]){
     q[tt ++] = j;
     if (tt == N)   tt = 0;
     st[j] = true;
    }
   }
  }
 }
}
int main(){
 scanf("%d", &n);
 memset(h, -1, sizeof h);
  for (int i = 1; i < N; i ++){
  add(i - 1, i, 0);
  add(i, i - 1, -1);
 }
  for (int i = 0; i < n; i ++){
  int a, b, c;
  scanf("%d%d%d", &a, &b, &c);
  a++, b++;
  add(a - 1, b, c);
 }
  spfa();
  printf("%d\n", dist[50001]);
  return 0;
}
发布了164 篇原创文章 · 获赞 112 · 访问量 6765

猜你喜欢

转载自blog.csdn.net/qq_45772483/article/details/105495659