hdu 2485 Destroying the bus stations【最大流Dinic+最短路floyd】好题

    xiaoxiao2024-11-18  6

    Destroying the bus stations

    Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2619    Accepted Submission(s): 879

    Problem Description

    Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “impossible” mission ----- to make it slow for the army of City Colugu to reach the airport. City Colugu has n bus stations and m roads. Each road connects two bus stations directly, and all roads are one way 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 be more than one road between two bus stations. If a bus station is destroyed, all roads connecting that station will become no use. What’s Gabiluso needs to do is 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 are numbered from 1 to n. The No.1 bus station is in the barrack and the No. n station is in the airport. The army always set out from the No. 1 station. No.1 station and No. n station can’t be destroyed because of the heavy guard. Of course there is no road from No.1 station to No. n station. Please help Gabiluso to calculate the minimum number of bus stations he must destroy to complete his mission.

    Input

    There are 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 that there is a road from station No. s to station No. f. 

    Output

    For each test case, output the minimum number of stations Gabiluso must destroy.

    Sample Input

    5 7 3

    1 3

    3 4

    4 5

    1 2

    2 5

    1 4

    4 5

    0 0 0

    Sample Output

    2

    Source

    2008 Asia Regional Beijing

     

    题目大意:给你n个点,m条无向边,问你最少拆多少个点才能使得从1到n的最短路大于k、

    思路:

    1、首先我们将问题简单化,如果没有这个k的限制,问题转化为:给你n个点,m条有向边,问你最少拆多少个点才能使得从1到达不了n、那么我们很容易想到拆点,将每个节点一分为二,然后两点之间建立一条有向边,权值容量设定为1,表示这个点只能拆一次,问题再次转化:给你n*2个点,好多条有向边,问你最少拆多少条边才能使得从1到不了n。那么问题就变成了:求一个网络的最小割。根据最小割最大流定理,我们跑一遍最大流,得到的最大流的值,也就是最小割的值,也就是最少拆多少个点就能使得从1到达不了n。

    2、问题简单化处理相对看起来比较简单,现在加了一个K这么个限制,看起来变难了,其实如果我们再一次将问题转化成::给你n个点,m条有向边,问你拆多少个点才能使得从1到达不了n,我们还会觉得很难吗?

    3、所以我们现在要处理的中心任务,就是把这个限制k拿掉、因为点比较少,我们直接跑一遍Floyd,求出两点间最短路,然后每条输入进来的边进行计算如下值:

    map【1】【u】+map【u】【v】+map【v】【n】=tmp,如果tmp<=k,那么这条边就是一条关键边,能够连接u,v,使得从1到n的最短路小于等于k,那么我们就建立这条边,如果tmp>k,就是说即使这条边建立了,从1到n的最短路,也不会因为这条边而改变,其实就是说,我们只建立tmp<=k的边,然后得到的图,就是一个无论怎么走,都是从1到n最短路小于等于k的一个图。那么如果我们将这个子图变成了不连通的(拆最少的点使得从1到达不了n),那么这个限制k就被我们搞定了。

    4、整体的思路我们最后构建一遍:

    ①跑一遍最短路Floyd

    ②对应每一条输入进来的边进行判断,如果map【1】【u】+map【u】【v】+map【v】【n】<=k,那么将这条边加入网络中。

    ③我们得到一个子图,这个子图中从1无论怎么走到n都是最短路小于等于k的一个图,如果我们将这个图变成了不连通的,那么对应原图,就是一个从1到n要么是没有路径到达,要么是有路径到达不可能有任何一条路径使得从1到n的最短路小于等于k,那么这个限制k就被我们搞定了。

    ④对应生成出来的子图,我们拆点一分为2,并且在其间建立一条有向边,权值设定为1,表示一个点只能拆一次。特殊拆点,从1到1+n以及从n到n+n这两个点拆分出来之后其间建立的容量为INF.

    ⑤对应每一条加进来的边,我们:add(u+n[u+n表示u拆分出来的点的编号],v,1)

    ⑥设定1为源点S,设定n*2为汇点T。

    5、然后全部搞定之后,跑一遍Dinic即可。

    Ac代码:

    #include<stdio.h> #include<string.h> #include<queue> #include<iostream> #include<algorithm> using namespace std; #define INF 0x3f3f3f3f struct node { int from; int to; int w; int next; }e[1515151]; int bian[4500][2]; int divv[100*100]; int map[55][55]; int cur[100*100]; int head[100*100]; int cont,ss,tt,n,m,k; void add(int from,int to,int w) { e[cont].to=to; e[cont].w=w; e[cont].next=head[from]; head[from]=cont++; } void getmap() { cont=0; ss=1; tt=n*2; memset(head,-1,sizeof(head)); for(int i=1;i<=n;i++) { if(i==1||i==n) { add(i,i+n,0x3f3f3f3f); add(i+n,i,0); continue; } add(i,i+n,1); add(i+n,i,0); } for(int i=0;i<m;i++) { int x=bian[i][0]; int y=bian[i][1]; if(map[1][x]+map[y][n]<k) { add(x+n,y,1); add(y,x+n,0); } } } int makedivv() { memset(divv,0,sizeof(divv)); divv[ss]=1; queue<int >s; s.push(ss); while(!s.empty()) { int u=s.front(); if(u==tt)return 1; s.pop(); for(int i=head[u];i!=-1;i=e[i].next) { int v=e[i].to; int w=e[i].w; if(w&&divv[v]==0) { divv[v]=divv[u]+1; s.push(v); } } } return 0; } int Dfs(int u,int maxflow,int tt) { if(u==tt)return maxflow; int ret=0; for(int &i=cur[u];i!=-1;i=e[i].next) { int v=e[i].to; int w=e[i].w; if(w&&divv[v]==divv[u]+1) { int f=Dfs(v,min(maxflow-ret,w),tt); e[i].w-=f; e[i^1].w+=f; ret+=f; if(ret==maxflow)return ret; } } return ret; } void Dinic() { int ans=0; while(makedivv()==1) { memcpy(cur,head,sizeof(head)); ans+=Dfs(ss,INF,tt); } printf("%d\n",ans); } int main() { while(~scanf("%d%d%d",&n,&m,&k)) { if(n+m+k==0)break; memset(map,0x3f3f3f3f,sizeof(map)); for(int i=1;i<=n;i++)map[i][i]=0; for(int i=0;i<m;i++) { int x,y; scanf("%d%d",&x,&y); bian[i][0]=x; bian[i][1]=y; map[x][y]=1; } for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { for(int k=1;k<=n;k++) { map[j][k]=min(map[j][k],map[j][i]+map[i][k]); } } } getmap(); Dinic(); } }

    转载请注明原文地址: https://ju.6miu.com/read-1293756.html
    最新回复(0)