POJ3680——Intervals(费用流)

来源:互联网 发布:树木每年被砍伐的数据 编辑:程序博客网 时间:2024/06/09 21:27
Intervals
Time Limit: 5000MS  Memory Limit: 65536K
Total Submissions: 5456Accepted: 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 plaincopy
  1. #include<cstdio>  
  2. #include<iostream>  
  3. #include<algorithm>  
  4. #include<queue>  
  5. using namespace std;  
  6. const int inf=1000000000;  
  7. queue<int>q;  
  8.   
  9. int n,k,head[410],pree[410],pre[410],color[102010],dist[410];  
  10. int hash[410],e=0,l=0,ll[410],rr[410],ww[410];  
  11. int ans;  
  12. bool vis[410];  
  13. struct node  
  14. {  
  15.     int u,v,c,w,next;  
  16. }edge[16100];  
  17.   
  18.   
  19. void addedge(int u,int v,int c,int w)  
  20. {  
  21.     edge[l].u=u;edge[l].v=v;edge[l].c=c;edge[l].w=w;edge[l].next=head[u];head[u]=l++;  
  22.     edge[l].u=v;edge[l].v=u;edge[l].c=0;edge[l].w=-w;edge[l].next=head[v];head[v]=l++;  
  23. }  
  24.   
  25. bool SPFA()  
  26. {  
  27.     while(!q.empty())q.pop();  
  28.     memset(pre,0,sizeof(pre));  
  29.     memset(vis,0,sizeof(vis));  
  30.     memset(dist,0x3f,sizeof(dist));  
  31.     dist[0]=0;  
  32.     vis[0]=1;  
  33.     q.push(0);  
  34.     while(!q.empty())  
  35.     {  
  36.         int u=q.front();q.pop();  
  37.         vis[u]=0;  
  38.         for(int i=head[u];i!=-1;i=edge[i].next)  
  39.         {  
  40.             int v=edge[i].v;  
  41.             if(edge[i].c>0 && edge[i].w+dist[u]<dist[v])  
  42.             {  
  43.                 dist[v]=edge[i].w+dist[u];  
  44.                 pre[v]=i;  
  45.                 if(!vis[v])  
  46.                 {  
  47.                     vis[v]=1;  
  48.                     q.push(v);  
  49.                 }  
  50.             }  
  51.         }  
  52.     }  
  53.     if(pre[1+e])return 1;else return 0;  
  54. }  
  55.   
  56. void mcmf()  
  57. {  
  58.     while(SPFA())  
  59.     {  
  60.         int u=e+1;  
  61.         int minn=inf;  
  62.         while(u!=0)  
  63.         {  
  64.             minn=min(minn,edge[pre[u]].c);  
  65.             u=edge[pre[u]].u;  
  66.         }  
  67.         u=e+1;  
  68.         while(u!=0)  
  69.         {  
  70.             edge[pre[u]].c-=minn;  
  71.             edge[pre[u]^1].c+=minn;  
  72.             u=edge[pre[u]].u;  
  73.         }  
  74.         ans+=minn*dist[e+1];  
  75.     }  
  76. }  
  77.   
  78. void readdata()  
  79. {  
  80.     freopen("poj3680.in","r",stdin);  
  81.     freopen("poj3680.out","w",stdout);  
  82.     int test;  
  83.     scanf("%d",&test);  
  84.     while(test--)  
  85.     {  
  86.         memset(head,255,sizeof(head));  
  87.         memset(color,0,sizeof(color));  
  88.         memset(pre,0,sizeof(pre));  
  89.         memset(hash,0,sizeof(hash));  
  90.         e=0;l=0;  
  91.         scanf("%d%d",&n,&k);  
  92.         for(int i=1;i<=n;i++)  
  93.         {  
  94.             scanf("%d%d%d",&ll[i],&rr[i],&ww[i]);  
  95.             color[ll[i]]=1; color[rr[i]]=1;  
  96.         }  
  97.         for(int j=1;j<=100010;j++)  
  98.             if(color[j]!=0)hash[++e]=j;  
  99.         for(int z=1;z<=n;z++)  
  100.             for(int i=1;i<=e;i++)  
  101.                 if(ll[z]==hash[i])  
  102.                 {  
  103.                     for(int j=1;j<=e;j++)  
  104.                         if(rr[z]==hash[j])  
  105.                             addedge(i,j,1,-ww[z]);  
  106.                 }  
  107.         addedge(0,1,k,0);  
  108.         for(int i=1;i<e;i++)  
  109.         addedge(i,i+1,k,0);  
  110.         addedge(e,e+1,k,0);  
  111.         ans=0;  
  112.         mcmf();  
  113.         cout<<-ans<<endl;  
  114.     }  
  115. }  
  116.   
  117. int main()  
  118. {  
  119.     readdata();  
  120.     return 0;  
  121. }  

0 0
原创粉丝点击