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
 
我么你先考虑,最大流,如果一个图中让你删掉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());
}
}

图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)的更多相关文章

  1. HDU 2485 Destroying the bus stations(!最大流∩!费用流∩搜索)

    Description Gabiluso is one of the greatest spies in his country. Now he’s trying to complete an “im ...

  2. HDU 2485 Destroying the bus stations(费用流)

    http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意: 现在要从起点1到终点n,途中有多个车站,每经过一个车站为1时间,现在要在k时间内到达终点,问至少要 ...

  3. hdu 2485 Destroying the bus stations 最小费用最大流

    题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...

  4. HDU 2485 Destroying the bus stations (IDA*+ BFS)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1, ...

  5. HDU 2485 Destroying the bus stations

    2015 ACM / ICPC 北京站 热身赛 C题 #include<cstdio> #include<cstring> #include<cmath> #inc ...

  6. 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)

    Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...

  7. 【hdu 4859】海岸线(图论--网络流最小割)

    题意:有一个区域,有'.'的陆地,'D'的深海域,'E'的浅海域.其中浅海域可以填充为陆地.这里的陆地区域不联通,并且整个地图都处在海洋之中.问填充一定浅海域之后所有岛屿的最长的海岸线之和. 解法:最 ...

  8. 【LA 3487】Duopoly(图论--网络流最小割 经典题)

    题意:C公司有一些资源,每种只有1个,有A.B两个公司分别对其中一些资源进行分组竞标,每组竞标对一些资源出一个总价.问C公司的最大收益. 解法:最小割.将A公司的竞标与源点相连,B公司的与汇点相连,边 ...

  9. 【uva 1515】Pool construction(图论--网络流最小割 模型题)

    题意:有一个水塘,要求把它用围栏围起来,每个费用为b.其中,(#)代表草,(.)代表洞,把一个草变成洞需要费用d, 把一个洞变成草需要费用f.请输出合法方案中的最小费用. 解法:(不好理解...... ...

随机推荐

  1. windows的注册表有什么用?

    注册表是什么 注册表是Microsoft Windows中的一个重要的数据库,用于存储系统和应用程序的设置信息.早在Windows 3.0推出OLE技术的时候,注册表就已经出现.随后推出的Window ...

  2. Swing组件中URL方法获取 图标

    13.3.2 图标的使用 1.创建图标 package lll; import java.awt.*; import javax.swing.*; public class DrawIcon impl ...

  3. 创建堆(python)

    创建最大(小)堆 二叉堆本质上是一种完全二叉树,存储方式并不是链式存储,而是顺序存储 堆操作:插入(叶子节点上调),删除(堆顶元素下沉) 堆创建:非叶子节点下沉(从最后一个非叶子节点开始) 最小堆: ...

  4. SQL基础系列(2)-内置函数--转载w3school

    1.    日期函数 Mssql: SELECT GETDATE() 返回当前日期和时间 SELECT DATEPART(yyyy,OrderDate) AS OrderYear, DATEPART( ...

  5. python3(十三)map reduce

    # map()函数接收两个参数,一个是函数,一个是Iterable, # map将传入的函数依次作用到序列的每个元素,并把结果作为新的Iterator返回. def f(x): return x * ...

  6. 【java 数据结构】还不会二叉树?一篇搞定二叉树

    二叉树是我们常见的数据结构之一,在学习二叉树之前我们需要知道什么是树,什么是二叉树,本篇主要讲述了二叉树,以及二叉树的遍历. 你能get到的知识点? 1.树的介绍 2.二叉树的介绍 3.二叉树遍历的四 ...

  7. Codeup 25609 Problem I 习题5-10 分数序列求和

    题目描述 有如下分数序列 2/1,3/2,5/3,8/5,13/8,21/13 - 求出次数列的前20项之和. 请将结果的数据类型定义为double类型. 输入 无 输出 小数点后保留6位小数,末尾输 ...

  8. GeoGebra函数使用

    分段函数使用 输入指令: If(x < -2, x, -2 < x < 2, x², x > 2, x)

  9. 搞懂 XML 解析,徒手造 WEB 框架

    恕我斗胆直言,对开源的 WEB 框架了解多少,有没有尝试写过框架呢?XML 的解析方式有哪些?能答出来吗?! 心中没有答案也没关系,因为通过今天的分享,能让你轻松 get 如下几点,绝对收获满满. a ...

  10. stand up meeting 12/11/2015

    part 组员 今日工作 工作耗时/h 明日计划 工作耗时/h UI 冯晓云 完成单词释义热度排序:允许用户自主添加释义:完成了button位置的修正(finally)和弹窗的美化:     6 tr ...