POJ 3680 Intervals 费用流构图题(区间的一定考虑相邻的)
来源:互联网 发布:淘宝拒签后怎么处理 编辑:程序博客网 时间:2024/05/19 19:16
Description
You are given N weighted open intervals. Theith interval covers (ai,bi) and weighswi. Your task is to pick some of the intervals to maximize the total weights under the limit that no point in the real axis is covered more than k times.
Input
The first line of input is the number of test case.
The first line of each test case contains two integers, N andK (1 ≤K ≤N ≤ 200).
The next N line each contain three integersai,bi,wi(1 ≤ai <bi ≤ 100,000, 1 ≤wi ≤ 100,000) describing the intervals.
There is a blank line before each test case.
Output
For each test case output the maximum total weights in a separate line.
Sample Input
43 11 2 22 3 43 4 83 11 3 22 3 43 4 83 11 100000 1000001 2 3100 200 3003 21 100000 1000001 150 301100 200 300
Sample Output
1412100000100301
题意:给定 N 个带权的开区间,第 i 个区间覆盖(ai, bi),权为 wi。现在要你挑出一些区间使得总权值最大, 并且满足实轴上任意一个点被覆盖不超过 K 次 ,每个区间选一次。注意区间内的点是实数。(1 <= K <= N<= 200, 1 <= ai < bi <= 100,000, 1 <= wi <= 100,000)
第一点:离散化处理。
第二点:求最大值,保存负数。
第三点:每个区间只能选择一次,所以对于区间(a,b),我们add(a,b,1,-w)
第四点:对于相邻的数字都要建立。(i,i+1,k,0);
涉及到区间的一定要考虑相邻数字的连接问题
- #include<iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<map>
- #include<queue>
- #include<set>
- #include<cmath>
- #include<bitset>
- #define mem(a,b) memset(a,b,sizeof(a))
- #define lson i<<1,l,mid
- #define rson i<<1|1,mid+1,r
- #define llson j<<1,l,mid
- #define rrson j<<1|1,mid+1,r
- #define INF 0x7fffffff
- typedef long long ll;
- typedef unsigned long long ull;
- using namespace std;
- #define maxn 20005
- struct
- {
- int v,w,c,next,re;
- //re记录逆边的下标,c是费用,w是流量
- } e[maxn];
- int n,cnt;
- int head[maxn],que[maxn*8],pre[maxn],dis[maxn];
- bool vis[maxn];
- void add(int u, int v, int w, int c)
- {
- e[cnt].v=v,e[cnt].w=w,e[cnt].c=c;
- e[cnt].next=head[u];
- e[cnt].re=cnt+1,head[u]=cnt++;
- e[cnt].v=u,e[cnt].w=0,e[cnt].c=-c;
- e[cnt].next=head[v];
- e[cnt].re=cnt-1,head[v]=cnt++;
- }
- bool spfa()
- {
- int i, l = 0, r = 1;
- for(i = 0; i <= n; i ++)
- dis[i] = INF,vis[i] = false;
- dis[0]=0,que[0]=0,vis[0]=true;
- while(l<r)
- {
- int u=que[l++];
- for(i=head[u];i!=-1;i=e[i].next)
- {
- int v = e[i].v;
- if(e[i].w&&dis[v]>dis[u]+e[i].c)
- {
- dis[v] = dis[u] + e[i].c;
- pre[v] = i;
- if(!vis[v])
- {
- vis[v] = true;
- que[r++] = v;
- }
- }
- }
- vis[u] = false;
- }
- return dis[n]!=INF;
- }
- int change()
- {
- int i,p,sum=INF,ans=0;
- for(i=n;i!=0;i=e[e[p].re].v)
- {//e[e[p].re].v为前向结点,不理解看add和spfa
- p=pre[i];//p为前向结点编号
- sum=min(sum,e[p].w);
- }
- for(i=n;i!=0;i=e[e[p].re].v)
- {
- p=pre[i];
- e[p].w-=sum;
- e[e[p].re].w+=sum;
- ans+=sum*e[p].c;//c记录的为单位流量费用,必须得乘以流量。
- }
- return ans;
- }
- int EK()
- {
- int sum=0;
- while(spfa()) sum+=change();
- return sum;
- }
- void init()
- {
- mem(head,-1),mem(pre,0),cnt=0;
- }
- struct abc
- {
- int u,v,w;
- }aa[405];
- int main()
- {
- //freopen("1.txt","r",stdin);
- int t;
- scanf("%d",&t);
- while(t--)
- {
- init();
- int N,K,i,j,x,y,tmp=0,a[402];
- map<int,int>Map;
- scanf("%d%d",&N,&K);
- for(i=0;i<N;i++)
- {
- scanf("%d%d%d",&aa[i].u,&aa[i].v,&aa[i].w);
- a[tmp++]=aa[i].u,a[tmp++]=aa[i].v;
- }
- sort(a,a+tmp);
- int k=unique(a,a+tmp)-a;
- for(i=0,tmp=0;i<k;i++)
- Map[a[i]]=++tmp;
- for(i=0;i<N;i++)
- {
- int u=Map[aa[i].u],v=Map[aa[i].v];
- add(u,v,1,-aa[i].w);
- }
- for(i=1;i<tmp;i++)
- add(i,i+1,K,0);
- n=tmp+1;
- add(0,1,K,0),add(tmp,n,K,0);
- printf("%d\n",-EK());
- }
- return 0;
- }
- POJ 3680 Intervals 费用流构图题(区间的一定考虑相邻的)
- poj 3680 Intervals(巧妙构图,费用流)
- POJ 3680 Intervals (费用流经典构图题)
- POJ 3680 Intervals 神奇的构图
- POJ 3680 Intervals 区间k覆盖 费用流
- POJ 3680 Intervals(最小费用流)
- POJ 3680 Intervals(经典费用流)
- POJ 3680 Intervals(最小费用流)
- poj 3680 Intervals 费用流
- poj 3680 Intervals 费用流
- poj 3680 Intervals(费用流)
- [费用流] POJ 3680 Intervals
- poj 3680 (费用流拆点+离散+神奇的构图)
- 2014湘潭全国邀请赛I题 Intervals /POJ 3680 / 在限制次数下取有权区间使权最大/小问题(费用流)
- poj 3680 Intervals 【坐标区间离散化 + 最大费用最大流】
- poj 3680 Intervals(最大费用流+离散化)
- POJ 3680 Intervals(费用流+离散化)
- Intervals (poj 3680 离散化+最小费用最大流)
- python使用模块chardet判断字符编码
- 进阶篇:3.10)铝型材(铝挤)件设计;
- 文本查询程序再探(C++primer5th)
- 热更新2
- Spring boot 打成jar包问题总结
- POJ 3680 Intervals 费用流构图题(区间的一定考虑相邻的)
- MYIR-ZYNQ7000系列-zturn教程(5):gpio_axi
- c++返回一个对象
- 团队开发Xcode8和Xcode9集成svn服务实现代码管理教程
- 获取鼠标位置(兼容火狐,IE)
- Codeforces Round #439 (Div. 2) C. The Intriguing Obsession(dp)
- Gradle加速编译
- 设计模式
- 使用darknet训练分类器