poj1201——差分约束,spfa

poj1201——差分约束,spfa

Intervals

Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 22553   Accepted: 8530

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题意:给定N个区间[ai,bi],对应ci,求一个集合,对每个区间都有ci个数在集合中,求集合的数的最小个数思路:差分约束,s(bi)-s(ai-1)>=ci,s(i+1)-s(i)>=0,s(i)-s(i+1)>=-1。以区间最左端的Min为源点,求最长路

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<queue>

using namespace std;

const int maxn=50010;
const int INF=(1<<29);

int N;
int a,b,c;
struct Edge
{
    int v,w;
    Edge *next;
};Edge e[maxn*10];
bool vis[maxn];
int cnt[maxn];
int dist[maxn];
int Min,Max;///Min作为源点

void add_edge(int u,int v,int w)
{
    Edge *pre=&e[u];
    Edge *p=(Edge*)malloc(sizeof(Edge));
    p->v=v;p->w=w;
    p->next=pre->next;
    pre->next=p;
}

bool relax(int u,int v,int w)
{
    if(dist[u]+w>dist[v]){
        dist[v]=dist[u]+w;
        return true;
    }
    return false;
}

bool spfa()
{
    for(int i=Min;i<=Max+1;i++) dist[i]=-INF;
    dist[Min]=0;
    memset(vis,0,sizeof(vis));
    memset(cnt,0,sizeof(cnt));
    queue<int> q;
    q.push(Min);vis[Min]=1;cnt[Min]++;
    while(!q.empty()){
        int u=q.front();q.pop();vis[u]=0;
        for(Edge *p=e[u].next;p!=NULL;p=p->next){
            int v=p->v,w=p->w;
            if(relax(u,v,w)){
                if(!vis[v]){
                    q.push(v);
                    vis[v]=1;
                    cnt[v]++;
                    if(cnt[v]>N) return false;
                }
            }
        }
    }
    return true;
}

int main()
{
    while(cin>>N){
        memset(e,0,sizeof(e));
        Min=INF;Max=-INF;
        for(int i=0;i<N;i++){
            scanf("%d%d%d",&a,&b,&c);
            add_edge(a,b+1,c);
            if(a<Min) Min=a;
            if(b>Max) Max=b;
        }
        for(int i=Min;i<=Max;i++){
            add_edge(i,i+1,0);
            add_edge(i+1,i,-1);
        }
        spfa();
        cout<<dist[Max+1]<<endl;
    }
    return 0;
}

时间: 2024-12-09 20:38:10

poj1201——差分约束,spfa的相关文章

poj1201差分约束

#include<stdio.h> #include<iostream> #include<string.h> #include<queue> #include<stack> #include<list> #include<stdlib.h> #include<algorithm> #include<vector> #include<map> #include<set> #i

POJ 3169 Layout (差分约束+SPFA)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6832   Accepted: 3292 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

ZOJ 2770 Burn the Linked Camp 差分约束+SPFA

第一道正儿八经的差分约束题 有排成一列的n个点,首先告诉你每个点的值最多是多少(最少显然要大于0),然后告诉你m段i,j,k,表示第i个点到第j个点的值的和至少有k,问你总和至少为多少. 要注意的是,告诉你的所有关系式都不要忘记建边,一开始漏了大于0的条件调半天o(╯□╰)o 不等式的形式是a-b<=c这样的= = 1 #include <cstdio> 2 #include <cstring> 3 #include <cmath> 4 #include <

【bzoj2330】: [SCOI2011]糖果 图论-差分约束-SPFA

[bzoj2330]: [SCOI2011]糖果 恩..就是裸的差分约束.. x=1 -> (A,B,0) (B,A,0) x=2 -> (A,B,1)  [这个情况加个A==B无解的要特判] x=3 -> (B,A,0)  [恩这个是不少于一开始zz建反了] x=4 -> (B,A,1) x=5 -> (A,B,0) 然后源点到所有点建1的边[恩据说有条链所以要反着连]跑最长路就好了 1 /* http://www.cnblogs.com/karl07/ */ 2 #inc

poj3159 差分约束 spfa

1 //Accepted 2692 KB 1282 ms 2 //差分约束 -->最短路 3 //TLE到死,加了输入挂,手写queue 4 #include <cstdio> 5 #include <cstring> 6 #include <iostream> 7 #include <queue> 8 #include <cmath> 9 #include <algorithm> 10 using namespace std;

poj3159——Candies(差分约束+SPFA堆栈)

Description During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse's class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and ofte

(简单) POJ 3169 Layout,差分约束+SPFA。

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

poj3169——Layout(差分约束+SPFA判断负环)

Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a straight line waiting for feed. The cows are standing in the same order as they are numbe

BZOJ 2330 [SCOI2011]糖果 差分约束spfa版

题意:自行百度,(之前做过一道candy的升级版). 方法:差分约束 解析:最近在学差分约束什么的,这道是做的第一个bz上的题,感觉还是较简单的.以下我对5种操作进行描述. case 转换不等式 转换不等式2 1 A>=0+B B>=0+A 2 B>=1+A 3 A>=0+B 4 A>=1+B 5 B>=0+A 如上表按照差分约束的原理加边,然后再观察上表不等式方向->为求大边,即最长路. 这些边是不够的,所有人应最少为1糖果,即创出个源点到各点距离为1. 后记: