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 ...
随机推荐
- 【转载】使用IntelliJ IDEA提示找不到struts-default文件
创建strus,参考文如下: https://blog.csdn.net/u010358168/article/details/79769137 使用IntelliJ IDEA创建struts2工程时 ...
- C#那20道题
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- C#第七节课
for嵌套 using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...
- Feign 负载均衡
一.是什么 Feign 是一个声明式 WebService 客户端.使用 Feign 能让编写 Web Service 客户端更加简单,他的使用方法是定义一个接口,然后在上面添加注解.同时也支持 JA ...
- elasticsearch 权威指南排序阅读笔记(六)
默认排序 默认查询是通过_source 准确性权重来排序 字段排序 { "query":{ "match":{ "productName": ...
- try finnlay 总结
public class FinnallyTest { public static void main(String[] args){ System.out.print(go()); } public ...
- [bzoj3123][Sdoi2013]森林_主席树_启发式合并
森林 bzoj-3123 Sdoi-2013 题目大意:给定一片共n个点的森林,T个操作,支持:连接两个不在一棵树上的两个点:查询一棵树上路径k小值. 注释:$1\le n,T \le 8\cdot ...
- Javascript中的原型继承具体解释
js中的继承,是面向对象的知识,由于js没有类的概念.所以继承是通过对象实现的.谈到继承.就必须说到prototype,就不得不先说下new的过程. 一个小小的列子: <script type= ...
- Linux命令(十一)——Shell程序设计二(循环控制语句)
1.if语句 (1)两路分支的if语句 (2)多路条件判断分支的if语句 2.测试语句 (1)文件测试 (2)字符串测试 (3)数值测试 (4)用逻辑操作符进行组合的测试语句 3.case语句 4.f ...
- Android开发之BUG专讲:入门篇(一)
前言: 本文作者:周才智 转载须注明作者与出处.违者必究. 原文地址:http://segmentfault.com/a/1190000004380690 话说诸葛亮是一个优秀的程序员,每个锦囊都是应 ...