2016-06-01 22:01:39
Title Link: POJ no.3680 intervals
Main topic:
Given n weighted intervals, a maximum of one point m can be repeated and a selection method is obtained to make the maximum
Solution:
Cost flow
Modeling:
The endpoints of the interval are connected in accordance with the sub-weight flow 1, and each point needs to be connected to a 0-weighted flow of infinity as a skip
Note the place:
100,000 points must be no, see me unique discrete Dafa
1 //intervals (POJ no.3680)2 //Cost Flow3#include <stdio.h>4#include <algorithm>5#include <queue>6 using namespacestd;7 Const intmaxn=410;8 intt,n,k;9 intHASH[MAXN];Ten intA[MAXN]; One intW[MAXN]; A structEdge - { - intto ; the int from; - intCost ; - intflow; - intNext; + Edge () {} -Edgeint from,intTo,intCostintFlowintNext): from( from), to, cost, flow, next (next) {} + }; AEdge n[maxn*Ten]; at intCNT; - intLen; - intNow ; - intHEAD[MAXN]; - BOOLVIS[MAXN]; - intDIST[MAXN]; in intPRE[MAXN]; -Queue <int>Q; to voidInsertintXintYintZintCost ) + { -n[++cnt]=Edge (X,y,cost,z,head[x]); thehead[x]=CNT; *N[++cnt]=edge (Y,x,-cost,0, Head[y]); $head[y]=CNT;Panax Notoginseng return ; - } the voidSPFA (intSintt) + { AFill (DIST,DIST+MAXN,100000); the Q.push (s); +vis[s]=1; -dist[s]=0; $ while(!q.empty ()) $ { -now=Q.front (); - Q.pop (); thevis[now]=0; - for(intI=head[now];i;i=n[i].next)Wuyi { the if(n[i].flow>0) - { Wu if(dist[n[i].to]>dist[now]+n[i].cost) - { Aboutdist[n[i].to]=dist[now]+N[i].cost; $pre[n[i].to]=i; - if(!vis[n[i].to]) - { -vis[n[i].to]=1; A Q.push (n[i].to); + } the } - } $ } the } the for(intI=pre[t];i;i=pre[n[i]. from]) the { then[i].flow--; -n[i^1].flow++; in } the return ; the } About intMincost_flow (intSintTintnum) the { the intans=0; the while(num--) + { - SPFA (s,t); theans+=Dist[t];Bayi } the return-ans; the } - intMain () - { thescanf"%d",&T); the while(t--) the { theCnt=1; -Fill (HEAD,HEAD+MAXN,0); thescanf"%d%d",&n,&K); the for(intI=1; i<=n;i++) the {94scanf"%d%d", &a[i*2-1],&a[i*2]); thescanf"%d",&w[i]); thehash[i*2-1]=a[i*2-1]; thehash[i*2]=a[i*2];98 } AboutSort (hash+1, hash+2*n+1); -Len=unique (hash+1, hash+2*n+1)-hash-1;101 for(intI=1; i<=n*2; i++)102 {103A[i]=lower_bound (hash+1, hash+len+1, A[i])-Hash;104 if(! (i&1)) Insert (a[i-1],a[i],1,-w[i/2]); the }106 for(intI=1; i<len;i++) Insert (i,i+1,100000,0);107Insert (len,len+1K0);108Insert0,1K0);109printf"%d\n", Mincost_flow (0, len+1, K)); the }111}
POJ no.3680 Intervals