POJ 3680 Intervals, poj3680intervals

Source: Internet
Author: User

POJ 3680 Intervals, poj3680intervals

Discretization + maximum cost flow...


Source point --- 1... 2... 3... n --- edge with a connection traffic of K and a fee of 0

For edges (a, B, w) that are connected from a to B with a capacity of 1 w


Intervals
Time Limit:5000 MS   Memory Limit:65536 K
Total Submissions:6503   Accepted:2691

Description

You are givenNWeighted open intervals.ITh interval covers (Ai,Bi) And weighsWi. 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 moreKTimes.

Input

The first line of input is the number of test case.
The first line of each test case contains two integers,NAndK(1 ≤KN≤ 200 ).
The nextNLine each contain three integersAi,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

Source

POJ Founder Monthly Contest-2008.07.27, windy7926778


#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <queue>#include <vector>using namespace std;const int maxn=500;const int INF=0x3f3f3f3f;struct Edge{    int to,next,cap,cost,flow;}edge[maxn*maxn];int Adj[maxn],Size,N;void init(){    Size=0; memset(Adj,-1,sizeof(Adj));}void addedge(int u,int v,int cap,int cost){    edge[Size].to=v;    edge[Size].next=Adj[u];    edge[Size].cost=cost;    edge[Size].cap=cap;    edge[Size].flow=0;    Adj[u]=Size++;}void Add_Edge(int u,int v,int cap,int cost){    addedge(u,v,cap,cost);    addedge(v,u,0,-cost);}int dist[maxn],vis[maxn],pre[maxn];bool spfa(int s,int t){    queue<int> q;    for(int i=0;i<N;i++)    {        dist[i]=-INF; vis[i]=false; pre[i]=-1;    }    dist[s]=0; vis[s]=true; q.push(s);    while(!q.empty())    {        int u=q.front();        q.pop();        vis[u]=false;        for(int i=Adj[u];~i;i=edge[i].next)        {            int v=edge[i].to;            if(edge[i].cap>edge[i].flow&&               dist[v]<dist[u]+edge[i].cost)           {               dist[v]=dist[u]+edge[i].cost;               pre[v]=i;               if(!vis[v])               {                   vis[v]=true;                   q.push(v);               }           }        }    }    if(pre[t]==-1) return false;    return true;}int MinCostMaxFlow(int s,int t,int& cost){    int flow=0;    cost=0;    while(spfa(s,t))    {        int Min=INF;        for(int i=pre[t];~i;i=pre[edge[i^1].to])        {            if(Min>edge[i].cap-edge[i].flow)                Min=edge[i].cap-edge[i].flow;        }        for(int i=pre[t];~i;i=pre[edge[i^1].to])        {            edge[i].flow+=Min;            edge[i^1].flow-=Min;            cost+=Min*edge[i].cost;        }        flow+=Min;    }    return flow;}int n,m,K;struct Bian{    int u,v,w;}bian[maxn];int has[maxn],hn;int main(){    int T_T;    scanf("%d",&T_T);    while(T_T--)    {        init();hn=0;        scanf("%d%d",&m,&K);        for(int i=0;i<m;i++)        {            int a,b,c;            scanf("%d%d%d",&a,&b,&c);            bian[i].u=a; bian[i].v=b; bian[i].w=c;            has[hn++]=a; has[hn++]=b;        }        sort(has,has+hn);        hn=unique(has,has+hn)-has;        n=hn+2;        N=n;        for(int i=1;i<n;i++)        {            Add_Edge(i-1,i,K,0);        }        for(int i=0;i<m;i++)        {            bian[i].u=lower_bound(has,has+hn,bian[i].u)-has+1;            bian[i].v=lower_bound(has,has+hn,bian[i].v)-has+1;            Add_Edge(bian[i].u,bian[i].v,1,bian[i].w);        }        int flow,cost;        flow=MinCostMaxFlow(0,n-1,cost);        printf("%d\n",cost);    }return 0;}






Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.