• POJ1201 Intervals


     

     Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 25125   Accepted: 9580

    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

    Source

    差分约束

     1 /**/
     2 #include<iostream>
     3 #include<cstdio>
     4 #include<cmath>
     5 #include<cstring>
     6 #include<algorithm>
     7 #include<queue>
     8 using namespace std;
     9 const int INF=0x3f3f3f3f;
    10 const int mxn=60000;
    11 struct edge{
    12     int v,dis;
    13     int next;
    14 }e[mxn*10];
    15 int hd[mxn],cnt;
    16 int dis[mxn];
    17 int vis[mxn];
    18 int inq[mxn];
    19 int n;
    20 void add_edge(int u,int v,int w){
    21     e[++cnt].next=hd[u];e[cnt].v=v;e[cnt].dis=w;hd[u]=cnt;
    22 }
    23 bool SPFA(){
    24     memset(dis,-1,sizeof dis);
    25     queue<int>q;
    26     q.push(0);
    27     inq[0]=1;
    28     vis[0]=1;
    29     dis[0]=0;
    30     while(!q.empty()){
    31         int u=q.front();
    32         for(int i=hd[u];i;i=e[i].next){
    33             int v=e[i].v;
    34             if(dis[u]+e[i].dis>dis[v]){
    35                 dis[v]=dis[u]+e[i].dis;
    36                 vis[v]++;
    37                 if(vis[v]>n)return 0;
    38                 if(!inq[v]){
    39                     inq[v]=1;
    40                     q.push(v);
    41                 }
    42             }
    43         }
    44         q.pop();
    45         inq[u]=0;
    46     }
    47     return 1;
    48 }
    49 int main(){
    50     scanf("%d",&n);
    51     int i,j;
    52     int mx=-1,a,b,c;
    53     for(i=1;i<=n;i++){
    54         scanf("%d%d%d",&a,&b,&c);
    55         add_edge(a,b+1,c);
    56         mx=max(mx,b+1);
    57     }
    58     for(i=0;i<mx;i++){
    59         add_edge(i,i+1,0);
    60         add_edge(i+1,i,-1);
    61     }
    62     SPFA();
    63     printf("%d
    ",dis[mx]);
    64     return 0;
    65 }
  • 相关阅读:
    ps 快捷键
    python中== 和 is 的区别
    微信开发者工具快捷键汇总
    IDEA常用快捷键
    非前后端分离项目使用vue继承,提取公共方法和filters
    Plupload上传插件中文帮助文档
    idea 提示 string template are not supported current JavaScrip Version 的解决
    nginx的分配方式
    nginx相关配置的内容
    Deepin启动界面个性化
  • 原文地址:https://www.cnblogs.com/SilverNebula/p/5742599.html
Copyright © 2020-2023  润新知