图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)
Problem Description
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.
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.
#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(最短路+限流建图)的更多相关文章
- 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 ...
- HDU 2485 Destroying the bus stations(费用流)
http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意: 现在要从起点1到终点n,途中有多个车站,每经过一个车站为1时间,现在要在k时间内到达终点,问至少要 ...
- hdu 2485 Destroying the bus stations 最小费用最大流
题意: 最少需要几个点才能使得有向图中1->n的距离大于k. 分析: 删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1.但是在网络流中我们只能直接限制边的容量.所以需要拆点来完成 ...
- HDU 2485 Destroying the bus stations (IDA*+ BFS)
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2485 题意:给你n个点,m条相连的边,问你最少去掉几个点使从1到n最小路径>=k,其中不能去掉1, ...
- HDU 2485 Destroying the bus stations
2015 ACM / ICPC 北京站 热身赛 C题 #include<cstdio> #include<cstring> #include<cmath> #inc ...
- 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)
Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...
- 【hdu 4859】海岸线(图论--网络流最小割)
题意:有一个区域,有'.'的陆地,'D'的深海域,'E'的浅海域.其中浅海域可以填充为陆地.这里的陆地区域不联通,并且整个地图都处在海洋之中.问填充一定浅海域之后所有岛屿的最长的海岸线之和. 解法:最 ...
- 【LA 3487】Duopoly(图论--网络流最小割 经典题)
题意:C公司有一些资源,每种只有1个,有A.B两个公司分别对其中一些资源进行分组竞标,每组竞标对一些资源出一个总价.问C公司的最大收益. 解法:最小割.将A公司的竞标与源点相连,B公司的与汇点相连,边 ...
- 【uva 1515】Pool construction(图论--网络流最小割 模型题)
题意:有一个水塘,要求把它用围栏围起来,每个费用为b.其中,(#)代表草,(.)代表洞,把一个草变成洞需要费用d, 把一个洞变成草需要费用f.请输出合法方案中的最小费用. 解法:(不好理解...... ...
随机推荐
- MTK Android Android数据保存到系统数据库
如果有留意Android中系统设置Settings里面的源码,你会发现代码中频繁用到了Settings.System操作,该类通过键值对的形式,将一些特定的值以全局的模式保存到Setting的数据库中 ...
- lr具体使用步骤概述
lr具体使用 1 无工具情况下的性能测试 2性能测试工具LoadRunner的工作原理 3 VuGen应用介绍 4 协议的类型及选择方法 5 脚本的创建过程 6 脚本的参数化 7 调试技术 8 Con ...
- leetcode Perform String Shifts
Perform String Shifts You are given a string s containing lowercase English letters, and a matrix sh ...
- Github上面拉取别人提交的PR
在github上面协同开发,避免不了拉取别的同学的PR,那么如何拉取呢? 1.首先我们看下upstream liz@liz-PC:~/jimeng/handle-api$ git remote -v ...
- Linux中使用netstat命令的基本操作,排查端口号的占用情况
Linux中netstat命令详解 Netstat是控制台命令,是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表.实际的网络连接以及每一个网络接口设备的状态信息.Netstat用于显示与I ...
- std::string 字符串分割
#include <iostream> #include <string> #include <vector> std::vector<std::string ...
- std::forward和std::move
std::forward完美转发 保证参数原来的属性(用在template的参数是引用的时候):左值引用在被转发之后仍然保持左值属性,右值引用在被转发之后依然保持右值属性 void show(int& ...
- 【Java】步入OOP 面向对象
面向对象编程 OOP Object Oriented Programming 面向对象是一种对现实世界理解和抽象的方法,是计算机编程技术发展到一定阶段后的产物. 面向对象是相对于面向过程来讲的,面向对 ...
- mysql datetime类型 按格式在页面输出
mysql datetime类型对应java Date类型 java.util.Date类型会显示时间戳 java.sql.Date 只显示年月日不显示时分秒 只需要重写get方法 就能按格式输出 ...
- linux之cat 操作
1.查看或创建 cat 1.txt #如果目录有这个文件则会打开查看,没有则会创建 2.压缩空白 cat 1.txt 我是第一行 我是第二 行 cat -bs 1.txt # 变成 cat 1.txt ...