POJ1201 Intervals 【差分约束系统】

Intervals

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 21591   Accepted: 8122

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

因为这题不需要判断负环,所以不用添加虚拟节点,另外,求最短路径时,原点应该是最右边的点。

#include <stdio.h>
#include <string.h>
#include <queue>
#define maxn 50002
#define inf 0x3f3f3f3f
using std::queue;

int head[maxn], id, left, right, dist[maxn];
struct Node{
    int to, w, next;
} E[maxn << 2];
bool vis[maxn];

void addEdge(int a, int b, int c)
{
    E[id].to = b; E[id].w = c;
    E[id].next = head[a]; head[a] = id++;
}

int SPFA()
{
    int i, u, v, tmp;
    for(i = left; i <= right + 1; ++i){
        vis[i] = 0; dist[i] = inf;
    }
    queue<int> Q; Q.push(right);
    dist[right] = 0; vis[right] = 1;
    while(!Q.empty()){
        u = Q.front(); Q.pop();
        vis[u] = 0;
        for(i = head[u]; i != -1; i = E[i].next){
            v = E[i].to;
            tmp = dist[u] + E[i].w;
            if(tmp < dist[v]){
                dist[v] = tmp;
                if(!vis[v]){
                    vis[v] = 1;
                    Q.push(v);
                }
            }
        }
    }
    return -dist[left];
}

int main()
{
    //freopen("stdin.txt", "r", stdin);
    int n, a, b, c, i;
    while(scanf("%d", &n) == 1){
        memset(head, -1, sizeof(head));
        left = maxn; right = 0;
        for(i = id = 0; i < n; ++i){
            scanf("%d%d%d", &a, &b, &c);
            addEdge(b+1, a, -c);
            if(a < left) left = a;
            if(b > right) right = b;
        }
        ++right;
        for(i = left; i < right; ++i){
            addEdge(i+1, i, 0);
            addEdge(i, i+1, 1);
        }
        printf("%d\n", SPFA());
    }
    return 0;
}

正着求也对:

#include <stdio.h>
#include <string.h>
#include <queue>
#define maxn 50002
#define inf 0x3f3f3f3f
using std::queue;

int head[maxn], id, left, right, dist[maxn];
struct Node{
    int to, w, next;
} E[maxn << 2];
bool vis[maxn];

void addEdge(int a, int b, int c)
{
    E[id].to = b; E[id].w = c;
    E[id].next = head[a]; head[a] = id++;
}

int SPFA()
{
    int i, u, v, tmp;
    for(i = left; i <= right + 1; ++i){
        vis[i] = 0; dist[i] = -inf;
    }
    queue<int> Q; Q.push(left);
    dist[left] = 0; vis[left] = 1;
    while(!Q.empty()){
        u = Q.front(); Q.pop();
        vis[u] = 0;
        for(i = head[u]; i != -1; i = E[i].next){
            v = E[i].to;
            tmp = dist[u] + E[i].w;
            if(tmp > dist[v]){
                dist[v] = tmp;
                if(!vis[v]){
                    vis[v] = 1;
                    Q.push(v);
                }
            }
        }
    }
    return dist[right];
}

int main()
{
   // freopen("stdin.txt", "r", stdin);
    int n, a, b, c, i;
    while(scanf("%d", &n) == 1){
        memset(head, -1, sizeof(head));
        left = maxn; right = 0;
        for(i = id = 0; i < n; ++i){
            scanf("%d%d%d", &a, &b, &c);
            addEdge(a, b+1, c);
            if(a < left) left = a;
            if(b > right) right = b;
        }
        ++right;
        for(i = left; i < right; ++i){
            addEdge(i, i + 1, 0);
            addEdge(i+1, i, -1);
        }
        printf("%d\n", SPFA());
    }
    return 0;
}

POJ1201 Intervals 【差分约束系统】

时间: 2024-08-02 06:51:02

POJ1201 Intervals 【差分约束系统】的相关文章

POJ1201 Intervals[差分约束系统]

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 26028   Accepted: 9952 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 po

poj 1201 Intervals(差分约束系统)(困难)

Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23205   Accepted: 8764 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 po

poj1201 Intervals——差分约束

题目:http://poj.org/problem?id=1201 差分约束裸题: 设 s[i] 表示到 i 选了数的个数前缀和: 根据题意,可以建立以下三个限制关系: s[bi] >= s[ai-1] + ci ( 1 <= i <= n) s[i] >= s[i-1] + 0 ( 1 <= i <= mx) s[i-1] >= s[i] + (-1) (1 <= i <= mx) 然后求最长路,可以发现其中的 dis 值不会多余增大,也就满足题意要

poj 1201 Intervals 差分约束系统

题目链接:http://poj.org/problem?id=1201 题意:给定n(1<= n <= 50000)个 闭区间,每个区间后面带一个值 Ci, 问集合Z个数的最小值使得在每个区间中的数的个数 “不少于Ci”? 思路: S[i] 表示 小于等于i 的个数,这样可以直接按照输入建立不等式之后转化为有向网即可: 需要注意的是 在原始的两个不等式 S[i-1] - s[i] <= 0 和 s[i-1] - s[i] <= 1不宜在建边时就加入,这会使得有向网络中的边数达到3*

POJ 1201 &amp;&amp; HDU 1384 Intervals(差分约束系统)

题目地址:POJ 1201   HDU 1384 根据题目意思,可以列出不等式如下: Sj-Si>=c; Si-S(i-1)>=0; S(i-1)-Si>=-1; 然后用最短路spfa来解决这个不等式.用max来当源点,0为终点.最终的-d[0]就是答案. 代码如下: #include <iostream> #include <cstdio> #include <string> #include <cstring> #include <

hdu 1384 Intervals 差分约束系统

注意初始化 #include "stdio.h" #include "string.h" #include "algorithm" #include "queue" #include "vector" using namespace std; const int inf=0x7FFFFFFF; struct node { int to; int c; }; int d[50005],inq[50005];

zoj 1508 Intervals 差分约束系统

#include "stdio.h" #include "string.h" #include "algorithm" using namespace std; struct name { int u,v,w; }e[50005]; int dist[50005]; int main() { int i,j,k,n,u,v,w,mx,mn,t; bool f; while(~scanf("%d",&n)) { for(

【POJ 1201】 Intervals(差分约束系统)

[POJ 1201] Intervals(差分约束系统) 11 1716的升级版 把原本固定的边权改为不固定. Intervals Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 23817   Accepted: 9023 Description You are given n closed, integer intervals [ai, bi] and n integers c1, ..., cn. Write a p

POJ1201 Intervals【SPFA】【差分约束】

Intervals Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 22307 Accepted: 8413 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