图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)
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
Sample Output
2
我么你先考虑,最大流,如果一个图中让你删掉N个点使得源点不能到达汇点, 这里设置边的流量为1,然后一边最大流即可。
然后我们要考虑每条路只能走一遍,所以不能直接连中间的点,要限流建图,将中间的点,一分为2,左边作为起始点,右边作为终止点,流量设置为1,这样就可现之每条边之走一遍(这样想,省略了最小割转化分过程,我一直认为最小割最大流没啥区别),现在有个K的限制,我们怎么办呢?之前我们不是拆点了?那么对于源点到左边的起始点的距离加上起始点到右边终止点的距离再加上终止点到原点的距离小于K不就可以了吗,大于K已经不可能在K时间内到达了,忽略即可!
明了点就是
一个点拆成了 I和I' 如果dis[s][u]+dis[u][v]+dis[v][t]<=K的话,我们就添一条U到V',注意是V一撇。然后最大刘 能赵四谢广坤。
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<queue>
#define INF 1e9
using namespace std;
const int maxn=1000+10;
const int maxm=100000+10;
struct Edge
{
int from,to,cap,flow;
Edge(){}
Edge(int f,int t,int c,int fl):from(f),to(t),cap(c),flow(fl){}
};
struct Dinic
{
int n,m,s,t;
vector<Edge> edges;
vector<int> G[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n,int s,int t)
{
this->n=n, this->s=s, this->t=t;
edges.clear();
for(int i=1;i<=n;++i) G[i].clear();
}
void AddEdge(int from,int to,int cap)
{
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool BFS()
{
queue<int> Q;
memset(vis,0,sizeof(vis));
vis[s]=true;
d[s]=0;
Q.push(s);
while(!Q.empty())
{
int x=Q.front(); Q.pop();
for(int i=0;i<G[x].size();++i)
{
Edge &e=edges[G[x][i]];
if(!vis[e.to] && e.cap>e.flow)
{
vis[e.to]=true;
d[e.to]= d[x]+1;
Q.push(e.to);
}
}
}
return vis[t];
}
int DFS(int x,int a)
{
if(x==t || a==0) return a;
int flow=0, f;
for(int &i=cur[x];i<G[x].size();++i)
{
Edge &e=edges[G[x][i]];
if(d[e.to]==d[x]+1 && (f=DFS(e.to,min(a,e.cap-e.flow) ) )>0)
{
e.flow +=f;
edges[G[x][i]^1].flow -=f;
flow +=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int max_flow()
{
int ans=0;
while(BFS())
{
memset(cur,0,sizeof(cur));
ans+=DFS(s,INF);
}
return ans;
}
}DC;
struct Edge_dist
{
int from,to,dist;
Edge_dist(){}
Edge_dist(int f,int t,int d):from(f),to(t),dist(d){}
}edges[maxm];
struct HeapNode
{
int d,u;
HeapNode(){}
HeapNode(int d,int u):d(d),u(u){}
bool operator<(const HeapNode& rhs)const
{
return d>rhs.d;
}
};
struct Dijkstra
{
int n,m;
vector<Edge_dist> edges;
vector<int> G[maxn];
bool done[maxn];
int d[maxn];
void init(int n)
{
this->n=n;
edges.clear();
for(int i=1;i<=n;++i) G[i].clear();
}
void AddEdge(int from,int to,int dist)
{
edges.push_back(Edge_dist(from,to,dist));
m=edges.size();
G[from].push_back(m-1);
}
void dijkstra(int s)
{
priority_queue<HeapNode> Q;
for(int i=1;i<=n;++i) d[i]=INF;
d[s]=0;
memset(done,0,sizeof(done));
Q.push(HeapNode(d[s],s));
while(!Q.empty())
{
HeapNode x=Q.top(); Q.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
for(int i=0;i<G[u].size();++i)
{
Edge_dist &e=edges[G[u][i]];
if(d[e.to]>d[u]+e.dist)
{
d[e.to]=d[u]+e.dist;
Q.push(HeapNode(d[e.to],e.to));
}
}
}
}
}DJ1,DJ2;
int main()
{
int T; scanf("%d",&T);
while(T--)
{
int n,m,cnt=0;//cnt来保存有效边数
scanf("%d%d",&n,&m);
DJ1.init(n),DJ2.init(n);
while(m--)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if(u!=v)
{
DJ1.AddEdge(u,v,w);
DJ2.AddEdge(v,u,w);
edges[cnt++]=Edge_dist(u,v,w);
}
}
int src,dst,len;
scanf("%d%d",&src,&dst);
DJ1.dijkstra(src),DJ2.dijkstra(dst);
len = DJ1.d[dst];
if(len==INF)//不能到达
{
printf("0\n");
continue;
}
DC.init(n,src,dst);
for(int i=0;i<cnt;i++)
{
int u=edges[i].from,v=edges[i].to,w=edges[i].dist;
if(DJ1.d[u]+DJ2.d[v]+w == len)
DC.AddEdge(u,v,1);
}
printf("%d\n",DC.max_flow());
}
}