HDU2485 destroying the bus stations (min cut---point cut)

Source: Internet
Author: User

Destroying the bus stationsTime limit:4000/2000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 2319 Accepted Submission (s): 743


Problem Descriptiongabiluso is one of the greatest spies in his country. Now he's trying to complete a "impossible" mission-----to make it slow for the army of city Colugu to reach the airport . City Colugu have n bus stations and M roads. Each road connects the bus stations directly, and all roads is one, the streets. In order to keep the air clean, the government bans all military vehicles. So the army must take buses to go to the airport. There may is more than one road between bus stations. If A bus station was destroyed, all roads connecting the station would become no use. What's gabiluso needs to do are destroying some bus stations to make the army can ' t get to the airport in K minutes. It takes exactly one minute for a bus to pass any road. All bus stations is numbered from 1 to N. The "the" bus station was in the barrack and the No. N Station was in the airport. The army always set off from the No. 1 station.
Station and No. N station can ' t is destroyed because of the heavy guard. Of course there is no road from the airport to No. N Station.


Please help Gabiluso to calculate the minimum number of bus stations he must destroy to complete his mission.

Inputthere is several test cases. Input ends with three zeros.

For each test case:

The first line contains 3 integers, N, M and K. (0< n <=50, 0< m<=4000, 0 < K < 1000)
Then M lines follows. Each line contains 2 integers, S and F, indicating this there is a road from Station No. s to Station No. f.

Outputfor each test case, output the minimum number of stations gabiluso must destroy.
Sample Input
5 7 31 33 44 51 22 51 44 50 0 0

Sample Output
2

Source2008 Asia Regional Beijing

The problem is to remove at least a few vertices, so that the source and sink point of the graph is not connected (unreachable or time greater than K). Think of the definition of minimum cut: Minimum cut C (s,t) = maximum flow, if the flow of each side of the graph is 1, then the minimum cut becomes the least edge removed, making the source and sink point of the graph is not connected. Divide each vertex in this question into two vertices, with an edge connection with a flow of 1 (u-> (u+n))... Thus, the model satisfies the minimum cut model. Another limitation is that paths with paths longer than k are not considered. Here, we need to first use SPFA preprocessing, the least cost of each vertex to the sink point, for heuristic search to meet the conditions of the augmented flow path , in addition to the point itself to spend time of 0, the other side of the cost is 1, when the path cost is greater than k when the maximum flow process can be terminated.

#include <stdio.h> #include <string.h> #include <queue> #include <algorithm>using namespace std   ; #define Captype intconst int maxn = 1010;    Total number of points const int MAXM = 400010;    Total number of edges const int INF = 1<<30;struct edg{int to,next; Captype Cap,flow;}  Edg[maxm];int Eid,head[maxn];int GAP[MAXN];  The number of points for each distance (or can be considered a height) int DIS[MAXN];  The shortest distance from each point to the end Enode int CUR[MAXN];    Cur[u] indicates that from the U point can flow through the cur[u] number side int pre[maxn];int fromes[maxn],fromet[maxn];void init () {eid=0; memset (head,-1,sizeof (Head));}    There are three parameters to the edge, 4 parameters void addedg (int u,int v,captype c,captype rc=0) without a forward edge {edg[eid].to=v; edg[eid].next=head[u]; Edg[eid].cap=c; edg[eid].flow=0;    head[u]=eid++; Edg[eid].to=u;    EDG[EID].NEXT=HEAD[V]; EDG[EID].CAP=RC; edg[eid].flow=0; head[v]=eid++;}    Captype maxflow_sap (int snode,int enode, int n,int maxtime) {//n is the total number of points including source and sink points, this must be noted memset (Gap,0,sizeof (GAP));    memset (dis,0,sizeof (dis));    memcpy (cur,head,sizeof (head));    memset (fromes,-1,sizeof (fromes)); Fromes[snode]=0;    Pre[snode] =-1;    Gap[0]=n;  Captype ans=0;    Max Stream int U=snode;            while (dis[snode]<n) {//determines from SNode point there is no flow to the next adjacent point if (u==enode) {//Find a way to add flow captype min=inf;            int inser;                for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to])//To find the maximum amount of traffic Min if (min>edg[i].cap-edg[i].flow) {                Min=edg[i].cap-edg[i].flow;            Inser=i;                } for (int i=pre[u]; i!=-1; i=pre[edg[i^1].to]) {edg[i].flow+=min;  Edg[i^1].flow-=min;            Flow//printf ("<-%d", edg[i].to) of the side that can be returned;            } ans+=min;            u=edg[inser^1].to;        Continue  } bool flag = FALSE;        Determine whether you can start from the U point to the adjacent point stream int V, add;                         for (int i=cur[u]; i!=-1; i=edg[i].next) {v=edg[i].to;            ------------------to find an augmented flow------------------------add=0 that satisfies the condition; if (U!=V-N/2) add=1; If the from U-->v is a forward flow and is not a passing pointIts own internal flow, it takes a unit of time, if it is reflux, indicating the flow of reflux to find other available edg[i].cap>0&&fromes[u]+add+fromet[v]>maxtime            Continue            else if (edg[i].cap>0) fromes[v]=fromes[u]+add;//printf ("(%d,%d,%d)", U,v,add); ---------------------------------------------------------------------if (EDG[I].CAP-EDG[I].FLOW&G                T;0 && dis[u]==dis[v]+1) {flag=true;                Cur[u]=pre[v]=i;            Break            }} if (flag) {u=v;        Continue        }//If a flow adjacent point is not found above, then the distance from the starting point U (which can also be considered a height) is the minimum distance of +1 int mind= n for the adjacent flow point;  for (int i=head[u]; i!=-1; i=edg[i].next) {v = edg[i].to;            Add=0;            if (U!=V-N/2) add=1;            if (edg[i].cap>0&&fromes[u]+add+fromet[v]>maxtime) continue;            else if (edg[i].cap>0) Fromes[v]=fromes[u]+add; if (edg[i].cap-edg[i].flow>0 &&amP                Mind>dis[edg[i].to]) {mind=dis[edg[i].to];            Cur[u]=i;        }} gap[dis[u]]--;  if (gap[dis[u]]==0) return ans; When Dis[u] The point of this distance is gone, it is impossible to find an augmented stream path from the source point//Because there is only one distance from the sink point to the current point, then from the source point to the sink point must pass the current point, but the current point is not able to find        Can flow to the point, then the inevitable flow of dis[u]=mind+1;//if a stream of adjacent points is found, the distance is the distance of the adjacent point +1, if not found, then the n+1 gap[dis[u]]++;  if (U!=snode) u=edg[pre[u]^1].to;    Back one Edge}//printf ("#%d", fromes[2]); return ans;}    void SPFA (int s,int t,int n) {queue<int>q;    int inq[maxn]={0};    for (int i=1; i<=n; i++) fromet[i] = INF;    fromet[t]=0;    Q.push (t);        while (!q.empty ()) {int U=q.front (); Q.pop ();        inq[u]=0;            for (int i=head[u]; i!=-1; i=edg[i].next) {int v=edg[i].to, add = 0;            if (U!=V+N/2) add=1;                if (Edg[i].cap==0&&fromet[edg[i].to]>fromet[u]+add) {//both take reverse edge fromet[edg[i].to]=fromet[u]+add;            if (!inq[v])        Inq[v]=1,q.push (v);    }}}}int Main () {int n,m,k;    int u,v;        while (scanf ("%d%d%d", &n,&m,&k) >0) {if (n==0&&m==0&&k==0) break;        Init ();        for (int i=1; i<=n; i++) addedg (i,i+n,1);            while (m--) {scanf ("%d%d", &u,&v);        ADDEDG (u+n,v,1);        } SPFA (1+n,n,n*2);//printf ("%d", fromet[1+n]);        int Ans=maxflow_sap (1+n, N, n*2, k);    printf ("%d\n", ans); }}


HDU2485 destroying the bus stations (min cut---point cut)

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.