题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=2485

题目要求:删除最少的点,使得源点到汇点的距离大于k

思路:拆点、建图求费用小于等于k的最大流

#include <iostream>
#include <cstring>
#include <climits>
#include <cstdio>
#include <queue>
using namespace std;
#define min(x,y) (x)<(y)?(x):(y)
const int maxm = 10005;
const int maxn = 1100; struct node{
int v,flow,cost,next;
}edge[maxm];
int head[maxn],dis[maxn],vis[maxn],pre[maxn],cur[maxn];
int id,n,m,k;
int sink,source;
void add_edge(int u,int v,int flow,int cost){
edge[id].v = v;edge[id].cost = cost;edge[id].flow = flow;edge[id].next = head[u];head[u] = id++;
edge[id].v = u;edge[id].cost = -cost;edge[id].flow = 0 ;edge[id].next = head[v];head[v] = id++;
}
void init(){
int u,v,i;
source = 1,sink = n*2;
memset(head,-1,sizeof(head));id = 0;
for( i = 1; i <= m; i++){
scanf("%d%d",&u,&v);
add_edge(u+n,v,1,1);
}
for( i = 2; i < n; i++)//拆点
add_edge(i,i+n,1,0);
add_edge(1,n+1,5000000,0);
add_edge(n,n+n,5000000,0);
} int spfa(){
memset(dis,-1,sizeof(dis));
memset(vis,0,sizeof(vis));
pre[source] = source;
dis[source] = 0;
vis[source] = 1;
queue<int>que;
que.push(source);
while( !que.empty()){
int u = que.front();
que.pop();
vis[u] = 0;
for(int id = head[u]; id != -1; id = edge[id].next){
int v = edge[id].v;
if(edge[id].flow > 0 && (dis[v] == -1 || dis[v] > dis[u] + edge[id].cost )){
dis[v] = dis[u] + edge[id].cost;
pre[v] = u;cur[v] = id;
if(!vis[v] ){
vis[v] = 1;
que.push(v);
}
}
}
}
if(dis[sink] == -1 || dis[sink] > k)return 0;
return 1;
} int get_max(){
int max_flow = 0;
while(spfa())
{
//cout << dis[sink] << endl;
max_flow += 1;
int now = sink;
while(now != source){
edge[cur[now]].flow -= 1;
edge[cur[now]^1].flow += 1;
now = pre[now];
}
}
return max_flow;
}
int main(){
freopen("in.txt","r",stdin);
while(~scanf("%d%d%d",&n,&m,&k),n||m||k){
init();
printf("%d\n",get_max());
}
return 0;
}

  

HDU2485Destroying the bus stations 拆点网络流求割点个数的更多相关文章

  1. POJ1144Network(求割点个数)

    题目链接 题意:一共n割点,然后若干行,每行第一个输入一个点,然后若干个点表示与他相连,0单独一行表示一个样例的结束.然后求图中的割点个数 割点:去掉该点之后得到的图不在连通,那么该店就是割点 一般割 ...

  2. loj 1063(求割点个数)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=26780 思路:判断一个点是否是割点的两个条件:1.如果一个点v是根 ...

  3. POJ 1144 Network(tarjan 求割点个数)

    Network Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 17016   Accepted: 7635 Descript ...

  4. Destroying the bus stations HDU - 2485(最小割点)

    题意: 就是求最小割点 解析: 正向一遍spfa 反向一遍spfa  然后遍历每一条边,对于当前边 如果dis1[u] + dis2[v] + 1 <= k 那么就把这条边加入到网络流图中, 每 ...

  5. UVA 315 315 - Network(求割点个数)

     Network  A Telephone Line Company (TLC) is establishing a new telephone cable network. They are con ...

  6. UVA-315 无向图求割点个数

    题意抽象: 给定一个无向图,输出割点个数. 割点定义:删除该点后,原图变为多个连通块. 考虑一下怎么利用tarjan判定割点: 对于点u和他相连的当时还未搜到的点v,dfs后如果DFN[u]<= ...

  7. poj 1144(求割点个数)

    题目链接:http://poj.org/problem?id=1144 思路:判断一个点是否是割点的两个条件:1.如果一个点v是根结点并且它的子女个数大于等于2,则v是割点.2.如果点v不是根结点,并 ...

  8. poj1144 Network【tarjan求割点】

    转载请注明出处,谢谢:http://www.cnblogs.com/KirisameMarisa/p/4319585.html   ---by 墨染之樱花 [题目链接]http://poj.org/p ...

  9. 图论--网络流--最小割 HDU 2485 Destroying the bus stations(最短路+限流建图)

    Problem Description Gabiluso is one of the greatest spies in his country. Now he's trying to complet ...

随机推荐

  1. 【JDK】JDK源码分析-List, Iterator, ListIterator

    List 是最常用的容器之一.之前提到过,分析源码时,优先分析接口的源码,因此这里先从 List 接口分析.List 方法列表如下: 由于上文「JDK源码分析-Collection」已对 Collec ...

  2. [NSNull intValue]: unrecognized selector sent to instance 0x375c9860

    今天遇到这个问题,程序崩溃了……日志如下: -[NSNull intValue]: unrecognized selector sent to instance 0x375c9860*** Termi ...

  3. 调试过程中发现按f5无法走进jdk源码

    debug 模式 ,在fis=new FileInputStream(file); 行打断点 调试过程中发现按f5无法走进jdk源码 package com.lzl.spring.test; impo ...

  4. L4170[CQOI2007]涂色

    #include <bits/stdc++.h> using namespace std; #define rep(i, a, b) for (int i = a; i <= b; ...

  5. 洛谷 P2572 [SCOI2010]序列操作

    题意简述 维护一个序列,支持如下操作 把[a, b]区间内的所有数全变成0 把[a, b]区间内的所有数全变成1 把[a,b]区间内所有的0变成1,所有的1变成0 询问[a, b]区间内总共有多少个1 ...

  6. 洛谷 P1196 [NOI2002]银河英雄传说

    题意简述 有30000列,每列都有一艘战舰,编号1~30000 有2种操作: 1.将一列的战舰运到另一列 2.询问两个战舰是否在同一列,如果是,求出它们之间的距离 题解思路 并查集, 维护每个点x离自 ...

  7. html5 placeholder属性兼容ie11

    placeholder 属性是html5的属性,用于提供描述输入字段预期值的提示信息(hint). 简单例子: <!DOCTYPE HTML> <html> <body& ...

  8. tensorflow学习笔记——图像数据处理

    喜欢摄影的盆友都知道图像的亮度,对比度等属性对图像的影响是非常大的,相同物体在不同亮度,对比度下差别非常大.然而在很多图像识别问题中,这些因素都不应该影响最后的结果.所以本文将学习如何对图像数据进行预 ...

  9. python编辑已存在的excel坑: BadZipFile: File is not a zip file

    背景-原代码如下,期望能自动创建excel,并且可以反复调用编辑: import xlwt,osfrom openpyxl.styles import Font, colors class Write ...

  10. c11标准

    在编译器vs13及其以上可以使用 编译器对语言的一种优化 1.变量初始化 int a=0,a(10),a{10};定义a的值的三种方式 2.nullptr 相当于c的null 有类型 更加的安全 3. ...