UVa1161 Objective:berlin (maximum flow)

Source: Internet
Author: User

Topics

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem= 3602

Description

The administration of a well-known football team have made a study about the lack of the
International away games. This study have concluded the only reason for this lack of the
The di?culty in organizing, the travel arrangements. To help solving this problem, the administration
Have asked you to build a program that computes the maximum number of people so can? Y from
A departure city to a destination city, using the available places in regular? ights. A given day, and
Arriving at or before a given time. When traveling from one city to another, a person could make multiple
Transfers. Each transfer are, at least, minutes long, i.e., the departure time should being, at least 30
Minutes after the arrival time. Given a set of ights for a single day and the latest arrival time, your
program should compute the maximum number of persons so can? Y, directly or indirectly, from a
Departure city to a destination city, arriving at or before the latest arrival time.

Input

The input would contain several test cases, each of the them as described below. Consecutive
Test cases is separated by a single blank line.
The. RST line contains a integer (smaller or equal to) indicating the number of cities that has
? ight connections. The second line contains a string indicating the city of departure. The third line
Contains a string indicating the destination city. The fourth line contains the latest arrival time, the
Format HHMM, where HH is the hour in the day (from XX to 23°c) and MM is the minute in the hour
(from XX to 59). The. Fth line contains a integer N (smaller or equal to-), with the number of
Existing? ights. Each of the following N lines contains the info for each ight. Each such line contains
Strings and three integers, separated by blank spaces, O E C D A, where O and E is, respectively,
The origin and destination of a? Ight, C is the number of available places in the? Ight (from 0 to 300),
and D and A is the departure and arrival times in the previously de?ned format HHMM. All ights?
Start and end in the same day. City names May has up to 8 characters.
Output
For each test case, the output must follow the description below.
The output consists of one single line with a integer stating the maximum number of people that
Can? y from the origin to the destination city, using the given? Ights and arriving at or before the given
Latest arrival time.

Sample Input

4
Lisbon
Berlin
1500
9
Lisbon London 6 1000 1100
London Lisbon 6 1130 1230
Lisbon Paris 5 1000 1100
Paris Lisbon 4 1130 1230
London Paris 1 1130 1300
London Berlin 2 1340 1510
Berlin London 2 1300 1430
Paris Berlin 10 1330 1500
Berlin Paris 9 1300 1430

Sample Output

6

Analysis

The title probably says that there are N cities and m routes, each route is from a certain point in time to arrive at a certain city, and each route has the maximum number of restrictions. It is now known as the departure city and destination city as well as a final moment to ask how many people can reach their destination before the final moment.

This problem will naturally want to think of the city as a point, the route as side. In this way the city needs to be 24*60*100=144000 by time, too much. However, the fact that there are even points at most will only have 2*m, that is, 10,000, the other more than 10 w are furnishings, so in fact, OK.

The positive solution of this problem is:

    • Take the route as a point and split it into two X and X ', X for the route, and another X ' for the end of the course;
    • Creates a new source point, the source point to all from the departure city is the edge of the route X capacity inf of the originating city, and all arrivals to the city are destination and the arrival time is before the final moment of the route X ' to the edge of the meeting point of the capacity inf;
    • All routes, X-to-X ' are limited by the number of routes, which indicates the maximum number of passengers that can flow from the beginning to the end of the route;
    • For all routes A and B, meet route A to the city for Route B and the arrival time of a route + 30 minutes before the start of the B route, from a ' to B, the edge of the capacity INF, so that the end of a route can start the B route;
    • Running the maximum flow is the result.

In fact, this solution can also be seen as the city by time to split the point and discretization.

But I did not expect to really be 10,000 points to run the network stream. In addition, the thinking is not limited, the point and the edge of the figure can be a variety of things. To boldly build a graph to run the network stream.

Code
#include <cstdio> #include <cstring> #include <iostream> #include <string> #include <queue > #include <algorithm>using namespace std, #define INF (1<<30) #define MAXN 11111#define MAXM 2222222struct edge{int v,cap,flow,next;} Edge[maxm];int vs,vt,ne,nv;int head[maxn];void addedge (int u,int v,int cap) {edge[ne].v=v; edge[ne].cap=cap; edge[NE].f    low=0; Edge[ne].next=head[u];    head[u]=ne++; Edge[ne].v=u; Edge[ne].cap=0;    edge[ne].flow=0; EDGE[NE].NEXT=HEAD[V]; head[v]=ne++;}    int level[maxn];int gap[maxn];void BFS () {memset (level,-1,sizeof (level));    memset (Gap,0,sizeof (GAP));    level[vt]=0;    gap[level[vt]]++;    Queue<int> que;    Que.push (VT);        while (!que.empty ()) {int U=que.front (); Que.pop ();            for (int i=head[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;            if (level[v]!=-1) continue;            level[v]=level[u]+1;            gap[level[v]]++;        Que.push (v); }}}int pre[maxn];int Cur[mAxn];int Isap () {BFS ();    memset (pre,-1,sizeof (pre));    memcpy (cur,head,sizeof (head));    int u=pre[vs]=vs,flow=0,aug=inf;    GAP[0]=NV;        while (LEVEL[VS]&LT;NV) {bool Flag=false;            for (int &i=cur[u]; i!=-1; i=edge[i].next) {int v=edge[i].v;                if (Edge[i].cap!=edge[i].flow && level[u]==level[v]+1) {flag=true;                Pre[v]=u;                U=v;                Aug= (Aug==-1?edge[i].cap:min (Aug,edge[i].cap));                Aug=min (Aug,edge[i].cap-edge[i].flow);                    if (V==VT) {Flow+=aug;                        for (u=pre[v]; v!=vs; V=u,u=pre[u]) {Edge[cur[u]].flow+=aug;                    Edge[cur[u]^1].flow-=aug;                    }//aug=-1;                Aug=inf;            } break;        }} if (flag) continue;        int MINLEVEL=NV; for (int i=head[u]; i!=-1; i=edge[i].next) {intV=EDGE[I].V;                if (Edge[i].cap!=edge[i].flow && level[v]<minlevel) {minlevel=level[v];            Cur[u]=i;        }} if (--gap[level[u]]==0) break;        level[u]=minlevel+1;        gap[level[u]]++;    U=pre[u]; } return flow;} int covert_time (int t) {return t/100*60+t%100;}    String city[111];string from[5555],to[5555];int cap[5555],s[5555],t[5555];int main () {int n,m;        while (cin>>n) {string s_city,t_city;        cin>>s_city>>t_city; int deadline;        cin>>deadline;        Deadline=covert_time (deadline);        cin>>m;            for (int i=1; i<=m; ++i) {cin>>from[i]>>to[i]>>cap[i]>>s[i]>>t[i];            S[i]=covert_time (S[i]);        T[i]=covert_time (T[i]); } vs=0; vt=m*2+1; nv=vt+1;        ne=0;        memset (head,-1,sizeof (head));       for (int i=1; i<=m; ++i) {if (from[i]==s_city) Addedge (Vs,i,inf);     if (to[i]==t_city && t[i]<=deadline) Addedge (I+m,vt,inf);        Addedge (I,i+m,cap[i]);                } for (int i=1, i<=m; ++i) {for (int j=1; j<=m; ++j) {if (i==j) continue;            if (To[i]==from[j] && t[i]+30<=s[j]) Addedge (I+m,j,inf);    }} printf ("%d\n", ISAP ()); } return 0;}

UVa1161 Objective:berlin (Max Stream)

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.