"BZOJ1570" [Jsoi2008]blue Mary's Travel dynamic plus edge network streaming

Source: Internet
Author: User

"BZOJ1570" [Jsoi2008]blue Mary's travel description after a while, the network company finally has a certain popularity, also began to receive some orders, of which the largest one from the city of B. Blue Mary decided to sign the order in person. To save money on travel, one of his financial advisers suggested buying only u airline tickets. U airlines have only one flight per day, and are all arriving in the afternoon on the day of departure, so they can only take one flight per day. After investigation, they were given detailed information on all flights operated by U Airways, which included the departure of each flight, the destination, and the number of votes to be picked up on one day. (Note: For a certain flight, whichever day they are able to buy the most, the number of votes on the day of departure will be the same.) Blue Mary notes that they will be able to reach City B from City a only on a U-airline flight, but they may not be able to reach B city on the same day due to the limited number of tickets that can be purchased on each flight. So now that Blue Mary needs your help, design a travel plan to make the arrival time of the last person arriving in City b the earliest. The first line of input contains 3 positive integers n,m and T. All cities that appear in the title are numbered,..., N, with City a number must be 1, City B must be n. U company has a total of M (one-way) flights. And even Blue Mary, the company has a total of t individual to go from a city to B city. The following m lines, each line containing 3 positive integers, x, y, Z, represent the origin of each flight of the company, the destination and the number of votes that Blue Mary can buy on one day of the flight. (that is, on either day, Blue Mary can only buy tickets for Z-U airlines from City X to City Y.) ) Enter a maximum of one-way flights from one city to another city. Output has only one row and contains a positive integer that represents the earliest arrival time of the person who finally reached City B. Let's say that the day they first took the plane was the first day. Sample Input3 3 5
1 2 1
2 3 5
3 1 4
Sample Output6
HINT

Conventions:
2 <= N <= 50
1 <= M <= 2450
1 <= T <= 50
1 <= x, y <= N
X! = Y
1 <= Z <= 50

Solution: We assume that everyone can reach the end point at Tim, so we enumerate the TIM, split each point into a Tim, and then run the maximum flow, if the flow =t, indicating that Tim is the answer

Method: using (i,j) means to break the original I node into the first J point
1.S-(0,0) Flow t
2. (i,j-1) (i,j) Flow ∞
3. For Edge (A,b,c) (a,j-1)-to (b,j) flow C
4. (n,j) T-flow t

Because I think more points, feel very virtual, so with the dynamic plus edge of the maximum flow, 56ms Fly ~

#include <iostream> #include <cstdio> #include <cstring> #include <queue> #define P (A, B) (*n+) A) using namespace Std;int to[1000000],next[1000000],val[1000000],d[50010],head[50010],pa[2500],pb[2500],pc[2500]; int n,m,t,cnt,ans,sum,s,t,tim;queue<int> Q;int Rd () {int Ret=0,f=1;char gc=getchar (); while (gc< ' 0 ' | | Gc> ' 9 ') {if (gc== '-') F=-f;gc=getchar ();} while (gc>= ' 0 ' &&gc<= ' 9 ') ret=ret*10+gc-' 0 ', Gc=getchar (); return ret*f;} void Add (int a,int b,int c) {to[cnt]=b,val[cnt]=c,next[cnt]=head[a],head[a]=cnt++;to[cnt]=a,val[cnt]=0,next[cnt]= head[b],head[b]=cnt++;} int BFS () {int I,u;memset (d,0,sizeof (d)), while (!q.empty ()) Q.pop ();d [S]=1,q.push (S), while (!q.empty ()) {U=q.front (), Q.pop (); for (I=head[u];i!=-1;i=next[i]) {if (!d[to[i]]&&val[i]) {d[to[i]]=d[u]+1;if (to[i]==t) return 1; Q.push (To[i]);}} return 0;} int dfs (int x,int MF) {if (x==t) return mf;int i,temp=mf,k;for (I=head[x];i!=-1;i=next[i]) {if (d[to[i]]==d[x]+1& &val[i]) {K=dfs (To[i],min (Temp,val[i])), if (!k) d[To[i]]=0;val[i]-=k,val[i^1]+=k,temp-=k;if (!temp) break;}} return mf-temp;} int main () {n=rd (), M=rd (), T=rd (), tim=n+t; s=0,t=n* (tim+1) +1;memset (head,-1,sizeof (head)); add (s,1,t); int i,j,k,a,b,c;for (i=1;i<=m;i++) pa[i]=rd (), pb[i]= RD (), PC[I]=RD (), for (i=1;i<=tim;i++) {for (j=1;j<=m;j++) Add (P (pa[j],i-1), P (pb[j],i), Pc[j]), and for (j=1;j<=n; J + +) Add (P (j,i-1), P (j,i), 1<<30), add (P (n,i), t,t), while (BFS ()) Sum+=dfs (s,1<<30), if (sum==t) {printf ("%d ", i); return 0;}}}

"BZOJ1570" [Jsoi2008]blue Mary's Travel dynamic plus edge network streaming

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.