POJ 3680:intervals "minimum cost maximum Flow"

Source: Internet
Author: User

You have n open interval, each interval has a weight of WI, you have to choose some range, so that satisfies: each point is not more than the K range coverage, the weight of the largest

Idea: The feeling is very good to think of the cost of flow, the end of each zone connected, the cost of the interval of the opposite number of weight (because to the maximum, so is the maximum cost of the maximum flow), the capacity of 1, as far as the limit of K, as long as from the source to the first point of the flow of K on the As long as the flow is greater than the K can (I take the positive infinity)

poj3680

#include <stdio.h>

#include <iostream>

#include <string.h>

#include <algorithm>

#include <queue>

#define MAXN 120090

#define ESP 0.00001

#define INF 0x3f3f3f3f

using namespace Std;

int HEAD[MAXN],POINT[MAXN],FLOW[MAXN],NEXT[MAXN];

int NOW=0,VALUE[MAXN],K,XX,YY,VV,X[MAXN],Y[MAXN];

int v[maxn],h=0,inte[maxn],id[maxn],root[maxn],n;

int dist[maxn],pre[maxn],j;

void Add (int x,int y,int F,int v)

{

NEXT[++NOW]=HEAD[X];

Head[x]=now;

Point[now]=y;

Flow[now]=f;

Value[now]=v;

Root[now]=x;

Next[++now]=head[y];

Head[y]=now;

Point[now]=x;

flow[now]=0;

Value[now]=-v;

Root[now]=y;

}

int SPFA (int s,int t)

{

for (int i=1;i<=j;i++) dist[i]=200000;

dist[t]=200000;

dist[s]=0;

int visit[maxn]={0};

Visit[s]=1;

queue<int>q;

Q.push (s);

while (!q.empty ())

{

int U=q.front ();

Q.pop ();

visit[u]=0;

for (int i=head[u];i;i=next[i])

{

int k=point[i];

if (Dist[u]+value[i]<dist[k] && flow[i])

{

Dist[k]=dist[u]+value[i];

Pre[k]=i;

if (!visit[k])

{

Visit[k]=1;

Q.push (k);

}

}

}

}

if (dist[t]==200000) return 0;else return 1;

}

int main ()

{

int tt;

scanf ("%d", &TT);

while (tt--)

{

int ans=0;

now=0;h=0;

memset (head,0,sizeof (head));

memset (pre,0,sizeof (pre));

scanf ("%d%d", &n,&k);

for (int i=1;i<=n;i++)

{

scanf ("%d%d%d", &XX,&YY,&VV);

X[I]=XX;Y[I]=YY;V[I]=VV;

Inte[++h]=xx;inte[++h]=yy;

}

Sort (inte+1,inte+1+h);

J=1;

Id[inte[1]]=1;

for (int i=2;i<=h;i++)

{

if (Inte[i]!=inte[j])

{

Inte[++j]=inte[i];

Id[inte[j]]=j;

}

}

for (int i=1;i<=j;i++)

Add (i,i+1,inf,0);

for (int i=1;i<=n;i++)

{

Add (Id[x[i]],id[y[i]],1,-v[i]);

}

int s=maxn-10,t=maxn-100;

Add (s,1,k,0);

Add (j,t,k,0);

while (SPFA (s,t))

{

int e=pre[t],minx=flow[e];

while (e)

{

Minx=min (Minx,flow[e]);

E=pre[root[e]];

}

E=PRE[T];

while (e)

{

Flow[e]-=minx;

flow[((e-1) ^1) +1]+=minx;

E=pre[root[e]];

}

Ans+=dist[t]*minx;

}

printf ("%d\n",-ans);

}

return 0;

}

POJ 3680:intervals "minimum cost maximum Flow"

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.