您的位置:首页 > 其它

POJ 3680 Intervals 费用流+离散化

2015-08-14 12:44 330 查看
Intervals

Time Limit: 5000MSMemory Limit: 65536K
Total Submissions: 6930Accepted: 2884
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个整数区间,每个区间有一个权值,从中取一些区间,使得任意整数点的重叠数不大于K,并且这些区间的总权值最大。
http://blog.csdn.net/waitfor_/article/details/7380406 引用这个题解

建图从0->1,1->2,~~~~~n->n+1每条边容量为k,费用为0,

对于每条线段,他的两个端点连边容量为1,费用-w

算法正确性证明:

如果两个区间没有交集,那么代表它们的边可以出现在同一增广路上,这一点显然。否则,它们就在不同的增广路上。每一个区间对应的边的容量都是1,这样,最后的流量就是被选出的两两有交集的区间的数量。受到(0,1,k,0)这条边的容量限制,这个值刚好不大于k.区间的权都是正的,因此选取的区间多多益善,所以流量必然最大。

(对于每次选取的增广路中总存在一个区间,在每次增广所得区间都与这个区间有交集)

#include <iostream>
#include <sstream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <functional>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <set>
#include <list>
#include <stack>
#define ALL(v) (v).begin(),(v).end()
#define foreach(i,v) for (__typeof((v).begin())i=(v).begin();i!=(v).end();i++)
#define SIZE(v) ((int)(v).size())
#define mem(a) memset(a,0,sizeof(a))
#define mem1(a) memset(a,-1,sizeof(a))
#define lp(k,a) for(int k=1;k<=a;k++)
#define lp0(k,a) for(int k=0;k<a;k++)
#define lpn(k,n,a) for(int k=n;k<=a;k++)
#define lpd(k,n,a) for(int k=n;k>=a;k--)
#define sc(a) scanf("%d",&a)
#define sc2(a,b) scanf("%d %d",&a,&b)
#define lowbit(x) (x&(-x))
#define ll int
#define pi pair<int,int>
#define vi vector<int>
#define PI acos(-1.0)
#define pb(a) push_back(a)
#define mp(a,b) make_pair(a,b)
#define TT cout<<"*****"<<endl;
#define TTT cout<<"********"<<endl;

using namespace std;
#define inf 0x3f3f3f3f
#define Inf 0x3FFFFFFFFFFFFFFFLL
#define N 5000
#define M 5000

struct Edge
{
ll to,cap,cost,nex;
Edge() {}
Edge(ll to,ll cap,ll cost,ll next):to(to),cap(cap),cost(cost),nex(next) {}
} edge[M];

struct{
int a,b,w;
}line[210];

ll head
,top;
ll D
,A
,P
,li
;
bool inq
;

void add(ll from,ll to,ll cap,ll cost)
{
edge[top]=Edge(to,cap,cost,head[from]);
head[from]=top++;
edge[top]=Edge(from,0,-cost,head[to]);
head[to]=top++;
}

bool spfa(ll s,ll t,ll &flow,ll &cost)
{
for(ll i=0;i<=t;i++) D[i]=inf;
mem(inq);
queue<ll>q;
q.push(s);
D[s]=0;
A[s]=inf;
while(!q.empty())
{
ll u=q.front();
q.pop();
inq[u]=0;
for(ll i=head[u];~i;i=edge[i].nex)
{
Edge &e=edge[i];
if(e.cap && D[e.to]>D[u]+e.cost)
{
D[e.to]=D[u]+e.cost;
P[e.to]=i;
A[e.to]=min(A[u],e.cap);
if(!inq[e.to])
{
inq[e.to]=1;
q.push(e.to);
}
}
}
}
if(D[t]==inf) return false;
cost+=D[t]*A[t];
flow+=A[t];
ll u=t;
while(u!=s)
{
edge[P[u]].cap-=A[t];
edge[P[u]^1].cap+=A[t];
u=edge[P[u]^1].to;
}
return true;
}

ll mcmf(ll s,ll t)
{
ll flow=0, cost=0;
while(spfa(s,t,flow,cost));
return cost;
}

int a
,S,T,n,m,k,u,v,w,cnt;
void init()
{
mem1(head);
cnt=0;
top=0;
lp(i,m)
{
sc2(u,v); sc(w);
li[cnt++]=u;
li[cnt++]=v;
line[i].a=u;
line[i].b=v;
line[i].w=w;
}

sort(li,li+cnt);
n=unique(li,li+cnt)-li;
S=0;
T=n+1;
lp(i,n+1)
add(i-1,i,k,0);

lp(i,m)
{
int x=lower_bound(li,li+n,line[i].a)-li+1;
int y=lower_bound(li,li+n,line[i].b)-li+1;
add(x,y,1,-line[i].w);
}
}

int main()
{
//freopen("in.txt","r",stdin);
int t;
sc(t);
while(t--)
{
scanf("%d%d",&m,&k);
init();
int cost=mcmf(S,T);
printf("%d\n",-cost);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: