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);




涉及到区间的一定要考虑相邻数字的连接问题

  1. #include<iostream>  
  2. #include<cstdio>  
  3. #include<cstring>  
  4. #include<algorithm>  
  5. #include<map>  
  6. #include<queue>  
  7. #include<set>  
  8. #include<cmath>  
  9. #include<bitset>  
  10. #define mem(a,b) memset(a,b,sizeof(a))  
  11. #define lson i<<1,l,mid  
  12. #define rson i<<1|1,mid+1,r  
  13. #define llson j<<1,l,mid  
  14. #define rrson j<<1|1,mid+1,r  
  15. #define INF 0x7fffffff  
  16. typedef long long ll;  
  17. typedef unsigned long long ull;  
  18. using namespace std;  
  19. #define maxn 20005  
  20. struct  
  21. {  
  22.     int v,w,c,next,re;  
  23.     //re记录逆边的下标,c是费用,w是流量  
  24. } e[maxn];  
  25. int n,cnt;  
  26. int head[maxn],que[maxn*8],pre[maxn],dis[maxn];  
  27. bool vis[maxn];  
  28. void add(int u, int v, int w, int c)  
  29. {  
  30.     e[cnt].v=v,e[cnt].w=w,e[cnt].c=c;  
  31.     e[cnt].next=head[u];  
  32.     e[cnt].re=cnt+1,head[u]=cnt++;  
  33.     e[cnt].v=u,e[cnt].w=0,e[cnt].c=-c;  
  34.     e[cnt].next=head[v];  
  35.     e[cnt].re=cnt-1,head[v]=cnt++;  
  36. }  
  37. bool spfa()  
  38. {  
  39.     int i, l = 0, r = 1;  
  40.     for(i = 0; i <= n; i ++)  
  41.         dis[i] = INF,vis[i] = false;  
  42.     dis[0]=0,que[0]=0,vis[0]=true;  
  43.     while(l<r)  
  44.     {  
  45.         int u=que[l++];  
  46.         for(i=head[u];i!=-1;i=e[i].next)  
  47.         {  
  48.             int v = e[i].v;  
  49.             if(e[i].w&&dis[v]>dis[u]+e[i].c)  
  50.             {  
  51.                 dis[v] = dis[u] + e[i].c;  
  52.                 pre[v] = i;  
  53.                 if(!vis[v])  
  54.                 {  
  55.                     vis[v] = true;  
  56.                     que[r++] = v;  
  57.                 }  
  58.             }  
  59.         }  
  60.         vis[u] = false;  
  61.     }  
  62.     return dis[n]!=INF;  
  63. }  
  64. int change()  
  65. {  
  66.     int i,p,sum=INF,ans=0;  
  67.     for(i=n;i!=0;i=e[e[p].re].v)  
  68.     {//e[e[p].re].v为前向结点,不理解看add和spfa  
  69.         p=pre[i];//p为前向结点编号  
  70.         sum=min(sum,e[p].w);  
  71.     }  
  72.     for(i=n;i!=0;i=e[e[p].re].v)  
  73.     {  
  74.         p=pre[i];  
  75.         e[p].w-=sum;  
  76.         e[e[p].re].w+=sum;  
  77.         ans+=sum*e[p].c;//c记录的为单位流量费用,必须得乘以流量。  
  78.     }  
  79.     return ans;  
  80. }  
  81. int EK()  
  82. {  
  83.     int sum=0;  
  84.     while(spfa()) sum+=change();  
  85.     return sum;  
  86. }  
  87. void init()  
  88. {  
  89.     mem(head,-1),mem(pre,0),cnt=0;  
  90. }  
  91. struct abc  
  92. {  
  93.     int u,v,w;  
  94. }aa[405];  
  95. int main()  
  96. {  
  97.     //freopen("1.txt","r",stdin);  
  98.     int t;  
  99.     scanf("%d",&t);  
  100.     while(t--)  
  101.     {  
  102.         init();  
  103.         int N,K,i,j,x,y,tmp=0,a[402];  
  104.         map<int,int>Map;  
  105.         scanf("%d%d",&N,&K);  
  106.         for(i=0;i<N;i++)  
  107.         {  
  108.             scanf("%d%d%d",&aa[i].u,&aa[i].v,&aa[i].w);  
  109.             a[tmp++]=aa[i].u,a[tmp++]=aa[i].v;  
  110.         }  
  111.         sort(a,a+tmp);  
  112.         int k=unique(a,a+tmp)-a;  
  113.         for(i=0,tmp=0;i<k;i++)  
  114.             Map[a[i]]=++tmp;  
  115.         for(i=0;i<N;i++)  
  116.         {  
  117.             int u=Map[aa[i].u],v=Map[aa[i].v];  
  118.             add(u,v,1,-aa[i].w);  
  119.         }  
  120.         for(i=1;i<tmp;i++)  
  121.             add(i,i+1,K,0);  
  122.         n=tmp+1;  
  123.         add(0,1,K,0),add(tmp,n,K,0);  
  124.         printf("%d\n",-EK());  
  125.     }  
  126.     return 0;