码迷,mamicode.com
首页 > 其他好文 > 详细

POJ训练计划1201_Intervals(差分约束)

时间:2014-08-22 22:38:21      阅读:179      评论:0      收藏:0      [点我收藏+]

标签:des   style   http   color   os   io   strong   for   ar   

解题报告

题目传送门

思路:

解方程组:

(a-1)-b<-c

0<=i-(i-1)<=1

Max-Min>=m

源点为Max,求出dis[Max]-dis[Min]

#include <iostream>
#include <cstring>
#include <cstdio>
#include <deque>
#include <stack>
#define N 100001
#define M 550000
#define inf 0x3f3f3f3f
using namespace std;
int head[N],dis[N],vis[N];
int cnt,n,m;
int maxx=0,minn=inf;
struct node {
    int v,w,next;
} edge[M];
void add(int u,int v,int w) {
    edge[cnt].v=v,edge[cnt].w=w;
    edge[cnt].next=head[u],head[u]=cnt++;
}
void SF() {
    stack<int>Q;
    memset(dis,inf,sizeof(dis));
    memset(vis,0,sizeof(vis));
    dis[maxx]=0,vis[maxx]=1;
    Q.push(maxx);
    while(!Q.empty()) {
        int u=Q.top();
        Q.pop();
        vis[u]=0;
        for(int i=head[u]; i!=-1; i=edge[i].next) {
            int v=edge[i].v;
            if(dis[v]>dis[u]+edge[i].w) {
                dis[v]=dis[u]+edge[i].w;
                if(!vis[v]) {
                    vis[v]=1;
                    Q.push(v);
                }
            }
        }
    }
}
int main() {
    int i,j,u,v,w;
    while(~scanf("%d",&n)) {
        cnt=0;
        memset(head,-1,sizeof(head));
        for(i=1; i<=n; i++) {
            scanf("%d%d%d",&u,&v,&w);
            if(v>maxx)
                maxx=v;
            if(u<minn)
                minn=u;
            add(v,u-1, -w);
        }
        for(i=1; i<=maxx; i++) {
            add(i-1,i,1);
            add(i,i-1,0);
        }
        SF();
        printf("%d\n",dis[maxx]-dis[minn-1]);
    }
    return 0;
}

Intervals
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21736   Accepted: 8180

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

POJ训练计划1201_Intervals(差分约束)

标签:des   style   http   color   os   io   strong   for   ar   

原文地址:http://blog.csdn.net/juncoder/article/details/38762053

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!