POJ3680 Intervals

来源:互联网 发布:域名更新 编辑:程序博客网 时间:2024/06/06 02:23

Intervals
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 8343 Accepted: 3562

Description

You are given N weighted open intervals. The ith interval covers (aibi) 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 aibiwi(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

Source

POJ Founder Monthly Contest – 2008.07.27, windy7926778 

—————————————————————————————————

题目大意:给你n个区间(ai,bi),对应区间的权重为ci。现在让你挑选一些区间,使得任一点出现的次数不超过k,问你满足条件的方案的最大权重和为多少?

思路:最小费用最大流,先拿源点和每个左端点连边,流量为1.费用为0,汇点和右端点连边,流量为1,费用为0.区间左端点和右端点连边费用为-价值,流量为1,然后n2判区间是否重叠,不重叠就连一条费用0,流量无穷的边

#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <algorithm>#include <cmath>#include <map>#include <set>#include <stack>#include <queue>#include <vector>#include <bitset>using namespace std;#define LL long longconst int INF = 0x3f3f3f3f;#define MAXN 100100#define MAXM 1000100int vis[MAXN],d[MAXN],pre[MAXN],aa[MAXN];struct Edge{    int u, v, c, cost, next;} edge[MAXM];int s[MAXN], cnt;void init(){    cnt = 0;    memset(s, -1, sizeof(s));}void add(int u, int v, int c, int cost){    edge[cnt].u = u;    edge[cnt].v = v;    edge[cnt].cost = cost;    edge[cnt].c = c;    edge[cnt].next = s[u];    s[u] = cnt++;    edge[cnt].u = v;    edge[cnt].v = u;    edge[cnt].cost = -cost;    edge[cnt].c = 0;    edge[cnt].next = s[v];    s[v] = cnt++;}bool spfa(int ss, int ee,int &flow,int &cost){    queue<int> q;    memset(d, INF, sizeof d);    memset(vis, 0, sizeof vis);    d[ss] = 0, vis[ss] = 1, pre[ss] = 0, aa[ss] = INF;    q.push(ss);    while (!q.empty())    {        int u = q.front();        q.pop();        vis[u] = 0;        for (int i = s[u]; ~i; i = edge[i].next)        {            int v = edge[i].v;            if (edge[i].c>0&& d[v]>d[u] + edge[i].cost)            {                d[v] = d[u] + edge[i].cost;                pre[v] = i;               aa[v] = min(aa[u], edge[i].c);                if (!vis[v])                {                    vis[v] = 1;                    q.push(v);                }            }        }    }    if (d[ee] == INF) return 0;    flow += aa[ee];    cost += d[ee]*aa[ee];    int u = ee;    while (u != ss)    {        edge[pre[u]].c -= aa[ee];        edge[pre[u] ^ 1].c += aa[ee];        u = edge[pre[u]].u;    }    return 1;}int MCMF(int ss, int ee){    int cost = 0, flow=0;    while (spfa(ss, ee, flow, cost));    return cost;}int a[300],b[300];int main(){    int T,m,n,k,u,v,c;    for(scanf("%d",&T);T--;)    {        init();        scanf("%d%d",&n,&k);        add(100001,100002,k,0);        add(100003,100004,k,0);        for(int i=0;i<n;i++)        {            scanf("%d%d%d",&a[i],&b[i],&c);            add(a[i],b[i],1,-c);            add(100002,a[i],1,0);            add(b[i],100003,1,0);        }        for(int i=0;i<n;i++)            for(int j=0;j<n;j++)        {            if(b[i]<a[j])                add(b[i],a[j],1,0);        }        printf("%d\n",-MCMF(100001,100004));    }    return 0;}


原创粉丝点击