poj 3680 Intervals(最大费用流+离散化)

来源:互联网 发布:java http编程实例 编辑:程序博客网 时间:2024/05/17 22:56
Intervals
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 6049 Accepted: 2434

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 thank 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

43 11 2 22 3 43 4 83 11 3 22 3 43 4 83 11 100000 1000001 2 3100 200 3003 21 100000 1000001 150 301100 200 300

Sample Output

1412100000100301
题意:给出n个区间[ai,bi],wi,表示如果取这个区间i,则能获得权值wi,现在有一个限制,就是取若干个区间后,所有取到的点的重复次数不能超过k,且要使最后获得的权值最大,求最后获得的权值。
思路:先说说如何构图:
先将区间的端点离散化(无视区间内的点),设离散化后共得到cnt个点,设超级源s=0,超级汇t=cnt+1,从s到cnt,每个点按顺序向后一个点连边,费用为0,容量为k。
对于每个区间端点ai、bi,取离散化后的标号hash[ai]、hash[bi],连一条边,费用为该区间权值,流量为1。
最后求最大费用流即可。
为什么这样构图可以求解呢?
这样构图以后,若两个区间有重叠的部分,那么如果两个区间都取,就肯定需要两条增广路,这样的话重叠部分的点重复次数就是2,一开始设置源点到点1的流量为k,就限制了点重复次数最多为k了。
AC代码:
#include <iostream>#include <cmath>#include <cstdlib>#include <cstring>#include <cstdio>#include <queue>#include <stack>#include <ctime>#include <algorithm>#define ll __int64using namespace std;const int INF = 1000000000;const int maxn = 200000;struct node{    int a, b, w;}inter[205];struct Edge{    int u, v, cost, cap, flow, next;}et[maxn * 10];int low[maxn], pre[maxn], dis[maxn], eh[maxn], hash[maxn];bool vis[maxn];int s, t, num, anscost, n, k;void init(){    memset(eh, -1, sizeof(eh));    num = 0;}void add(int u, int v, int cost, int cap, int flow){    Edge e = {u, v, cost, cap, flow, eh[u]};    et[num] = e;    eh[u] = num++;}void addedge(int u, int v, int cost, int cap){    add(u, v, cost, cap, 0);    add(v, u, -cost, 0, 0);}bool spfa(){    int Q[maxn], first = 0, tail = 1;    for(int i = s; i <= t; i++)    {        pre[i] = -1;        low[i] = 0;        dis[i] = -INF;        vis[i] = false;    }    dis[s] = 0, low[s] = INF, vis[s] = true;    Q[0] = s;    while(first != tail)    {        int u = Q[first++];        vis[u] = false;        for(int i = eh[u]; i != -1; i = et[i].next)        {            int v = et[i].v, cost = et[i].cost, cap = et[i].cap, flow = et[i].flow;            if(cap - flow && dis[v] < dis[u] + cost)            {                dis[v] = dis[u] + cost;                pre[v] = i;                low[v] = min(low[u], cap - flow);                if(!vis[v])                {                    vis[v] = true;                    Q[tail++] = v;                }            }        }    }    return dis[t] != -INF;}void costflow(){    anscost = 0;    while(spfa())    {        int x = pre[t];        anscost += low[t] * dis[t];        while(x != -1)        {            et[x].flow += low[t];            et[x^1].flow -= low[t];            x = pre[et[x].u];        }    }}int main(){    int tt;    scanf("%d", &tt);    while(tt--)    {        scanf("%d%d", &n, &k);        memset(hash, 0, sizeof(hash));        for(int i = 0; i < n; i++)        {            scanf("%d%d%d", &inter[i].a, &inter[i].b, &inter[i].w);            hash[inter[i].a] = hash[inter[i].b] = 1;        }        int cnt = 0;        for(int i = 1; i <= 100000; i++)        if(hash[i] > 0) hash[i] = ++cnt;        s = 0, t = cnt + 1;        init();        for(int i = 0; i <= cnt; i++) addedge(i, i + 1, 0, k);        for(int i = 0; i < n; i++) addedge(hash[inter[i].a], hash[inter[i].b], inter[i].w, 1);        costflow();        printf("%d\n", anscost);    }    return 0;}


0 0
原创粉丝点击