Time Limit: 5000MS | Memory Limit: 65536K | |
Total Submissions: 7588 | Accepted: 3187 |
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个区间,每个区间有个权值,现在问从中取出一些区间求权值和最大,每一个点的覆盖次数都不能超过m
思路:开始看错题意了,以为一个区间可以覆盖多次,建图一直建不出来。后来才发现原来一个区间只能覆盖一次,那么我们可以这么建图。
把区间里的所有端点存下来离散化排序去重,这样就变成x轴上的一些离散的点,然后让源点连接最左边容量为m费用为0,每一个点都跟后一个端点连一条边,容量为m,费用为0,最后一个点跟汇点连一条边,容量为m费用为0,这样就可以解决不管怎么流单个点都绝对不会超过m次的问题了,那么如何解决取一些区间权值和最大呢
我们假设一个区间a,b,现在找到a和b对应的离散化后的下标,然后连一条线,容量为1,(表示只能取一次),费用为wi
这样跑一遍最大费用最大流就是答案了。 具体在纸上画一画应该就能看懂了。
代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
using namespace std;
#define N 1010
#define INF 0x3f3f3f3f
struct Edge
{
int from,to,next,cap,cost;///起点,终点,同起点下一条边,残余流量,费用
} edge[N*N];
struct E
{
int u,v,w;
} e[N];
int cnt,head[N],sum[N];
int vis[N],d[N],pp[N],f[N];
int sumflow;///最大流量总和
void init()
{
cnt=0;
memset(head,-1,sizeof(head));
}
void addedge(int from,int to,int cap,int cost)
{
edge[cnt].from=from;
edge[cnt].to=to;
edge[cnt].cost=cost;
edge[cnt].cap=cap;
edge[cnt].next=head[from];
head[from]=cnt++;
edge[cnt].from=to;
edge[cnt].to=from;
edge[cnt].cost=-cost;
edge[cnt].cap=0;
edge[cnt].next=head[to];
head[to]=cnt++;///存反向边
}
int spfa(int s,int t,int n)
{
queue<int>q;
memset(vis,0,sizeof(vis));
memset(pp,-1,sizeof(pp));///pp[i]表示最短路径上以i为终点的边的编号
for(int i=0; i<=n; i++)
d[i]=-INF;
d[s]=0;
vis[s]=1;
q.push(s);
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].to;
if(edge[i].cap>0&&d[v]<d[u]+edge[i].cost)
{
d[v]=d[u]+edge[i].cost;
pp[v]=i;
if(!vis[v])
{
vis[v]=1;
q.push(v);
}
}
}
}
if(d[t]==-INF) return 0;///找不到一条到终点的路
return 1;
}
int MCMF(int s,int t,int n)
{
int mincost=0,minflow,flow=0;///最小费用,路径中最小流量,总流量
while(spfa(s,t,n))///找当前的最短路
{
minflow=INF+1;
for(int i=pp[t]; i!=-1; i=pp[edge[i].from])
minflow=min(minflow,edge[i].cap);///从路径中找最小的流量
flow+=minflow;///总流量加上最小流量
for(int i=pp[t]; i!=-1; i=pp[edge[i].from])
{
edge[i].cap-=minflow;///当前边减去最小流量
edge[i^1].cap+=minflow;///反向边加上最小流量
}
mincost+=d[t]*minflow;///最小费用等于路径和*每条路径的流量(经过多少次)
}
sumflow=flow;
return mincost;
}
int main()
{
int T,n,m,k;
scanf("%d",&T);
while(T--)
{
init();
scanf("%d %d",&n,&m);
int num=0;
for(int i=1; i<=n; i++)
{
scanf("%d %d %d",&e[i].u,&e[i].v,&e[i].w);
f[++num]=e[i].u;
f[++num]=e[i].v;
}
sort(f+1,f+1+num);
int tot=1;
for(int i=2; i<=num; i++)
if(f[i]!=f[i-1])
f[++tot]=f[i];
int s=0,t=tot+1;
addedge(s,1,m,0);
for(int i=1;i<tot;i++)
addedge(i,i+1,m,0);
for(int i=1;i<=n;i++)
{
int u=lower_bound(f+1,f+1+tot,e[i].u)-f;
int v=lower_bound(f+1,f+1+tot,e[i].v)-f;
addedge(u,v,1,e[i].w);
}
addedge(tot,t,m,0);
printf("%d\n",MCMF(s,t,t));
}
return 0;
}