bzoj3876 "Ahoi2014" spur plot

Source: Internet
Author: User

3876: [Ahoi2014] Spur plot time limit: ten Sec Memory Limit: + MB
Submit: 485 Solved: 297
[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
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) <=5000


by anonymous upload

there are Yuanhui with upper and lower boundary cost flow problem

We add the source point S and the meeting point T in the diagram.

Here is the composition method:

For each edge (x, Y, z), The Edge (S,y,1,z) and (X,y,inf,z). For each point x, the Edge (x,t,out[x],0) and (x,1,inf,0). Where Out[x] represents the degree of the X-node.

Finally, the maximum flow of the minimum cost s to T is obtained.

#include <iostream> #include <cstdio> #include <cmath> #include <cstring> #include <cstdlib > #include <algorithm> #include <queue> #define F (I,j,n) for (int. i=j;i<=n;i++) #define D (i,j,n) for (int i=j;i>=n;i--) #define LL Long long#define pa pair<int,int> #define MAXN 500#define MAXM 200000#define inf 10000000 00using namespace Std;struct edge_type{int next,from,to,v,c;} E[maxm];int n,m,ans,cnt=1,y,z,s,t;int head[maxn],dis[maxn],p[maxn];bool inq[maxn];inline int read () {int X=0,f=1;char Ch=getchar (); while (ch< ' 0 ' | | Ch> ' 9 ') {if (ch== '-') F=-1;ch=getchar ();} while (ch>= ' 0 ' &&ch<= ' 9 ') {x=x*10+ch-' 0 '; Ch=getchar ();} return x*f;} inline void Add_edge (int x,int y,int v,int c) {e[++cnt]= (Edge_type) {head[x],x,y,v,c};head[x]=cnt;e[++cnt]= (Edge_type) {head[y],y,x,0,-c};head[y]=cnt;} inline bool Spfa () {Queue<int>q;memset (inq,false,sizeof (INQ)); F (i,1,t) Dis[i]=inf;dis[s]=0;inq[s]=true;q.push (s), while (!q.empty ()) {int X=q.front (); Inq[x]=false;q.pop (); for (int i=head[x];i;i=e[i].next) {int y=e[i].to;if (E[I].V&AMP;&AMP;DIS[Y]&GT;DIS[X]+E[I].C) {dis[y]=dis[x]+e[i].c;p [Y]=i;if (!inq[y]) {Inq[y]=true;q.push (y);}}} return dis[t]!=inf;} inline void Mcf () {Ans=0;while (SPFA ()) {int tmp=inf;for (int i=p[t];i;i=p[e[i].from]) tmp=min (TMP,E[I].V); Ans+=tmp*dis [T];for (int i=p[t];i;i=p[e[i].from]) {e[i].v-=tmp;e[i^1].v+=tmp;}}} int main () {n=read (); s=n+1;t=n+2; F (i,1,n) {m=read (); F (j,1,m) {y=read (); Z=read (); Add_edge (i,y,inf,z); Add_edge (s,y,1,z);} if (i!=1) Add_edge (i,1,inf,0); Add_edge (i,t,m,0);} MCF ();p rintf ("%d\n", ans);

bzoj3876 "Ahoi2014" 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: 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.