*POJ 1201 - Intervals(查分约束)

来源:互联网 发布:大唐电信数据所招聘 编辑:程序博客网 时间:2024/05/17 01:33

题目:

http://poj.org/problem?id=1201

题意:

给出n个区间【a,b】,每个区间一个c,表示在集合Z中,此区间至少有c个共同元素。询问集合Z至少有多少元素。

思路:

对于区间【a,b】= c. sa 表示区间【0,a】的共同元素,即 sb - sa >= c ,所以建边(a,b+1,c),(为了防止端点重复,则区间变为【a,b+1),eg:【1,3】=1,【3,6】 = 3,建边后【1,6】= 4,但是实际上是 = 3.)

这样建边无法将所有的点连起来,注意到关系 0<= (a+1) - a <= 1 , 所以建边(a,a+1,0),(a+1,a,-1);

此题是根据 x-y>=c,求最小值,则转换为最短路是求 s - > t 的最长路

AC.

#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <cmath>using namespace std;const int inf = 0x3f3f3f3f;const int maxn = 5e4+5;int N, s, t;int tol, head[maxn];struct Edge{    int to, w, next;}edge[maxn*3];void addedge(int u, int v, int w){    edge[tol].to = v;    edge[tol].w = w;    edge[tol].next = head[u];    head[u] = tol++;}int vis[maxn], dis[maxn];void spfa(int s){    for(int i = s; i <= t; ++i) {        vis[i] = 0;        dis[i] = -inf;    }    queue<int> que;    vis[s] = 1;    dis[s] = 0;    que.push(s);    while(!que.empty()) {        int u = que.front(); que.pop();        //printf("%d\n", u);        vis[u] = 0;        for(int i = head[u]; ~i; i = edge[i].next) {            int v = edge[i].to, w = edge[i].w;            if(dis[v] < dis[u] + w) {                dis[v] = dis[u] + w;                if(!vis[v]) {                    vis[v] = 1;                    que.push(v);                }            }        }    }}void init(){    tol = 0;    memset(head, -1, sizeof(head));}int main(){    //freopen("in", "r", stdin);    while(~scanf("%d", &N)) {        int a, b, c;        s = inf, t = -inf;        init();        for(int i = 1; i <= N; ++i) {            scanf("%d%d%d", &a, &b, &c);            s = min(s, a);            t = max(t, b+1);            addedge(a, b+1, c);        }        for(int i = s; i <= t; ++i) {            addedge(i, i+1, 0);            addedge(i+1, i, -1);        }        //printf("%d %d\n", s, t);        spfa(s);        printf("%d\n", dis[t]);    }    return 0;}


0 0