HDU(2485),最小割最大流
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=2485
Destroying the bus stations
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2651 Accepted Submission(s): 891
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.
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.
1 3
3 4
4 5
1 2
2 5
1 4
4 5
0 0 0
给定n个点, m条有向边 ,k
下面m条有向边
问删最少几个点使得1-n的最短路>k
分析:
#include <iostream>
#include <stdio.h>
#include <cstring>
#include <vector>
#include <queue>
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = + ;
int k; struct Edge
{
int from,to,cap,flow,cost;
Edge() {}
Edge(int a,int b,int c,int d,int e):from(a),to(b),cap(c),flow(d),cost(e) {}
}; struct MCMF
{
int n,m,s,t;
vector<Edge> edges;
vector<int> g[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn]; void init(int n)
{
this->n =n;
for(int i=; i<n; i++)g[i].clear();
edges.clear();
}
void addedge(int from,int to,int cap,int cost)
{
Edge e1= Edge(from,to,cap,,cost), e2= Edge(to,from,,,-cost);
edges.push_back(e1);
edges.push_back(e2);
m=edges.size();
g[from].push_back(m-);
g[to].push_back(m-);
}
bool spfa(int s,int t, int & flow,int & cost)
{
for(int i=; i<n; i++)
d[i]=INF;
memset(inq,,sizeof(inq));
d[s]=;
inq[s]=;
p[s]=;
a[s]=INF;
queue<int>q;
q.push(s);
while(!q.empty())
{
int u=q.front();
q.pop();
inq[u]=;
for(int i=; i<g[u].size(); i++)
{
Edge & e = edges[g[u][i]];
if(e.cap>e.flow && d[e.to]>d[u]+e.cost)
{
d[e.to]=d[u]+e.cost;
p[e.to]=g[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to])
{
q.push(e.to);
inq[e.to]=;
}
}
}
}
if(d[t]>k)
return false;
if(d[t]==INF)
return false; flow+=a[t];
cost+=a[t]*d[t];
for(int u=t; u!=s; u=edges[p[u]].from)
{
edges[p[u]].flow +=a[t];
edges[p[u]^].flow-=a[t];
}
return true;
} int MincostMaxflow(int s,int t)
{
int flow=,cost =;
while(spfa(s,t,flow,cost));
return flow;
}
} sol; int main()
{
freopen("input.txt","r",stdin);
int n,m;
while(scanf("%d%d%d",&n,&m,&k))
{
int s = ,t = *n+;
if(n==&&m==&&k==) break;
int u,v;
sol.init(n*+);
for(int i=; i<=n; i++)
sol.addedge(i+n,i,,); sol.addedge(,+n,INF,);
sol.addedge(n,*n,INF,);
sol.addedge(,,INF,);
sol.addedge(*n,t,INF,);
for(int i=; i<m; i++)
{
scanf("%d%d",&u,&v);
sol.addedge(u,v+n,INF,);
}
printf("%d\n",sol.MincostMaxflow(s,t));
}
return ;
}
HDU(2485),最小割最大流的更多相关文章
- hdu 2485(最小费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2485 思路:题目的意思是删除最少的点使1,n的最短路大于k.将点转化为边,容量为1,费用为0,然后就是 ...
- hdu4289 最小割最大流 (拆点最大流)
最小割最大流定理:(参考刘汝佳p369)增广路算法结束时,令已标号结点(a[u]>0的结点)集合为S,其他结点集合为T=V-S,则(S,T)是图的s-t最小割. Problem Descript ...
- 【BZOJ-1797】Mincut 最小割 最大流 + Tarjan + 缩点
1797: [Ahoi2009]Mincut 最小割 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 1685 Solved: 724[Submit] ...
- BZOJ-1001 狼抓兔子 (最小割-最大流)平面图转对偶图+SPFA
1001: [BeiJing2006]狼抓兔子 Time Limit: 15 Sec Memory Limit: 162 MB Submit: 14686 Solved: 3513 [Submit][ ...
- hdu1569 方格取数(2) 最大点权独立集=总权和-最小点权覆盖集 (最小点权覆盖集=最小割=最大流)
/** 转自:http://blog.csdn.net/u011498819/article/details/20772147 题目:hdu1569 方格取数(2) 链接:https://vjudge ...
- BZOJ1001:狼抓兔子(最小割最大流+vector模板)
1001: [BeiJing2006]狼抓兔子 Description 现在小朋友们最喜欢的"喜羊羊与灰太狼",话说灰太狼抓羊不到,但抓兔子还是比较在行的,而且现在的兔子还比较笨, ...
- HDU1565 方格取数(1) —— 状压DP or 插头DP(轮廓线更新) or 二分图点带权最大独立集(最小割最大流)
题目链接:https://vjudge.net/problem/HDU-1565 方格取数(1) Time Limit: 10000/5000 MS (Java/Others) Memory L ...
- hdu 3691最小割将一个图分成两部分
转载地址:http://blog.csdn.net/xdu_truth/article/details/8104721 题意:题给出一个无向图和一个源点,让你求从这个点出发到某个点最大流的最小值.由最 ...
- 最小割最大流定理&残量网络的性质
最小割最大流定理的内容: 对于一个网络流图 $G=(V,E)$,其中有源点和汇点,那么下面三个条件是等价的: 流$f$是图$G$的最大流 残量网络$G_f$不存在增广路 对于$G$的某一个割$(S,T ...
- Destroying The Graph 最小点权集--最小割--最大流
Destroying The Graph 构图思路: 1.将所有顶点v拆成两个点, v1,v2 2.源点S与v1连边,容量为 W- 3.v2与汇点连边,容量为 W+ 4.对图中原边( a, b ), ...
随机推荐
- PG sys function
The System Catalogs of PostgreSQLscott=# \dS List of relations Schema | Name | Type | Owner -------- ...
- Leetcode: Water and Jug Problem && Summary: GCD求法(辗转相除法 or Euclidean algorithm)
You are given two jugs with capacities x and y litres. There is an infinite amount of water supply a ...
- 使用gradle创建java程序
创建一个Java项目 我们可以使用Java插件来创建一个Java项目,为了做到这点,我们需要把下面这段语句加入到build.gradle文件中: 1 apply plugin: 'java' 就是这样 ...
- 转:windows 7系统安装与配置Tomcat服务器环境
工具/原料 jdk-7u45-windows-x64(我的系统是64位系统,32位的请选x86下载) apache-tomcat-8.0.0-RC5-windows-x64 方法/步骤 下载说明, ...
- [原创]java WEB学习笔记62:Struts2学习之路--表单标签:form,表单标签的属性,textfield, password, hidden,submit ,textarea ,checkbox ,list, listKey 和 listValue 属性,select ,optiongroup ,checkboxlist
本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...
- visualvm添加远程管理-centos
VisualVM连接远程服务器有两种方式:JMX和jstatd,两种方式都不能完美支持所有功能,例如JMX不支持VisualGC,jstatd不支持CPU监控,实际使用可同时配置上并按需选用. 1.修 ...
- 由ccf第一题引出的问题
当时的情况是这样的,代码中需要用到一个较大的二维数组,但只要定义这个大二维数组编译就出错,无奈后来用malloc自己实现了类似二维数组的操作. 其中的b数组设为全局的或者静态的也都可以解决overfl ...
- sqlhelper-sql数据库
using System; using System.Collections.Generic; using System.Text; using System.Data.SqlClient; usin ...
- z/os上的tar和gzip(2)
前一篇文章写过了如何合成并压缩大批量文件,这篇文章解释一下如何在拿到压缩文件后如何解压并还原大批量文件. 解压缩的JCL很简单,如下所示,和压缩的JCL类似,只要把参数改成UNPACK,然后设置一 ...
- 狗屁不通的“视频专辑:零基础学习C语言(小甲鱼版)”(2)
前文链接:狗屁不通的“视频专辑:零基础学习C语言(小甲鱼版)”(1) 小甲鱼在很多情况下是跟着谭浩强鹦鹉学舌,所以谭浩强书中的很多错误他又重复了一次.这样,加上他自己的错误,错谬之处难以胜数. 由于拙 ...