Destroying the bus stations
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 2240 Accepted Submission(s): 716
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.
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.
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
分析:起点发起最短路,由终点发起最短路,如果某一点满足dis_s【x】+dis_t【x】<k,则加入途中,求最小割;
代码示例:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define Lh 5000
#define Le 50000
#define max 1000000000
using namespace std;
typedef struct
{
int to;
int w;
int next;
}node;
typedef struct
{
int x;
int t;
}DEP;
typedef struct
{
int a,b;
}nodf;
node E[Le];
nodf e[Le];
DEP fir,nex;
int head[Lh],headx[Lh],deep[Lh],cnt;
int mark[100][100],use[100][100];
node Es1[Le],Es2[Le];
int heads1[Lh],heads2[Lh],dis1[Lh],dis2[Lh],cnts1,cnts2;
void ADDs1(int a,int b,int c)
{
Es1[++cnts1].to=b;
Es1[cnts1].w=c;
Es1[cnts1].next=heads1[a];
heads1[a]=cnts1;
}
void ADDs2(int a,int b,int c)
{
Es2[++cnts2].to=b;
Es2[cnts2].w=c;
Es2[cnts2].next=heads2[a];
heads2[a]=cnts2;
}
void spfa1(int s,int n)
{
for(int i=0;i<=n;i++)
dis1[i]=max;
int visit[Lh];
memset(visit,0,sizeof(visit));
queue<int>Q;
dis1[s]=0;
Q.push(s);
while(!Q.empty())
{
int k=Q.front();
Q.pop();
visit[k]=0;
for(int i=heads1[k];i;i=Es1[i].next)
{
int to=Es1[i].to;
int w=Es1[i].w;
if(dis1[k]+w<dis1[to])
{
dis1[to]=dis1[k]+w;
if(!visit[to])
{
visit[to]=1;
Q.push(to);
}
}
}
}
}
void spfa2(int s,int n)
{
for(int i=0;i<=n;i++)
dis2[i]=max;
int visit[Lh];
memset(visit,0,sizeof(visit));
dis2[s]=0;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int k=Q.front();
Q.pop();
visit[k]=0;
for(int i=heads2[k];i;i=Es2[i].next)
{
int to=Es2[i].to;
int w=Es2[i].w;
if(dis2[k]+w<dis2[to])
{
dis2[to]=dis2[k]+w;
if(!visit[to])
{
visit[to]=1;
Q.push(to);
}
}
}
}
}
///
void ADD(int a,int b,int c)
{
E[++cnt].to=b;
E[cnt].w=c;
E[cnt].next=head[a];
head[a]=cnt;
E[++cnt].to=a;
E[cnt].w=0;
E[cnt].next=head[b];
head[b]=cnt;
}
int min(int x,int y)
{
return x<y?x:y;
}
int bfs(int s,int t,int n)
{
memset(deep,255,sizeof(deep));
queue<DEP>Q;
fir.x=s;
fir.t=0;
deep[s]=0;
Q.push(fir);
while(!Q.empty())
{
fir=Q.front();
Q.pop();
for(int i=head[fir.x];i;i=E[i].next)
{
nex.x=E[i].to;
nex.t=fir.t+1;
if(deep[nex.x]!=-1||!E[i].w)
continue;
deep[nex.x]=nex.t;
Q.push(nex);
}
}
for(int i=0;i<=n;i++)
headx[i]=head[i];
return deep[t]!=-1;
}
int dfs(int s,int t,int flow)
{
if(s==t)
return flow;
int newflow=0;
for(int i=headx[s];i;i=E[i].next)
{
headx[s]=i;
int to=E[i].to;
int w=E[i].w;
if(!w||deep[to]!=deep[s]+1)
continue;
int temp=dfs(to,t,min(w,flow-newflow));
newflow+=temp;
E[i].w-=temp;
E[i^1].w+=temp;
if(newflow==flow)
break;
}
if(!newflow)deep[s]=0;
return newflow;
}
int Dinic(int s,int t,int m)
{
int sum=0;
while(bfs(s,t,m))
{
sum+=dfs(s,t,max);
}
return sum;
}
void creat(int n)
{
for(int i=0;i<Lh;i++)
{
head[i]=heads1[i]=heads2[i]=0;
}
memset(mark,0,sizeof(mark));
memset(use,0,sizeof(use));
cnt=1;
cnts1=1;
cnts2=1;
}
int main()
{
int n,m,k;
int a,b;
int num;
while(~scanf("%d%d%d",&n,&m,&k))
{
if(n==0&&m==0&&k==0)
break;
creat(n);
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
if(!use[a][b])
{
ADDs1(a,b,1);
ADDs2(b,a,1);
mark[a][b]=1;
use[a][b]=1;
}
}
spfa1(1,n);
spfa2(n,n);
num=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
if(mark[i][j]&&dis1[i]+dis2[j]<k)
{
ADD(i+n,j,max);
num++;
}
ADD(i,i+n,1);
num++;
}
int w=Dinic(1+n,n,2*n);
printf("%d\n",w);
}
return 0;
}
#include<string.h>
#include<algorithm>
#include<iostream>
#include<queue>
#define Lh 5000
#define Le 50000
#define max 1000000000
using namespace std;
typedef struct
{
int to;
int w;
int next;
}node;
typedef struct
{
int x;
int t;
}DEP;
typedef struct
{
int a,b;
}nodf;
node E[Le];
nodf e[Le];
DEP fir,nex;
int head[Lh],headx[Lh],deep[Lh],cnt;
int mark[100][100],use[100][100];
node Es1[Le],Es2[Le];
int heads1[Lh],heads2[Lh],dis1[Lh],dis2[Lh],cnts1,cnts2;
void ADDs1(int a,int b,int c)
{
Es1[++cnts1].to=b;
Es1[cnts1].w=c;
Es1[cnts1].next=heads1[a];
heads1[a]=cnts1;
}
void ADDs2(int a,int b,int c)
{
Es2[++cnts2].to=b;
Es2[cnts2].w=c;
Es2[cnts2].next=heads2[a];
heads2[a]=cnts2;
}
void spfa1(int s,int n)
{
for(int i=0;i<=n;i++)
dis1[i]=max;
int visit[Lh];
memset(visit,0,sizeof(visit));
queue<int>Q;
dis1[s]=0;
Q.push(s);
while(!Q.empty())
{
int k=Q.front();
Q.pop();
visit[k]=0;
for(int i=heads1[k];i;i=Es1[i].next)
{
int to=Es1[i].to;
int w=Es1[i].w;
if(dis1[k]+w<dis1[to])
{
dis1[to]=dis1[k]+w;
if(!visit[to])
{
visit[to]=1;
Q.push(to);
}
}
}
}
}
void spfa2(int s,int n)
{
for(int i=0;i<=n;i++)
dis2[i]=max;
int visit[Lh];
memset(visit,0,sizeof(visit));
dis2[s]=0;
queue<int>Q;
Q.push(s);
while(!Q.empty())
{
int k=Q.front();
Q.pop();
visit[k]=0;
for(int i=heads2[k];i;i=Es2[i].next)
{
int to=Es2[i].to;
int w=Es2[i].w;
if(dis2[k]+w<dis2[to])
{
dis2[to]=dis2[k]+w;
if(!visit[to])
{
visit[to]=1;
Q.push(to);
}
}
}
}
}
///
void ADD(int a,int b,int c)
{
E[++cnt].to=b;
E[cnt].w=c;
E[cnt].next=head[a];
head[a]=cnt;
E[++cnt].to=a;
E[cnt].w=0;
E[cnt].next=head[b];
head[b]=cnt;
}
int min(int x,int y)
{
return x<y?x:y;
}
int bfs(int s,int t,int n)
{
memset(deep,255,sizeof(deep));
queue<DEP>Q;
fir.x=s;
fir.t=0;
deep[s]=0;
Q.push(fir);
while(!Q.empty())
{
fir=Q.front();
Q.pop();
for(int i=head[fir.x];i;i=E[i].next)
{
nex.x=E[i].to;
nex.t=fir.t+1;
if(deep[nex.x]!=-1||!E[i].w)
continue;
deep[nex.x]=nex.t;
Q.push(nex);
}
}
for(int i=0;i<=n;i++)
headx[i]=head[i];
return deep[t]!=-1;
}
int dfs(int s,int t,int flow)
{
if(s==t)
return flow;
int newflow=0;
for(int i=headx[s];i;i=E[i].next)
{
headx[s]=i;
int to=E[i].to;
int w=E[i].w;
if(!w||deep[to]!=deep[s]+1)
continue;
int temp=dfs(to,t,min(w,flow-newflow));
newflow+=temp;
E[i].w-=temp;
E[i^1].w+=temp;
if(newflow==flow)
break;
}
if(!newflow)deep[s]=0;
return newflow;
}
int Dinic(int s,int t,int m)
{
int sum=0;
while(bfs(s,t,m))
{
sum+=dfs(s,t,max);
}
return sum;
}
void creat(int n)
{
for(int i=0;i<Lh;i++)
{
head[i]=heads1[i]=heads2[i]=0;
}
memset(mark,0,sizeof(mark));
memset(use,0,sizeof(use));
cnt=1;
cnts1=1;
cnts2=1;
}
int main()
{
int n,m,k;
int a,b;
int num;
while(~scanf("%d%d%d",&n,&m,&k))
{
if(n==0&&m==0&&k==0)
break;
creat(n);
for(int i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
if(!use[a][b])
{
ADDs1(a,b,1);
ADDs2(b,a,1);
mark[a][b]=1;
use[a][b]=1;
}
}
spfa1(1,n);
spfa2(n,n);
num=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
if(mark[i][j]&&dis1[i]+dis2[j]<k)
{
ADD(i+n,j,max);
num++;
}
ADD(i,i+n,1);
num++;
}
int w=Dinic(1+n,n,2*n);
printf("%d\n",w);
}
return 0;
}