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 ...
随机推荐
- javascrip this指向问题深入理解
在JavaScript中this变量是一个令人难以摸清的关键字,this可谓是非常强大,充分了解this的相关知识有助于我们在编写面向对象的JavaScript程序时能够游刃有余. 1. 一般用处 对 ...
- Day9 面向对象进阶
@property装饰器 之前我们讨论过Python中属性和方法访问权限的问题,虽然我们不建议将属性设置为私有的,但是如果直接将属性暴露给外界也是有问题的,比如我们没有办法检查赋给属性的值是否有效.我 ...
- eas之获得任何一个KDTable的选中行
import com.kingdee.bos.ctrl.kdf.table.util.KDTableUtil; int[] selectRows =KDTableUtil.getSelectedRow ...
- IDEA返回上一步
在开发中进入一个方法后想要到原来那行 ctrl+alt+左 回到上一步 ctrl+alt+右 回到下一步
- Windows 安装react native
1.下载node.js (https://nodejs.org/en/) 2.安装node.js,安装完成后按住 图标键+R ,输入CMD进入命令行终端,输入npm -v C:\Users\Admin ...
- ACDream - k-GCD
先上题目: B - k-GCD Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Sub ...
- 基于java注解实现自己的orm框架
ORM即Object Relation Mapping,Object就是对象,Relation就是关系数据库,Mapping映射,就是说Java中的对象和关系数据库中的表存在一种对应关系. 现在常见的 ...
- no projects are found to import
从svn上导出的项目在导入Eclipse中常常出现 no projects are found to import . 产生的原因是:项目文件里中没有".classpath"和&q ...
- eclipse快捷键,比較有用
1:Ctrl+Pg Up 向左切换选项卡 , 切换到头显示隐藏选项卡(等于Ctrl+e). 2:Ctrl+Pg Dn 向右切换选项卡 , 切换到头显示隐藏选项卡(等于Ctrl+e). ...
- ural 1005 Stone Pile
这是道01背包题 ,尽管背包不会 ,可是还是看出来了,递推公式写啊写没写出来,后来一同学说是dfs.这我就開始了A了, 题意是给你n个重量是Wn的石头 让你放到两个包里面.看他们两个差值最小, ...