POJ 3680 Intervals

Intervals
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7424 Accepted: 3100

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 ≤ KN ≤ 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


【题目分析】

    一看到巨大的ai,bi和超级小的n,k就知道有必要进行离散化(就是因为离散化出错,RE了好多遍,有可能并没有重复,需要特判一下)。然后就不会做了,关键是每一个点覆盖不能超过k次,用网络流的方法确实是很巧妙的,加入(S,1,k,0)(T-1 ,T ,k ,0 )(ai,bi,1,-wi)(i,i+1,k,0)如果一个区间被选择,那么这个区间可能通过的流量就减去了1(S点出来的流量为k,那么最大流就是k)然后就轻而易举的解决了。


【代码】

#include <cstdio>
#include <cstring>
#include <string>
#include <queue>
#include <algorithm>
using namespace std;
const int inf =0x3f3f3f3f;
const int maxn=10010;
const int maxm=10010;
#define M(a) memset(a,-1,sizeof a)
#define M0(a) memset(a,0,sizeof a)
#define MB(a) memset(a,0x3f,sizeof a)
int n,m,S,T,ans,en=0;
int v[maxn],u[maxn],cost[maxn],f[maxn],h[maxn],ne[maxn];
bool vis[maxm];//u->v
int l[maxn],r[maxn],w[maxn],ss[maxn];
int dis[maxm],with[maxm],minn[maxm];
inline void add(int a,int b,int r,int c)
{
	u[en]=a;v[en]=b;ne[en]=h[a];f[en]=r;cost[en]=c; h[a]=en++;
	u[en]=b;v[en]=a;ne[en]=h[b];f[en]=0;cost[en]=-c;h[b]=en++;
}
inline bool tell()
{
queue<int>q;
	M0(vis);MB(dis);MB(minn);M0(with);
	q.push(S);vis[S]=true;dis[S]=0;
	while (!q.empty())
	{
		int x=q.front();q.pop();vis[x]=false;
		for (int i=h[x];i!=-1;i=ne[i])
		{
			int y=v[i];
			if (dis[y]>dis[x]+cost[i]&&f[i]>0)
			{
				dis[y]=dis[x]+cost[i];
				minn[y]=min(minn[x],f[i]);
				with[y]=i;
				if (!vis[y]) vis[y]=1,q.push(y);
			}
		}
	}
	if (dis[T]==0x3f3f3f3f) return false;
	return true;
}
inline int zeng()
{
	for (int i=T;i!=S;i=v[with[i]^1])
	{
		f[with[i]]-=minn[T];
		f[with[i]^1]+=minn[T];
	}
	return minn[T]*dis[T];
}
inline void solve()
{
	M(v);M(u);M(h);M(ne);
	int n,k;
	scanf("%d%d",&n,&k);
	S=0;en=0;ans=0;
	for (int i=1;i<=n;++i){
		scanf("%d%d%d",&l[i],&r[i],&w[i]);
		ss[i*2-1]=l[i];
		ss[i*2]=r[i];
	}
	sort(ss+1,ss+2*n+1);
	for (int i=2*n;i>1;--i) if (ss[i]==ss[i-1]) ss[i]=inf;
	sort(ss+1,ss+2*n+1);
	int flag=0;
	for (int i=1;i<=2*n;i++)
	{
		if (ss[i]==inf) {T=i+1;flag=1;break;}
	}
	if (!flag) T=2*n+1;
	for (int i=1;i<T;++i)add(i,i+1,k,0);
	for (int i=1;i<=n;++i){
		for (int j=1;j<T;++j) if (l[i]==ss[j]) {l[i]=j;break;}
		for (int j=1;j<T;++j) if (r[i]==ss[j]) {r[i]=j;break;}
	}
	for (int i=1;i<=n;++i){
		add(l[i],r[i],1,-w[i]);
	}
	add(S,1,k,0);
	add(T-1,T,k,0);
	while (tell()) ans-=zeng();
	printf("%d\n",ans);
}
int main()
{
	int tt=0;
	scanf("%d",&tt);
	for (int z=1;z<=tt;++z) solve();
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值