您的位置:首页 > 其它

【POJ3680】【离散化+费用流 思维】 Intervals 区间图的最大权问题

2016-09-16 21:28 417 查看
传送门:POJ 3680

描述:

Intervals

Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 7719 Accepted: 3246
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次, 问你所能得到的最大权重是多少?

思路:

这题需要把区间端点离散化为n个点,让端点编号从1~n

建图:

1.添加源点s和汇点t。s向第一个点连边,容量为k,费用为0,第n个点向t连边,容量为k,费用为0;

2.第i个点向第i+1个点连边,容量为oo,费用为0;

3.把每个区间端点离散成a,b,则由a向b连边,容量为1,费用为-w(w为该区间的权值)。

最后最大费用只需要取相反数结果即可~~

代码:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;

//最小费用最大流,求最大费用只需要取相反数结果即可。
//点的总数为 N,点的编号 0~N
const int maxn=444;
const int maxm=4444;
const int inf=0x3f3f3f3f;

struct Edge{
int to,next,cap,flow,cost;
}es[maxm];
int head[maxn],tol;
int p[maxn];//记录增广路径上 到达点i的边的编号
int d[maxn];//单位总费用
bool vis[maxn];
int N;//节点总个数
int a[maxn],b[maxn],w[maxn];
int li[maxn],x[maxn],cnt;
int n,k;

void init(int n){
N=n;
tol=0;
memset(head, -1, sizeof(head));
}

void addedge(int u,int v,int cap,int cost){
es[tol].to=v;
es[tol].cap=cap;
es[tol].cost=cost;
es[tol].flow=0;
es[tol].next=head[u];
head[u]=tol++;
es[tol].to=u;
es[tol].cap=0;
es[tol].cost=-cost;
es[tol].flow=0;
es[tol].next=head[v];
head[v]=tol++;
}

bool spfa(int s,int t){//寻找花销最少的路径
//跑一遍SPFA 找s——t的最少花销路径 且该路径上每一条边不能满流
//若存在 说明可以继续增广,反之不能
queue<int>q;
for(int i=0; i<=N; i++){
d[i]=inf;
vis[i]=false;
p[i]=-1;
}
d[s]=0;
vis[s]=true;
q.push(s);
while(!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;
for(int i=head[u]; i!=-1; i=es[i].next){ //逆向枚举以u为起点的边
int v=es[i].to;
if(es[i].cap>es[i].flow && d[v]>d[u]+es[i].cost){//可以松弛 且 没有满流
d[v]=d[u]+es[i].cost;
p[v]=i; //记录前驱边 的编号
if(!vis[v]){ vis[v]=true; q.push(v);}
}
}
}
return p[t]!=-1;//可达返回true
}

//返回的是最大流,cost存的是最小费用
void MCMF(int s,int t,int &cost){
int flow = 0;cost = 0;
while(spfa(s,t)){//每次寻找花销最小的路径
int Min=inf;
//通过反向弧 在源点到汇点的最少花费路径 找最小增广流
for(int i=p[t]; i!=-1; i=p[es[i^1].to]){
if(Min>es[i].cap-es[i].flow)
Min=es[i].cap-es[i].flow;
}
//增广
for(int i=p[t]; i!=-1; i=p[es[i^1].to]){
es[i].flow+=Min;
es[i^1].flow-=Min;
cost+=es[i].cost*Min;//增广流的花销
}
flow+=Min;//总流量累加
}
}

int lisan(){//离散化模板
for(int i=1; i<=2*n; i++)li[i]=x[i];
sort(li+1, li+2*n+1);//unique的作用是“去掉”容器中相邻元素的重复元素,它实质上是一个伪去除,
int m=unique(li+1, li+2*n+1)-li-1;//它会把重复的元素添加到容器末尾,而返回值是去重之后的尾地址
for(int i=1; i<=2*n; i++)x[i]=lower_bound(li+1, li+m+1, x[i])-li;//一般使用前需要对容器进行排序
for(int i=1; i<=n; i++)a[i]=lower_bound(li+1, li+m+1, a[i])-li;
for(int i=1; i<=n; i++)b[i]=lower_bound(li+1, li+m+1, b[i])-li;
return m;
}//m等于去重数组的大小

int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
cnt=0;
for(int i=1; i<=n; i++){
scanf("%d%d%d",&a[i],&b[i],&w[i]);
x[++cnt]=a[i];
x[++cnt]=b[i];
}
cnt=lisan();
init(cnt+1);
addedge(0, 1, k, 0);
addedge(cnt, cnt+1, k, 0);
for(int i=1; i<cnt; i++)
addedge(i, i+1, inf, 0);
for(int i=1; i<=n; i++)
addedge(a[i], b[i], 1, -w[i]);
int cost;
MCMF(0, cnt+1, cost);
printf("%d\n",-cost);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  费用流 poj 离散化
相关文章推荐