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 ...
随机推荐
- Vue push() pop() shift() unshift() splice() sort() reverse() ...
Vue 变异方法 push() 方法可向数组的末尾添加一个或多个元素,并返回新的长度. pop() 方法用于删除并返回数组的最后一个元素. shift() 方法用于把数组的第一个元素从其中删除,并返回 ...
- emmmmmm(官宣?)
实验室永远不会是学习的唯一地点,不是吗? 总后悔当初退竞赛,现在却还是选择退出,大概是自己真的不适合吧...
- String使用方式详细总结
1.用双引号创建 2.用new String方式创建 3.双引号相加创建 4.两个new String相加时 5.两个引用相加时 6.双引号加new String创建或者new String加双引号创 ...
- Linux思维导图之查找命令
常用查找命令的区别:
- mysql 基础教程
创建数据库: CREATE DATABASE --DATABASE 或者 SCHEMA数据库集合 IF NOT EXISTS db_name CHARACTER SET utf8 COLLATE ut ...
- 第一个GTK程序
/*我已经把代码写在此处 希望借鉴和完善!一起加油奥(PS:我的QQ是1693672542欢迎加我一起进行探讨学习奥!!!)*/#include <stdio.h>#include< ...
- POJ 1198/HDU 1401
双向广搜... 呃,双向广搜一般都都用了HASH判重,这样可以更快判断两个方向是否重叠了.这道题用了双向的BFS,有效地减少了状态.但代码太长了,不写,贴一个别人的代码.. #include<i ...
- How to run Java main class and pass application arguments in Maven?
原文: http://www.logicbig.com/how-to/maven/mvn-java-exec-args/ --------------------------------------- ...
- SERVICE_NAME和SERVICE_NAMES和GLOBAL_DBNAME的各自己定义
tnsnames.ora文件中边SERVICE_NAME的參数值--对于动态注冊和静态注冊.该參数有不同的取值 对于动态注冊: The following pfile/spfile parameter ...
- linux c 操作utmp 和 wtmp 文件接口
/var/run/utmp 保存当前在本系统中的用户信息 /var/log/wtmp 保存登陆过本系统的用户信息 他们保存的信息是基于结构体 struct utmp 的(/usr/include/bi ...