首页 技术 正文
技术 2022年11月15日
0 收藏 561 点赞 3,656 浏览 1583 个字

题目地址: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;
}

  

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,082
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,557
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,406
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,179
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,815
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,898