首页 > 代码库 > POJ训练计划1201_Intervals(差分约束)
POJ训练计划1201_Intervals(差分约束)
解题报告
题目传送门
思路:
解方程组:
(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.
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(差分约束)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。