C + + Path advanced--Minimum cost maximum flow (spur plot)

Source: Internet
Author: User

F.a.qs Home discuss Problemset Status ranklist Contest modifyuser hyxzc Logout Donate this site
Notice: Since this OJ is built under the Linux platform, and many of the data are produced under Windows, please note the input, output statements and data types and ranges to avoid unnecessary re appearance. 3876: [Ahoi2014] Spur plot time limit:10 Sec Memory limit:256 MB
submit:542 solved:332
[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 the data to meet the N<=300,0<=ki<=50,1<=tij<=300,sigma (Ki) <=5000: Connect the starting point with all points, the path between the edge, the traffic is INF, the weight value is Z, Connect the start point to the end of the path, the weight is Z, the flow is 1, and then run the minimum cost maximum flow. Code:
1#include <cstdio>2#include <cstring>3#include <queue>4#include <iostream>5#include <algorithm>6 #defineMAXN 3107 #defineS 08 #defineT (n+1)9   Ten using namespacestd; One    A intans=0, n,m; -    - namespaceMin_cost_max_flow the    { -       intHEAD[MAXN], from[maxn],dis[maxn],inq[maxn],cnt=1;  -        -       structSS +         { -             intC; +             intv; A             intNext; at             intto ; -             int  from; -}e[1001001]; -       voidAddintUintVintWintc) -            { -e[++cnt].next=Head[u]; ine[cnt].v=W; -E[cnt].c=C; toE[CNT]. from=u; +head[u]=CNT; -e[cnt].to=v; the            } *      voidInsertintUintVintWintc) $            {Panax Notoginseng Add (u,v,w,c); -Add (V,u,0,-c); the            } +      BOOLSPFA () A            { the               for(intI=1; i<=t;i++) dis[i]=0x7fffffff; +dis[s]=0; -inq[s]=0; $queue<int>que; $ Que.push (S); -               while(!que.empty ()) -                { the                   intnow=Que.front (); - Que.pop ();Wuyiinq[now]=0; the                    for(intI=head[now];i;i=e[i].next) -                      if(e[i].v&&dis[e[i].to]>dis[now]+e[i].c) Wu                         { -dis[e[i].to]=dis[now]+e[i].c; About                              from[e[i].to]=i; $                             if(!inq[e[i].to]) inq[e[i].to]=1, Que.push (e[i].to);  -                         } -                } -             if(dis[t]==0x7fffffff)return 0;  A                return 1;  +            }               the      voidMCF () -            { $              intx=0x7fffffff; the               for(intI= from[t];i;i= from[E[i]. from]) thex=min (x,e[i].v); the               for(intI= from[t];i;i= from[E[i]. from]) the                 { -e[i].v-=x; ine[i^1].v+=x; theans+=x*e[i].c; the                 }     About            }            the     } the  intMain () the      { +         using namespaceMin_cost_max_flow; -scanf"%d",&N);  the          for(intI=1; i<=n;i++)Bayi           { the             intK; thescanf"%d",&k); -Insert (I,t,k,0); -              for(intj=1; j<=k;j++) the               { the                 inty,z; thescanf"%d%d",&y,&z); theInsert (I,y,0x7fffffff, z); -Insert (S,y,1, z);  the               }  the             if(i!=1) Insert (I,1,0x7fffffff,0);  the           }94          while(SPFA ()) MCF (); theprintf"%d\n", ans);  the}

C + + Path advanced--Minimum cost maximum flow (spur plot)

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.