Test instructions
Give the N to the interval (AI,BI) and the corresponding weight of WI, now to select some of the interval, require any point can not be more than K interval coverage, the goal is to maximize the total weight.
Analysis:
Conversion to the maximum cost flow, change the minimum cost flow template is good.
Code:
POJ 3680//sep9 #include <iostream> #include <vector> #include <queue> #include <algorithm> usi
NG namespace Std;
const int maxn=2048;
const int maxm=20024;
const int inf=2000000000;
struct Edge {int v,f,w,nxt;
}E[4*MAXM+10];
int g[maxn+10];
int nume,src,sink;
Queue<int> Q;
BOOL INQ[MAXN+10];
int dist[maxn+10];
int prev[maxn+10],pree[maxn+10];
int A[MAXN],B[MAXN],W[MAXN];
void Addedge (int u,int v,int c,int w) {e[++nume].v=v;e[nume].f=c;e[nume].w=w;e[nume].nxt=g[u];g[u]=nume;
E[++nume].v=u;e[nume].f=0;e[nume].w=-w;e[nume].nxt=g[v];g[v]=nume; } bool Find_path () {while (!
Q.empty ()) Q.pop ();
memset (dist,-1,sizeof (Dist));
memset (inq,false,sizeof (INQ));
Q.push (SRC);
Inq[src]=true;
dist[src]=0; while (! Q.empty ()) {int U=q.front ();
Q.pop ();
Inq[u]=false; for (int i=g[u];i;i=e[i].nxt) {if (e[i].f>0&&dist[u]+e[i].w>dist[e[i].v]) {dist[e[i].v]=dist[u]+e[i].
W
Prev[e[i].v]=u;
Pree[e[i].v]=i;
if (!INQ[E[I].V]) { Q.push (E[I].V);
Inq[e[i].v]=true;
}}}} return dist[sink]==-1?false:true;
} int Max_cost_flow (int f) {int res=0;
while (f>0) {if (Find_path () ==false) return-1;
int d=f;
for (int v=sink;v!=src;v=prev[v]) d=min (D,E[PREE[V]].F);
F-=d;
Res+=d*dist[sink];
for (int v=sink;v!=src;v=prev[v]) {e[pree[v]].f-=d;
E[pree[v]^1].f+=d;
}} return res;
} void Init () {memset (g,0,sizeof (g));
nume=1;
} int main () {int cases;
scanf ("%d", &cases);
while (cases--) {init ();
int i,n,m,k;
scanf ("%d%d", &n,&k);
Vector<int> x;
for (i=0;i<n;++i) {scanf ("%d%d%d", &a[i],&b[i],&w[i]);
X.push_back (A[i]);
X.push_back (B[i]);
} sort (X.begin (), X.end ());
X.erase (Unique (X.begin (), X.end ()), X.end ());
M=x.size ();
src=m,sink=m+1;
Addedge (src,0,k,0);
Addedge (m-1,sink,k,0);
for (I=0;i+1<m;++i) Addedge (i,i+1,inf,0);
for (i=0;i<n;++i) {int u=find (X.begin (), X.end (), A[i])-x.begin (); int v=Find (X.begin (), X.end (), B[i])-x.begin ();
Addedge (U,v,1,w[i]);
} printf ("%d\n", Max_cost_flow (k));
} return 0; }