POJ 3680 Intervals 费用流+离散化
2015-08-14 12:44
330 查看
Intervals
Description
You are given N weighted open intervals. The ith interval covers (ai,
bi) and weighs wi. 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 and K (1 ≤
K ≤ N ≤ 200).
The next N line each contain three integers ai,
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
Sample Output
Source
POJ Founder Monthly Contest – 2008.07.27, windy7926778
引用一个题解
有N个整数区间,每个区间有一个权值,从中取一些区间,使得任意整数点的重叠数不大于K,并且这些区间的总权值最大。
http://blog.csdn.net/waitfor_/article/details/7380406 引用这个题解
建图从0->1,1->2,~~~~~n->n+1每条边容量为k,费用为0,
对于每条线段,他的两个端点连边容量为1,费用-w
算法正确性证明:
如果两个区间没有交集,那么代表它们的边可以出现在同一增广路上,这一点显然。否则,它们就在不同的增广路上。每一个区间对应的边的容量都是1,这样,最后的流量就是被选出的两两有交集的区间的数量。受到(0,1,k,0)这条边的容量限制,这个值刚好不大于k.区间的权都是正的,因此选取的区间多多益善,所以流量必然最大。
(对于每次选取的增广路中总存在一个区间,在每次增广所得区间都与这个区间有交集)
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 6930 | Accepted: 2884 |
You are given N weighted open intervals. The ith interval covers (ai,
bi) and weighs wi. 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 and K (1 ≤
K ≤ N ≤ 200).
The next N line each contain three integers ai,
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
4 3 1 1 2 2 2 3 4 3 4 8 3 1 1 3 2 2 3 4 3 4 8 3 1 1 100000 100000 1 2 3 100 200 300 3 2 1 100000 100000 1 150 301 100 200 300
Sample Output
14 12 100000 100301
Source
POJ Founder Monthly Contest – 2008.07.27, windy7926778
引用一个题解
有N个整数区间,每个区间有一个权值,从中取一些区间,使得任意整数点的重叠数不大于K,并且这些区间的总权值最大。
http://blog.csdn.net/waitfor_/article/details/7380406 引用这个题解
建图从0->1,1->2,~~~~~n->n+1每条边容量为k,费用为0,
对于每条线段,他的两个端点连边容量为1,费用-w
算法正确性证明:
如果两个区间没有交集,那么代表它们的边可以出现在同一增广路上,这一点显然。否则,它们就在不同的增广路上。每一个区间对应的边的容量都是1,这样,最后的流量就是被选出的两两有交集的区间的数量。受到(0,1,k,0)这条边的容量限制,这个值刚好不大于k.区间的权都是正的,因此选取的区间多多益善,所以流量必然最大。
(对于每次选取的增广路中总存在一个区间,在每次增广所得区间都与这个区间有交集)
#include <iostream> #include <sstream> #include <cstdio> #include <cstring> #include <algorithm> #include <functional> #include <cmath> #include <vector> #include <queue> #include <map> #include <set> #include <list> #include <stack> #define ALL(v) (v).begin(),(v).end() #define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++) #define SIZE(v) ((int)(v).size()) #define mem(a) memset(a,0,sizeof(a)) #define mem1(a) memset(a,-1,sizeof(a)) #define lp(k,a) for(int k=1;k<=a;k++) #define lp0(k,a) for(int k=0;k<a;k++) #define lpn(k,n,a) for(int k=n;k<=a;k++) #define lpd(k,n,a) for(int k=n;k>=a;k--) #define sc(a) scanf("%d",&a) #define sc2(a,b) scanf("%d %d",&a,&b) #define lowbit(x) (x&(-x)) #define ll int #define pi pair<int,int> #define vi vector<int> #define PI acos(-1.0) #define pb(a) push_back(a) #define mp(a,b) make_pair(a,b) #define TT cout<<"*****"<<endl; #define TTT cout<<"********"<<endl; using namespace std; #define inf 0x3f3f3f3f #define Inf 0x3FFFFFFFFFFFFFFFLL #define N 5000 #define M 5000 struct Edge { ll to,cap,cost,nex; Edge() {} Edge(ll to,ll cap,ll cost,ll next):to(to),cap(cap),cost(cost),nex(next) {} } edge[M]; struct{ int a,b,w; }line[210]; ll head ,top; ll D ,A ,P ,li ; bool inq ; void add(ll from,ll to,ll cap,ll cost) { edge[top]=Edge(to,cap,cost,head[from]); head[from]=top++; edge[top]=Edge(from,0,-cost,head[to]); head[to]=top++; } bool spfa(ll s,ll t,ll &flow,ll &cost) { for(ll i=0;i<=t;i++) D[i]=inf; mem(inq); queue<ll>q; q.push(s); D[s]=0; A[s]=inf; while(!q.empty()) { ll u=q.front(); q.pop(); inq[u]=0; for(ll i=head[u];~i;i=edge[i].nex) { Edge &e=edge[i]; if(e.cap && D[e.to]>D[u]+e.cost) { D[e.to]=D[u]+e.cost; P[e.to]=i; A[e.to]=min(A[u],e.cap); if(!inq[e.to]) { inq[e.to]=1; q.push(e.to); } } } } if(D[t]==inf) return false; cost+=D[t]*A[t]; flow+=A[t]; ll u=t; while(u!=s) { edge[P[u]].cap-=A[t]; edge[P[u]^1].cap+=A[t]; u=edge[P[u]^1].to; } return true; } ll mcmf(ll s,ll t) { ll flow=0, cost=0; while(spfa(s,t,flow,cost)); return cost; } int a ,S,T,n,m,k,u,v,w,cnt; void init() { mem1(head); cnt=0; top=0; lp(i,m) { sc2(u,v); sc(w); li[cnt++]=u; li[cnt++]=v; line[i].a=u; line[i].b=v; line[i].w=w; } sort(li,li+cnt); n=unique(li,li+cnt)-li; S=0; T=n+1; lp(i,n+1) add(i-1,i,k,0); lp(i,m) { int x=lower_bound(li,li+n,line[i].a)-li+1; int y=lower_bound(li,li+n,line[i].b)-li+1; add(x,y,1,-line[i].w); } } int main() { //freopen("in.txt","r",stdin); int t; sc(t); while(t--) { scanf("%d%d",&m,&k); init(); int cost=mcmf(S,T); printf("%d\n",-cost); } return 0; }
相关文章推荐
- nginx平台 (类似Apache 服务器)
- 16进制颜色代码(完全)
- 每周4天班,年赚数百万美金:一家小公司的极简经商之道
- 身边到处都是“死神来了”
- ACM/ICPC 区预赛现场VIM配置
- Replacement
- Replication复制
- JAVA在IO流量汇总
- POJ 1488 TEX Quotes(字符串)
- iOS开发之监听键盘高度的变化
- 微信,已经严重地搅和了我们的生活!
- SQLServer数据库存储过程教程
- sql去除html标签
- 平衡二叉树的调整
- 天融信AlphaFuzzer测试工具 使用教程
- Oracle Recovery 02 - 常规恢复之不完全恢复
- Volley详解(三)——响应(Response)
- java对象与xml格式字符串的转换
- Gym - 100625F Count Ways 快速幂+容斥原理
- 以色列Aladdin HASP SRM(AES-128)加密狗破解经验分享