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

Source: Internet
Author: User

Title Description

After a period of time, the network company finally has a certain popularity, also began to receive some orders, 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.

Input

The first line consists of 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

There is only one row that 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 input

3 3 5
1 2 1
2 3 5
3 1 4

Sample output

6

Exercises

Dynamic Plus Edge network flow

Since the subject is limited to one plane per day, the flight time can be considered as a day, and the departure time as the No. 0 day.

This makes it possible to split a point by splitting each point except 1 and n into Totaltime+1, as a 0~totaltime+1 layer. Note that point 1 and point n do not need to be split, that is, 1 and n regardless of the number of layers, the numbering is always 1 and N.

First add s->1, the capacity is T, represents the T individual.

Then the enumeration time t, the point x at the level of the first split of the K-layer (X,K), The Edge (I,t-1), (i,t), the capacity of the INF; for the edges X->y (z) in the original, The Edge (x,t-1), and the capacity is Z.

This dynamic add edge after the N as a meeting point, run Dinic, judge whether the full stream can be.

#include <cstdio> #include <cstring> #include <queue> #define N 10010#define M 1000010#define inf 0x3f3f3f3f#define POS (i, j) (i = = 1 | | i = = n)?  I: (n-2) * (j) + i + 1using namespace std;queue<int> q;int head[n], to[m], val[m], next[m], cnt = 1, s, T, Dis[n], dx[m], dy[m], dz[m];void Add (int x, int y, int z) {to[++cnt] = y, val[cnt] = Z, next[cnt] = head[x], head[ X] = cnt;to[++cnt] = x, val[cnt] = 0, next[cnt] = Head[y], head[y] = cnt;} BOOL BFs () {int x, i;memset (dis, 0, sizeof (DIS)), while (!q.empty ()) Q.pop ();d is[s] = 1, Q.push (s) and while (!q.empty ()) {x = Q.front (), Q.pop (); for (i = head[x]; i; i = Next[i]) {if (Val[i] &&!dis[to[i]]) {Dis[to[i]] = dis[x] + 1;if (to[i) = = T) return 1;q.push (To[i]);}} return 0;} int dinic (int x, int low) {if (x = = t) return Low;int temp = Low, I, k;for (i = head[x]; i; i = Next[i]) {if (Val[i] & & Dis[to[i]] = = Dis[x] + 1) {k = Dinic (To[i], min (temp, val[i])), if (!k) dis[to[i] [= 0;val[i]-= k, VAL[i ^ 1] + = K;IF (! ( Temp-= k)) break;}} return low-temp;}  int main () {int n, m, K, I, j, ans = 0;scanf ("%d%d%d", &n, &m, &k), s = 0, t = N, add (S, 1, k); for (i = 1; I <= m; i + +) scanf ("%d%d%d", &dx[i], &dy[i], &dz[i]); for (i = 1; I <= n + k; i + +) {for (j = 2; J < N; j + +) Add (POS (J, I-1), POS (J, i), INF), for (j = 1; j <= M; j + +) Add (POS (dx[j], i-1), POS (dy[j], i), Dz[j] ); while (BFS ()) ans + = Dinic (s, INF), if (ans = = k) {printf ("%d\n", I); return 0;}} 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.