hdu 2485 Destroying the bus stations 最小费用最大流
题意:
最少需要几个点才能使得有向图中1->n的距离大于k。
分析:
删除某一点的以后,与它相连的所有边都不存在了,相当于点的容量为1。但是在网络流中我们只能直接限制边的容量。所以需要拆点来完成对的点容量的限制。对于边i -> j,先建边i ->i',再建i'->j。i ->i'只能建一次,容量为1,费用为0。i'->j的容量是INF。此题中因为已经有源点,所以源点(1)不能限制容量。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N =, M=,INF=0x3f3f3f3f;
struct node
{
int to, next, c ,f;//c是容量,f是费用
}edge[M];
int head[N],dis[N],load[N],p[N];
bool vis[N];
int tot,flow,ans,D;
bool spfa(int S, int E,int n)
{
int que[N*],qout,qin;
memset(vis,,sizeof(vis));
memset(load,-,sizeof(load));
memset(p,-,sizeof(p));
for(int i=;i<=n;i++)
dis[i]=INF;
qin=qout=;
que[qin++]=S;
dis[S]=;
vis[S]=;
while(qin!=qout)
{
int u=que[qout++];
vis[u]=;
for(int i=head[u];i!=-;i=edge[i].next)
{
if(edge[i].c)
{
int v=edge[i].to;
if(dis[v]-dis[u]>edge[i].f)
{
dis[v]=dis[u]+edge[i].f;
p[v]=u;
load[v]=i;
if(!vis[v])
{
vis[v]=;
que[qin++]=v;
}
}
}
}
}
if(dis[E]>D) return ;
return ;
}
void MCF(int S, int E,int n)
{
int u,mn;
flow=ans=;
while(spfa(S,E,n))
{
u=E; mn=INF;
while(p[u]!=-)
{
mn=min(edge[load[u]].c, mn);
u=p[u];
}
u=E;
while(p[u]!=-)
{
edge[load[u]].c-=mn;
edge[load[u]^].c+=mn;
u=p[u];
}
ans+=dis[E]*mn;
flow+=mn;
}
}
void addedge(int a,int b,int c,int d)
{
edge[tot].to=b;edge[tot].c=c;edge[tot].f=d;
edge[tot].next=head[a];head[a]=tot++;
edge[tot].to=a;edge[tot].c=;edge[tot].f=-d;
edge[tot].next=head[b];head[b]=tot++;
}
void init()
{
tot=;
memset(head,-,sizeof(head));
}
int main()
{
//freopen("test.txt","r",stdin);
int n,m,i,j,k;
while(scanf("%d%d%d",&n,&m,&D)!=EOF&&n)
{
init();
while(m--)
{
scanf("%d%d",&i,&j);
if(i!=) addedge(i+n,j,INF,);
else addedge(i,j,INF,);
}
for(i=;i<n;i++) addedge(i,i+n,,);
MCF(,n,*n);
printf("%d\n",flow);
}
return ;
}
hdu 2485 Destroying the bus stations 最小费用最大流的更多相关文章
- 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(!最大流∩!费用流∩搜索)
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(最短路+限流建图)
Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...
- 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 ...
- SCU3033 Destroying a Painting(最小费用最大流)
题目大概说有一个有n*m个格子的画板,画板上每个格子都有颜色,现在要把所有格子的颜色改成红.绿或者蓝,改变的代价是二者RGB值的曼哈顿距离,还要求红绿蓝格子个数的最大值和最小值要尽可能接近,问最少的代 ...
- HDUOJ----2485 Destroying the bus stations(2008北京现场赛A题)
Destroying the bus stations ...
- hdu 2686 Matrix 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2686 Yifenfei very like play a number game in the n*n ...
- hdu 1533 Going Home 最小费用最大流
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1533 On a grid map there are n little men and n house ...
随机推荐
- Android LinearLayout整个布局设置不可点击
1,activity的xml布局(布局中有个Button按钮,点击按钮弹出一个popupwindow ) <?xml version="1.0" encoding=" ...
- Ubuntu下解压(unzip)乱码的解决方法
在Windows上压缩的文件,是以Windows系统默认编码中文来压缩文件.由于zip文件中没有声明其编码,所以linux上的unzip一般以默认编码解压,中文文件名会出现乱码. 通过unzip -- ...
- golang入门-defer
package main import "fmt" func main() { i := 5 tmap := make(map[string]int, 5) tmap[" ...
- python PIL图像处理-生成图片验证码
生成效果如图: 代码 from PIL import Image,ImageDraw,ImageFont,ImageFilter import random # 打开一个jpg图像文件: im = I ...
- S-HR薪酬项目与核算表的关系
- 继续聊WPF——Expander控件(2)
<Window x:Class="Expander_Sample2.Window1" xmlns="http://schemas.microsoft.com/win ...
- CentOS 7 x64下Apache+MySQL(Mariadb)+PHP5.6的安装
每次搭建新服务器,都要来来回回把这些包再装一下,来来回回搞了不下20遍了吧,原来都是凭经验,配置过程中重复入坑是难免的,故写此文做个备忘.虽然有像xampp这样的集成包,但是在生产环境的Linux发行 ...
- 修改oracle客户端的字符集
客户端字符集环境select * from nls_instance_parameters,其来源于v$parameter, 表示客户端的字符集的设置,可能是参数文件,环境变量或者是注册表 方法有 : ...
- perl的安装
http://www.activestate.com/activeperl/downloads 安装的时候,默认把perl放置到环境变量的PATH中 之后,需要重启电脑,确保环境变量生效 执行perl ...
- DDos攻击篇
DDoS(Distributed Denial of Service,分布式拒绝服务)攻击的主要目的是让指定目标无法提供正常服务,甚至从互联网上消失,是目前最强大.最难防御的攻击之一. 1.1. SY ...