HDU4289Control【最大流已知点权拆点】
来源:互联网 发布:excel求逆矩阵 编辑:程序博客网 时间:2024/04/27 14:57
Description
You, the head of Department of Security, recently received a top-secret information that a group of terrorists is planning to transport some WMD1 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
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).
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.
See samples for detailed information.
Sample Input
5 65 35234121 55 42 32 44 32 1
Sample Output
3
这个题是暑假集训的URAL1277Cops and Thieves Dinic最大流一模一样==
拆点的时候方法不唯一,只要头尾与各边能对应就好,建议+n不用*2+1 ==
这个题数组大小要严格控制,否则要么mle 要么re
#include <iostream>#include<cstdio>#include<cstring>using namespace std;const int mm=220005;const int mn=22222;const int oo=1000000000;int node,src,dest,edge;int reach[mm],flow[mm],nxt[mm];int head[mn],work[mn],dis[mn],q[mn];inline int min(int a,int b){ return a<b?a:b;}inline void prepare(int _node,int _src,int _dest){ node=_node,src=_src,dest=_dest; memset(head,-1,sizeof(head)); edge=0;}inline void addedge(int u,int v,int c1,int c2){ reach[edge]=v,flow[edge]=c1,nxt[edge]=head[u],head[u]=edge++; reach[edge]=u,flow[edge]=c2,nxt[edge]=head[v],head[v]=edge++;}bool Dinic_bfs(){ int i,u,v,l,r=0; for(i=0;i<node;++i)dis[i]=-1; dis[q[r++]=src]=0; for(l=0;l<r;++l) for(i=head[u=q[l]];i>=0;i=nxt[i]) if(flow[i]&&dis[v=reach[i]]<0) { dis[q[r++]=v]=dis[u]+1; if(v==dest)return 1; } return 0;}int Dinic_dfs(int u,int exp){ if(u==dest)return exp; for(int &i=work[u],v,tmp;i>=0;i=nxt[i]) if(flow[i]&&dis[v=reach[i]]==dis[u]+1&&(tmp=Dinic_dfs(v,min(exp,flow[i])))>0) { flow[i]-=tmp; flow[i^1]+=tmp; return tmp; }dis[u]--; return 0;}int Dinic_flow(){ int i,ret=0,delta; while(Dinic_bfs()) { for(i=0;i<node;++i)work[i]=head[i]; while(delta=Dinic_dfs(src,oo))ret+=delta; } return ret;}int main(){ // freopen("cin.txt","r",stdin); int n,m,s,d; while(~scanf("%d%d",&n,&m)) { scanf("%d%d",&s,&d); //s--;d--; prepare(n*2+1,s,d+n); for(int i=1;i<=n;i++) { int w; scanf("%d",&w); // addedge(i<<1|1,i<<1,w,0); addedge(i,i+n,w,0); } for(int i=0;i<m;i++) { int a,b; scanf("%d%d",&a,&b); //a--;b--; addedge(a+n,b,oo,0); addedge(b+n,a,oo,0); } printf("%d\n",Dinic_flow()); } return 0;}
0 0
- HDU4289Control【最大流已知点权拆点】
- hdu4289Control 最大流
- hdu4289Control(网络流)
- cratedb已知的最大集群
- HDU 4700 flow Gomory–Hu tree,通过已知最大流构造图形
- 美数学爱好者发现已知最大素数
- 人类目前已知最大的质数
- 已知某年某月获取月最大天数
- 数学爱好者发现已知最大素数:共七百万位
- 第三周—二分法4 已知面积,求最大体积
- 已知一数组,按第一个数最大第二个数最小,依次下去进行的规则排序
- 2、 java 已知一个int数组, 编程从数组中获取最大数.
- 农田开发 NOJ (已知N个点选取3个求最大三角形面积问题)
- 农田开发 NOJ (已知N个点选取3个求最大三角形面积问题)
- 求已知字符串的最大回文字符串长度,并输出该字符串
- 已知最大公约和最小公倍数,求可能的解的组数
- 最大流-最大利益
- 已知先序遍历和中序遍历,求后序遍历 && 求二叉树中节点的最大距离
- MySql5.7.11 for Windows 安装精简版(一)
- SPI总线的特点、工作方式介绍
- Android studio中多项目共享Library
- Spring MVC 解读——<mvc:annotation-driven/>
- 关于安卓长度单位计算方法
- HDU4289Control【最大流已知点权拆点】
- 自我提升
- mybatis insert 空属性用trim suffix="" suffixOverrides=","去多余的“,”
- XTU1236 Fraction
- 欢迎使用CSDN-markdown编辑器
- 插入操作
- Xml解析
- 关于linux的读写锁
- java操作redis基本功能_Main方法(六)