POJ3680 Intervals 离散化+最小费用流

6 篇文章 0 订阅
5 篇文章 0 订阅
Intervals
Time Limit: 5000MS      Memory Limit: 65536K
Total Submissions: 7858     Accepted: 3311

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 

将每个区间的左端点 连接一条费用为-w,流量为1的边 到 右端点
对所有大于i的点j 连一条费用为0,流量为INF的边
S连一条0费用,k流量的边到1
最后一个点 连一条0费用,k流量的边到t

但是考虑到a,b<1e5,显然会超时 还要优化
注意到总区间数只有200个 ,对所有点进行离散化 点数就简化为400个
而且对所有大于i的点j 连一条费用为0,流量为INF的边 其实等价与对所有i 连一条费用为0,流量为INF的边到i+1

#include<iostream>
#include<stdlib.h>
#include<stdio.h>
#include<string>
#include<vector>
#include<deque>
#include<queue>
#include<algorithm>
#include<set>
#include<map>
#include<stack>
#include<time.h>
#include<math.h>
#include<list>
#include<cstring>
#include<fstream>
//#include<memory.h>
using namespace std;
#define ll long long
#define ull unsigned long long
#define pii pair<int,int>
#define INF 1000000007
#define pll pair<ll,ll>
#define pid pair<int,double>

const int N = 500;
const int M = N*10;

struct Edge{
    int to,c,w,next;
}edge[M];
int head[N];

int nume=0;
inline void addEdge(int k,int u,int v,int c,int w){
    edge[k].to=v,
            edge[k].c=c,
            edge[k].w=w,
            edge[k].next=head[u];
    head[u]=k;
}

inline void addEdge(int u,int v,int c,int w){
    addEdge(nume++,u,v,c,w);
    addEdge(nume++,v,u,0,-w);
}

void init(int n){
    fill(head,head+n+1,-1);
    nume=0;
}

bool used[N];
int dis[N],load[N],p[N];//距离 ,前驱边,前驱点
bool spfa(int s,int e,int n){
    deque<int>que;
    for(int i=0;i<=n;++i){
        dis[i]=INF;
        load[i]=p[i]=-1;
        used[i]=false;
    }
    que.push_back(s);
    dis[s]=0;
    used[s]=true;
    while(!que.empty()){
        int u=que.front();
        que.pop_front();
        used[u]=false;
        for(int i=head[u];i!=-1;i=edge[i].next){
            if(edge[i].c>0){
                int v=edge[i].to;
                if(dis[v]>dis[u]+edge[i].w){
                    dis[v]=dis[u]+edge[i].w;
                    p[v]=u;
                    load[v]=i;
                    if(used[v]==false){
                        used[v]=true;
                        que.push_back(v);
                    }
                }
            }
        }
    }
    return dis[e]!=INF;
}

int min_cost_flow(int s,int t,int n){
    int ansflow=0,anscost=0;
    while(spfa(s,t,n)){
        int u=t;
        int f=INF;
        while(p[u]!=-1){
            f=min(f,edge[load[u]].c);
            u=p[u];
        }
        u=t;
        while(p[u]!=-1){
            edge[load[u]].c-=f;
            edge[load[u]^1].c+=f;
            u=p[u];
        }
        anscost+=dis[t]*f;
        ansflow+=f;
    }
    return anscost;
}

map<int,int>myHash;

pii line[N];
int w[N];

void slove(int n,int k){
    myHash.clear();
    for(int i=0;i<n;++i){
        scanf("%d%d%d",&line[i].first,&line[i].second,&w[i]);
        myHash[line[i].first],myHash[line[i].second];
    }
    int nn=myHash.size(),s=nn+1,t=s+1;
    {
        int tmp=1;
        for(map<int,int>::iterator it=myHash.begin();it!=myHash.end();++it){
            it->second=tmp++;
        }
    }
    init(t);
    for(int i=0;i<n;++i){
        int u=myHash[line[i].first],v=myHash[line[i].second];
        addEdge(u,v,1,-w[i]);
    }
    for(int i=1;i<nn;++i){
        addEdge(i,i+1,INF,0);
    }
    addEdge(s,1,k,0);
    addEdge(nn,t,k,0);
    printf("%d\n",-min_cost_flow(s,t,t));
}

int main()
{
    //freopen("/home/lu/Documents/r.txt","r",stdin);
    //freopen("/home/lu/Documents/w.txt","w",stdout);
    int T;
    scanf("%d",&T);
    while(T--){
        int n,k;
        scanf("%d%d",&n,&k);
        slove(n,k);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值