HDU - 4289 Control (Dinic)
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 10 7.
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 题意:
在图中,删除点需要相应的花费,求最小的花费,使得s,t不连通。
思路:
最大流=最小割。
一开始忘记了,这个,想了半天费用流。。。
拆带限制点的流量即可。
#include<iostream>
#include<algorithm>
#include<vector>
#include<stack>
#include<queue>
#include<map>
#include<set>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<ctime> #define fuck(x) cerr<<#x<<" = "<<x<<endl;
#define debug(a, x) cerr<<#a<<"["<<x<<"] = "<<a[x]<<endl;
#define ls (t<<1)
#define rs ((t<<1)|1)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const int maxm = ;
const int inf = 0x3f3f3f3f;
const ll Inf = ;
const int mod = ;
const double eps = 1e-;
const double pi = acos(-); int Head[maxn],cnt;
struct edge{
int Next,v;
int w;
}e[maxm];
void add_edge(int u,int v,int w){
e[cnt].Next=Head[u];
e[cnt].v=v;
e[cnt].w=w;
Head[u]=cnt++;
} int D_vis[maxn],D_num[maxn];
int source,meeting;
int n,m;
bool bfs()
{
memset(D_vis,,sizeof(D_vis));
for(int i=;i<=*n;i++){//注意要覆盖所有点
D_num[i]=Head[i];
}
D_vis[source]=;
queue<int>q;
q.push(source);
while(!q.empty()){
int u=q.front();
q.pop();
int k=Head[u];
while(k!=-){
if(!D_vis[e[k].v]&&e[k].w){
D_vis[e[k].v]=D_vis[u]+;
q.push(e[k].v);
}
k=e[k].Next;
}
}
return D_vis[meeting];
}
int dfs(int u,int f)
{
if(u==meeting){return f;}
int &k=D_num[u];
while(k!=-){
if(D_vis[e[k].v]==D_vis[u]+&&e[k].w){
int d=dfs(e[k].v,min(f,e[k].w));
if(d>){
e[k].w-=d;
e[k^].w+=d;
return d;
}
}
k=e[k].Next;
}
return ;
}
int Dinic()
{
int ans=;
while(bfs()){
int f;
while((f=dfs(source,inf))>){
ans+=f;
} }
return ans;
} int city1(int x){
return x;
}
int city2(int x){
return x+n;
} int main() {
// ios::sync_with_stdio(false);
// freopen("in.txt", "r", stdin); while (scanf("%d%d",&n,&m)!=EOF){
memset(Head,-,sizeof(Head));
cnt=;
scanf("%d%d",&source,&meeting);
meeting+=n;
for(int i=;i<=n;i++){
int x;
scanf("%d",&x);
add_edge(city1(i),city2(i),x);
add_edge(city2(i),city1(i),);
}for(int i=;i<=m;i++){
int x,y;
scanf("%d%d",&x,&y);
add_edge(city2(x),city1(y),inf);
add_edge(city1(y),city2(x),);
add_edge(city2(y),city1(x),inf);
add_edge(city1(x),city2(y),);
}
printf("%d\n",Dinic());
} return ;
}
HDU - 4289 Control (Dinic)的更多相关文章
- HDU 4289 Control (网络流,最大流)
HDU 4289 Control (网络流,最大流) Description You, the head of Department of Security, recently received a ...
- hdu 4289 Control(最小割 + 拆点)
http://acm.hdu.edu.cn/showproblem.php?pid=4289 Control Time Limit: 2000/1000 MS (Java/Others) Mem ...
- HDU 4289 Control 最小割
Control 题意:有一个犯罪集团要贩卖大规模杀伤武器,从s城运输到t城,现在你是一个特殊部门的长官,可以在城市中布置眼线,但是布施眼线需要花钱,现在问至少要花费多少能使得你及时阻止他们的运输. 题 ...
- hdu 4289 Control 网络流
题目链接 给出一些点, 每个点有一个权值, 给出一些边, 起点以及终点, 去掉一些点使得起点和终点不连通, 求最小的val. 拆点, 把一个点s拆成s和s', 之间建一条边, 权值为点权. 对于一条边 ...
- HDU 4289 Control (最小割 拆点)
Control Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...
- HDU 4289 Control(最大流+拆点,最小割点)
题意: 有一群恐怖分子要从起点st到en城市集合,你要在路程中的城市阻止他们,使得他们全部都被抓到(当然st城市,en城市也可以抓捕).在每一个城市抓捕都有一个花费,你要找到花费最少是多少. 题解: ...
- HDU 4289 Control
最小割 一个点拆成两个 AddEdge(i,i+N,x); 原图中的每条边这样连 AddEdge(u+N,v,INF); AddEdge(v+N,u,INF); S是源点,t+N是汇点.最大流就是答案 ...
- hdu 4289 最大流拆点
大致题意: 给出一个又n个点,m条边组成的无向图.给出两个点s,t.对于图中的每个点,去掉这个点都需要一定的花费.求至少多少花费才能使得s和t之间不连通. 大致思路: 最基础的拆点最大 ...
- (网络流 最大流 Dinic || SAP)Control -- hdu --4289
链接: http://acm.hdu.edu.cn/showproblem.php?pid=4289 http://acm.hust.edu.cn/vjudge/contest/view.action ...
随机推荐
- python系列之(1)BeautifulSoup的用法
好久没更新博客了.打算写一个python的爬虫系列及数据分析.falg也不能随便立,以免打脸. python爬取内容,是过程,分析数据是结果,最终得出结论才是目的.python爬虫爬取了内容,一般都是 ...
- Servlet过虑器
过滤器是在请求的预处理和后处理时调用的对象. 主要用于执行转换,日志记录,压缩,加解密,输入验证等过滤任务. servlet过滤器是可插拔的,即它在web.xml文件中定义,如果从web.xml文件中 ...
- 通过 PHP OPcache 提升 Laravel 应用运行速度
什么是 OPcache 每一次执行 PHP 脚本的时候,该脚本都需要被编译成字节码,而 OPcache 可以对该字节码进行缓存,这样,下次请求同一个脚本的时候,该脚本就不需要重新编译,这极大节省了脚本 ...
- 实现一个简易的promise
//promise里面只有三个状态,且三个状态的转换形式有两种 //由pending转换为fulfilled,由pending转换为rejected //Promise的构造函数参数是一个函数,函数的 ...
- Vue组件跨层级通信
正常组件间通信 父->子组件 是通过属性传递 子->父组件 是通过this.$emit()传递 this.$emit()返回的是this,如果需要一些值 可使用callback方式传递 p ...
- HDU-1069_Monkey and Banana
Monkey and Banana Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) P ...
- Redis源码解析:01简单动态字符串SDS
Redis没有直接使用C字符串(以'\0'结尾的字符数组),而是构建了一种名为简单动态字符串( simple dynamic string, SDS)的抽象类型,并将SDS用作Redis的默认字符 ...
- H3C 802.11无线网络的介质访问控制
- Laravel5使用ElasticSearch
https://blog.csdn.net/qq_16829085/article/details/80725125 安装elasticsearch和ik插件 (elasticsearch的使用需要配 ...
- supersockets和 AppSession,AppServer 配合工作
现在, 你已经有了 RequestInfo, ReceiveFilter 和 ReceiveFilterFactory, 但是你还没有正式使用它们. 如果你想让他们在你的程序里面可用, 你需要定义你们 ...