Intervals
Time Limit: 5000MS
Memory Limit: 65536K
Total Submissions: 5456
Accepted: 2153
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
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个带权的开区间,第 i 个区间覆盖(ai, bi),权为wi。现在要你挑出一些区间使得总权值最大,并且满足实轴上任意一个点被覆盖不超过 K 次。 (1 <= K <= N
<= 200, 1 <= ai < bi <= 100,000, 1 <= wi <= 100,000)
题解:
经典构图题。先将所有区间端点离散化到整数1..M,另加源 s=0,汇 t=M+1;对
每个点 i (0 <= i <= M)加边(i, i+1, inf, 0);对每个区间(ai, bi)加边(ai’ , bi’ , 1, -wi),其中
ai’ , bi’分别表示 ai, bi 离散化后对应的数值。addedge(S,1,K,0),addedge(M,T,K,0),求一次最小费用流再取反即为结果。
如果区间被选上,流量从费用为负的边通过,否则从费用为零的边通过。。。
最后在联超级源和超级汇时,限制边选取的次数k。。。
代码:
[cpp]
view plain
copy
- #include<cstdio>
- #include<iostream>
- #include<algorithm>
- #include<queue>
- using namespace std;
- const int inf=1000000000;
- queue<int>q;
- int n,k,head[410],pree[410],pre[410],color[102010],dist[410];
- int hash[410],e=0,l=0,ll[410],rr[410],ww[410];
- int ans;
- bool vis[410];
- struct node
- {
- int u,v,c,w,next;
- }edge[16100];
- void addedge(int u,int v,int c,int w)
- {
- edge[l].u=u;edge[l].v=v;edge[l].c=c;edge[l].w=w;edge[l].next=head[u];head[u]=l++;
- edge[l].u=v;edge[l].v=u;edge[l].c=0;edge[l].w=-w;edge[l].next=head[v];head[v]=l++;
- }
- bool SPFA()
- {
- while(!q.empty())q.pop();
- memset(pre,0,sizeof(pre));
- memset(vis,0,sizeof(vis));
- memset(dist,0x3f,sizeof(dist));
- dist[0]=0;
- vis[0]=1;
- q.push(0);
- while(!q.empty())
- {
- int u=q.front();q.pop();
- vis[u]=0;
- for(int i=head[u];i!=-1;i=edge[i].next)
- {
- int v=edge[i].v;
- if(edge[i].c>0 && edge[i].w+dist[u]<dist[v])
- {
- dist[v]=edge[i].w+dist[u];
- pre[v]=i;
- if(!vis[v])
- {
- vis[v]=1;
- q.push(v);
- }
- }
- }
- }
- if(pre[1+e])return 1;else return 0;
- }
- void mcmf()
- {
- while(SPFA())
- {
- int u=e+1;
- int minn=inf;
- while(u!=0)
- {
- minn=min(minn,edge[pre[u]].c);
- u=edge[pre[u]].u;
- }
- u=e+1;
- while(u!=0)
- {
- edge[pre[u]].c-=minn;
- edge[pre[u]^1].c+=minn;
- u=edge[pre[u]].u;
- }
- ans+=minn*dist[e+1];
- }
- }
- void readdata()
- {
- freopen("poj3680.in","r",stdin);
- freopen("poj3680.out","w",stdout);
- int test;
- scanf("%d",&test);
- while(test--)
- {
- memset(head,255,sizeof(head));
- memset(color,0,sizeof(color));
- memset(pre,0,sizeof(pre));
- memset(hash,0,sizeof(hash));
- e=0;l=0;
- scanf("%d%d",&n,&k);
- for(int i=1;i<=n;i++)
- {
- scanf("%d%d%d",&ll[i],&rr[i],&ww[i]);
- color[ll[i]]=1; color[rr[i]]=1;
- }
- for(int j=1;j<=100010;j++)
- if(color[j]!=0)hash[++e]=j;
- for(int z=1;z<=n;z++)
- for(int i=1;i<=e;i++)
- if(ll[z]==hash[i])
- {
- for(int j=1;j<=e;j++)
- if(rr[z]==hash[j])
- addedge(i,j,1,-ww[z]);
- }
- addedge(0,1,k,0);
- for(int i=1;i<e;i++)
- addedge(i,i+1,k,0);
- addedge(e,e+1,k,0);
- ans=0;
- mcmf();
- cout<<-ans<<endl;
- }
- }
- int main()
- {
- readdata();
- return 0;
- }