HDNOIP201304 Blocking infection |
Difficulty level: A; programming language: Unlimited; run time limit: 1000ms; run space limit: 51200KB; code length limit: 2000000B |
Question Description |
H State N cities, N cities with n-1 two-way road interconnected to form a tree, city 1th is the capital, is also the root node in the tree. A highly hazardous infectious disease has erupted in the capital of H country. The authorities have decided to set up quarantine checkpoints on some highways in order to prevent outbreaks from spreading to the border cities (the cities indicated by leaf nodes), but the costs of multiplication on different roads may vary, and in order to avoid contagion in border cities, there must be at least one quarantine checkpoint on the route from the capital to each frontier city, Ask you to program the minimum multiplication total cost to stop the infection. |
Input |
The first line is an integer n, which indicates the number of cities. The next line of n-1, with 3 integers per line, u, V, W, separated by a space between every two integers, represents a two-way road from City u to City v, with a multiplication cost of W. The data guarantees that a tree is entered, and the root node number is 1. |
Output |
A total of one row, containing an integer, represents the minimum multiplication total cost required to control the outbreak. |
Input example |
9 2 1 888 1 3 88 2 4 6 5 2 8 6 3 100 3 7 10 8 3 50 7 9 1 |
Output example |
102 |
Other Notes |
For 60% of the data, 2<n<10, for 80% of the data, 2<n<100, for 100% of the data, 2<n<100000 |
The puzzle: Write a minimal cut and go straight away ...
WA took two rounds because BFs forgot to set vis[s]=true ... Qaq ...
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <algorithm>5#include <queue>6#include <cstring>7 #definePAU Putchar (")8 #defineENT Putchar (' \ n ')9 using namespacestd;Ten Const intmaxn=100000+Ten, maxm=200000+Ten, inf=-1u>>1; One int out[MAXN]; A structisap{ - structted{intX,y,w;ted*nxt,*re;} adj[maxm],*fch[maxn],*ms,*cur[maxn],*RET[MAXN]; - intd[maxn],gap[maxn],n,s,t; the voidInitintN) { This->n=n;memset (d,-1,sizeof(d)); Ms=adj;return;} - voidAddintXintYintW) { -*ms= (Ted) {x,y,w,fch[x],ms+1};fch[x]=ms++; -*ms= (Ted) {y,x,0, fch[y],ms-1};fch[y]=ms++; + return; - } + voidBFs () { Aqueue<int>Q; Q.push (T);d [t]=0; at while(!Q.empty ()) { - intu=Q.front (); Q.pop (); - for(ted*e=fch[u];e;e=e->NXT) { - intv=e->y;if(d[v]<0) d[v]=d[u]+1, Q.push (v); - } -}return; in } - intMxflow (intSintT) { to This->s=s; This->t=t;bfs (); ted*e;intk=s,flow=0; + for(intI=1; i<=n;i++) gap[d[i]]++,cur[i]=Fch[i]; - while(d[s]<N) { the if(k==T) { * intMi=inf,pos; for(intI=s;i!=t;i=cur[i]->y)if(CUR[I]->W<MI) mi=cur[i]->w,pos=i; $ for(intI=s;i!=t;i=cur[i]->y) cur[i]->w-=mi,cur[i]->re->w+=mi;flow+=mi;k=Pos;Panax Notoginseng} for(E=CUR[K];E;E=E->NXT)if(e->w&&d[k]==d[e->y]+1) Break; - if(e) cur[k]=e,ret[e->y]=e->re,k=e->y; the Else{if(--gap[d[k]]==0) Break; cur[k]=fch[k];intMi=N; + for(E=FCH[K];E;E=E->NXT)if(E->W&&D[E->Y]<MI) mi=d[e->y]; Ad[k]=mi+1; gap[d[k]]++;if(k!=s) k=ret[k]->y; the } +}returnflow; - } $ }sol; $ structted{intX,Y,W;TED*NXT;} adj[maxm],*fch[maxn],*ms=adj; - voidAddintXintYintW) { -*ms= (Ted) {x,y,w,fch[x]};fch[x]=ms++; the*ms= (Ted) {y,x,w,fch[y]};fch[y]=ms++; - return;Wuyi } the BOOLVIS[MAXN]; - voidBFsintS) { Wuqueue<int>Q; Q.push (S); vis[s]=true; - while(!Q.empty ()) { About intu=Q.front (); Q.pop (); $ for(ted*e=fch[u];e;e=e->NXT) { - intv=e->y;if(!Vis[v]) { -vis[v]=true; out[U]++;sol.add (U,V,E->W); Q.push (v);//printf ("add:%d->%d%d\n", u,v,e->w); System ("pause"); - } A } +}return; the } - intn,m;queue<int>Tset; $ voidMakeg () { the for(intI=1; i<=n;i++)if(! out[i]) Tset.push (i);//printf ("Tset:"); the while(!Tset.empty ()) { the intX=tset.front (); Tset.pop (); Sol.add (x,n+1, 1e9);//printf ("%d", x); the}return; - } inInlineintRead () { the intx=0, sig=1;CharCh=GetChar (); the while(!isdigit (CH)) {if(ch=='-') sig=-1; ch=GetChar ();} About while(IsDigit (CH)) x=Ten*x+ch-'0', ch=GetChar (); the returnx*=Sig; the } theInlinevoidWriteintx) { + if(x==0) {Putchar ('0');return;}if(x<0) Putchar ('-'), x=-x; - intlen=0, buf[ the]; while(x) buf[len++]=x%Ten, x/=Ten; the for(inti=len-1; i>=0; i--) Putchar (buf[i]+'0');return;Bayi } the voidinit () { theN=read (); M=~-n;sol.init (n+1);intx,y,w; - for(intI=1; i<=m;i++){ -X=read (); Y=read (); w=read (); Add (x,y,w); the}BFS (1); Makeg (); Write (Sol.mxflow (1, n+1)); the return; the } the voidWork () { - return; the } the voidprint () { the return;94 } the intMain () { theInit (); work ();p rint ();return 0; the}
Coj 0252 HDNOIP201304 Blocking infection