博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu4289——Control(最大流最小割+SAP)
阅读量:2343 次
发布时间:2019-05-10

本文共 4462 字,大约阅读时间需要 14 分钟。

Problem Description

  You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD 1 from one city (the source) to another one (the destination). You know their date, source and destination, and they are using the highway network.
  The highway network consists of bidirectional highways, connecting two distinct city. A vehicle can only enter/exit the highway network at cities only.
  You may locate some SA (special agents) in some selected cities, so that when the terrorists enter a city under observation (that is, SA is in this city), they would be caught immediately.
  It is possible to locate SA in all cities, but since controlling a city with SA may cost your department a certain amount of money, which might vary from city to city, and your budget might not be able to bear the full cost of controlling all cities, you must identify a set of cities, that:
  * all traffic of the terrorists must pass at least one city of the set.
  * sum of cost of controlling all cities in the set is minimal.
 You may assume that it is always possible to get from source of the terrorists to their destination.

1 Weapon of Mass Destruction

Input

  There are several test cases.
  The first line of a single test case contains two integer N and M ( 2 <= N <= 200; 1 <= M <= 20000), the number of cities and the number of highways. Cities are numbered from 1 to N.
  The second line contains two integer S,D ( 1 <= S,D <= N), the number of the source and the number of the destination.
  The following N lines contains costs. Of these lines the ith one contains exactly one integer, the cost of locating SA in the ith city to put it under observation. You may assume that the cost is positive and not exceeding 107.
  The followingM lines tells you about highway network. Each of these lines contains two integers A and B, indicating a bidirectional highway between A and B.
  Please process until EOF (End Of File).

Output

  For each test case you should output exactly one line, containing one integer, the sum of cost of your selected set.
  See samples for detailed information.

Sample Input

5 6
5 3
5
2
3
4
12
1 5
5 4
2 3
2 4
4 3
2 1

Sample Output

3

题意就是给一个无向图,去掉每个点有费用,求至少需要多少费用才能使起点到终点不连通。

一开始我很困惑,为什么求的是最少费用却要用最大流?又翻了翻别的解题报告,看到最小割三个字就恍然大悟,立马想到算法导论上讲最大流的图中每个割都是最小割,并且这个最小割在数值上等于最大流。看来只看书是没用的,还得联系实际做题。
还有就是又深入了解了下模板,有一些地方还是没注意。

#include 
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define INF 0x3f3f3f3f#define MAXN 11000#define Mod 10001using namespace std;struct E{ int to, frm, nxt, cap;}edge[MAXN<<5];int head[MAXN], e, n, m, src, des;int dep[MAXN], gap[MAXN]; //gap[x]=y:说明残留网络中dep[i]=x的个数为yvoid addedge(int u, int v, int c){ edge[e].frm = u; edge[e].to = v; edge[e].cap = c; edge[e].nxt = head[u]; head[u] = e++; edge[e].frm = v; edge[e].to = u; edge[e].cap = 0; edge[e].nxt = head[v]; head[v] = e++;}int Q[MAXN];void BFS(int src, int des){ memset(dep, -1, sizeof(dep)); memset(gap, 0, sizeof(gap)); gap[0] = 1; //说明此时有1个dep[i] = 0 int front = 0, rear = 0; dep[des] = 0; Q[rear++] = des; int u, v; while (front != rear) { u = Q[front++]; //cout<
<
edge[S[i]].cap) { temp = edge[S[i]].cap; inser = i; } for (i=0; i!=top; ++i) { edge[S[i]].cap -= temp; edge[S[i]^1].cap += temp; } res += temp; top = inser; u = edge[S[top]].frm; } if (u != des && gap[dep[u] -1] == 0)//出现断层,无增广路 break; for (i = cur[u]; i != -1; i = edge[i].nxt)//遍历与u相连的未遍历结点 if (edge[i].cap != 0 && dep[u] == dep[edge[i].to] + 1) //层序关系, 找到允许 break; if (i != -1)//找到允许弧 { cur[u] = i; S[top++] = i;//加入路径栈 u = edge[i].to;//查找下一个结点 } else //无允许的路径,修改标号 当前点的标号比与之相连的点中最小的多1 { int min = n; for (i = head[u]; i != -1; i = edge[i].nxt) //找到与u相连的v中dep[v]最小的点 { if (edge[i].cap == 0) continue; if (min > dep[edge[i].to]) { min = dep[edge[i].to]; cur[u] = i; //最小标号就是最新的允许弧 } } --gap[dep[u]]; //dep[u] 的个数变化了 所以修改gap ++gap[dep[u] = min + 1]; //将dep[u]设为min(dep[v]) + 1, 同时修改相应的gap[] if (u != src) //该点非源点&&以u开始的允许弧不存在,退点 u = edge[S[--top]].frm; } } return res;}int main(){ while(~scanf("%d%d",&n,&m)) { int s,d; scanf("%d%d",&s,&d); src=s,des=d+n; e=0; memset(head,-1,sizeof(head)); for(int i=1;i<=n;++i) { int w; scanf("%d",&w); addedge(i,i+n,w); } for(int i=1;i<=m;++i) { int a,b; scanf("%d%d",&a,&b); addedge(a+n,b,INF); addedge(b+n,a,INF); } n=2*n; //n为点的个数! int ans=SAP(); printf("%d\n",ans); } return 0;}

转载地址:http://utcvb.baihongyu.com/

你可能感兴趣的文章
三目运算符跟赋值运算符的计算顺序
查看>>
elf文件与符号表
查看>>
linux net-snmp(之安装及配置)
查看>>
linux net-snmp(之android移植)
查看>>
linux net-snmp(之mib2c工具生成标量节点代码)
查看>>
linux net-snmp(之mib2c工具生成表格代码)
查看>>
扩展程序运行时的库路径
查看>>
【CUDA并行程序设计系列(4)】CUDA内存
查看>>
CPU、GPU、CUDA,CuDNN 简介
查看>>
U-boot如何引导Linux内核启动?
查看>>
程序各个段text,data,bss,stack,heap
查看>>
如何利用ROS MoveIt快速搭建机器人运动规划平台?
查看>>
catkin_make &amp;amp;catkin build
查看>>
Camera和IMU的标定过程之kalibr 源码编译
查看>>
在ubuntu下安装python的numpy和scipy模块
查看>>
Ubuntu下apt-get与pip安装命令的区别
查看>>
linux CMakeLists.txt 语法
查看>>
cmake 简介
查看>>
CMake学习笔记(1)——用CMake编译一个hello world程序
查看>>
cmake使用总结---工程主目录CMakeList文件编写
查看>>