Intervals
Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 8587 | Accepted: 3662 |
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
题意:给你N个区间段的(a,b)和价值,让你在不相交的情况下求m次求得最大值。
题解:先将每个点都离散化,然后依次添加INF的边,然后输入m条边,用lower_bound找到x,y相应下标,然后加边权值为这个区间段的相反数,跑最小费用流就可以了
题解这里
#include<cstdio> #include<cstring> #include<algorithm> #include<queue> using namespace std; const int N=500; const int M=1e4+88; const int INF=0x3f3f3f3f; int head[N],tot,pre[N],C[N],F[N],V[N],n,m; struct node{ int u,v,flow,cost,next; }e[M]; void add(int u,int v,int flow,int cost){ e[tot].u=u;e[tot].v=v;e[tot].flow=flow;e[tot].cost=cost;e[tot].next=head[u];head[u]=tot++; e[tot].u=v;e[tot].v=u;e[tot].flow=0;e[tot].cost=-cost;e[tot].next=head[v];head[v]=tot++; } int SPFA(int s,int t){ memset(pre,-1,sizeof(pre)); for(int i=1;i<=t+1;++i) F[i]=0,C[i]=INF,V[i]=0; queue<int>Q; Q.push(s); C[0]=0,F[0]=INF,V[0]=1; while(!Q.empty()){ int u=Q.front(); Q.pop(); V[u]=0; for(int i=head[u];~i;i=e[i].next){ int v=e[i].v,f=e[i].flow,c=e[i].cost; if(f>0&&C[v]>C[u]+c) { C[v]=C[u]+c; pre[v]=i; F[v]=min(f,F[u]); if(!V[v]) V[v]=1,Q.push(v); } } } return F[t]&&C[t]!=0; } int MCMF(int s,int t){ int ans=0,temp; while(temp=SPFA(s,t)){ for(int i=pre[t];~i;i=pre[e[i].u]) { ans+=temp*e[i].cost; e[i].flow-=temp; e[i^1].flow+=temp; } } return ans; } struct point{ int x,y,val; }Po[N]; int ar[N]; int main(){ int T; for(scanf("%d",&T);T--;){ scanf("%d%d",&n,&m); tot=0; memset(head,-1,sizeof(head)); int ct=0; for(int i=1;i<=n;++i) {scanf("%d%d%d",&Po[i].x,&Po[i].y,&Po[i].val); ar[++ct]=Po[i].x,ar[++ct]=Po[i].y; } sort(ar+1,ar+ct+1); int num=2; for(int i=2;i<=ct;++i) { while(ar[i]==ar[num-1]&&i<ct) ++i; if(i<=ct) ar[num++]=ar[i]; } for(int i=2;i<=num;++i) add(i-1,i,INF,0); add(0,1,m,0); add(num,num+1,m,0); for(int i=1;i<=n;++i) { int l=lower_bound(ar+1,ar+num+1,Po[i].x)-ar; int r=lower_bound(ar+1,ar+num+1,Po[i].y)-ar; add(l,r,1,-Po[i].val); } printf("%d ",-MCMF(0,num+1)); } }