"BZOJ-3876" line plot has the upper and lower bounds of the network flow (there is a low bound active sink minimum cost maximum flow)

Source: Internet
Author: User

3876: [Ahoi2014] Spur plot time limit:10 Sec Memory limit:256 MB
submit:821 solved:502
[Submit] [Status] [Discuss] Description "Story Background" otaku jyy very much like to play RPG games, such as Paladin, Xuan Jian and so on. But jyy like not fighting scenes, but similar to the drama of the general hatred of the story. These games tend to have a lot of spur plots, and now jyy want to spend the least time watching all the spur plots. "Problem description" Jyy now play in the RPG game, there are a total of n plot points, from 1 to n numbered, the first I plot points can be based on the different choices of jyy, and through different spur plot, go to the different new plot point ki. Of course, if it is 0, it means that the I plot point is a game ending. Jyy It takes some time to watch a spur plot. Jyy began at the 1th plot point, the beginning of the game. Obviously any plot point is from the number 1th plot point can reach. In addition, as the game progresses, the plot is irreversible. So the game is guaranteed to start from any plot point, can not return to this plot point. Because jyy overuse modifier, resulting in the game's "archive" and "read" function damage, so jyy to go back to the previous plot point, the only way is to exit the current game, and start a new game, that is, back to the 1th plot point. Jyy can exit the game at any time and start over again. Constantly start a new game repeatedly watching the plot has been seen is very painful, jyy hope to spend the least time, read all the different spur plot. Input line contains a positive integer n. Next n lines, I act the information of the plot point of I; the first integer is, the next integer pair, bij and Tij, represents the time from the plot point I can go to the plot point and watch the line plot take. Output

The output line contains an integer that represents the minimum time required for jyy to read all the spur plots.

Sample Input6
2 2 1) 3 2
2 4 3) 5 4
2 5 5) 6 6
0
0
0Sample Output -HINT

Jyy need to start the Game 3 times, plus a first game, 4 times the game process is

1->2->4,1->2->5,1->3->5 and 1->3->6. For 100% of data meet N<=300,0<=ki<=50,1<=tij<=300,sigma (Ki) <=5000source

by anonymous upload

Solution

There is a lower bound of the active sink minimum cost maximum flow, compared to the bare

For Edge U-->v Upper bound inf cost C, lower bound 1 cost C

For each point, the connection capacity is out, the cost is 0; even 1, in lieu of Yuanhui, capacity inf, cost C

Ps:zkw run fast, but rank the first two pages how so fast ... Not a complex level of ah ....

Code
#include <iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>#include<queue>using namespacestd;intRead () {intx=0, f=1;CharCh=GetChar ();  while(ch<'0'|| Ch>'9') {if(ch=='-') f=-1; Ch=GetChar ();}  while(ch>='0'&& ch<='9') {x=x*Ten+ch-'0'; Ch=GetChar ();} returnx*F;}#defineMAXN 310#defineMAXM 1000100intN;structedgenode{intNext,to,cap,cost;} EDGE[MAXM];intHead[maxn],cnt=1;voidAddintUintVintWintc) {CNT++; Edge[cnt].cap=w;edge[cnt].cost=c;edge[cnt].to=v;edge[cnt].next=head[u];head[u]=CNT;}voidInsertintUintVintWintc) {Add (u,v,w,c); Add (V,u,0,-c);}intDis[maxn],s,t,mincost;BOOLMARK[MAXN];#defineINF 0x7fffffffBOOLSPFA () {Queue<int>q; memset (Mark,0,sizeof(Mark));  for(intI=s; i<=t; i++) dis[i]=inf; Q.push (T); Dis[t]=0; mark[t]=1;  while(!Q.empty ()) {            intNow=q.front (); Q.pop (); mark[now]=0;  for(intI=head[now]; I I=edge[i].next)if(edge[i^1].cap && dis[edge[i].to]>dis[now]+edge[i^1].cost) {dis[edge[i].to]=dis[now]+edge[i^1].cost; if(!mark[edge[i].to]) Q.push (edge[i].to), mark[edge[i].to]=1; }        }    returndis[s]!=inf;}intDfsintLocintLow ) {Mark[loc]=1; if(loc==t)returnLow ; intW,used=0;  for(intI=head[loc]; I I=edge[i].next)if(Edge[i].cap &&!mark[edge[i].to] && dis[edge[i].to]==dis[loc]-edge[i].cost) {W=dfs (Edge[i].to,min (low-used,edge[i].cap)); Edge[i].cap-=w; edge[i^1].cap+=w; Used+=w; mincost+=w*Edge[i].cost; if(Used==low)returnLow ; }    returnused;}intzkw () {inttmp=0;  while(SPFA ()) {mark[t]=1;  while(Mark[t]) memset (Mark,0,sizeof(Mark)), tmp+=DFS (S,inf); }    returntmp;}intMain () {n=read (); s=0, t=n+1;  for(intI=1; i<=n; i++)        {            intm=read ();  for(intv,c,j=1; j<=m; J + +) v=read (), C=read (), insert (I,V,INF,C), insert (S,v,1, c); Insert (I,t,m,0); if(i!=1) Insert (I,1Inf0); } zkw (); printf ("%d\n", Mincost); return 0;}

"BZOJ-3876" line plot has the upper and lower bounds of the network flow (there is a low bound active sink 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.