HDU 4289Control【最大流已知点权拆点】
2016-04-07 19:48
225 查看
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 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
这个题是暑假集训的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;
}
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 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
这个题是暑假集训的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;
}
相关文章推荐
- 拉灯游戏加强版
- 相机切换
- Win10驱动数字签名的解决办法
- Linux的mmap内存映射机制解析
- 线性回归模型
- spring MVC入门
- 14.linux按键驱动程序(一)
- 矩阵快速幂 UVA 10870 Recurrences
- WebView浏览本地html
- [IO]——纯文本读取
- Spark容错机制
- SecureCRT rz 上传文件失败问题
- CentOS7修改默认运行级别
- 平衡二叉树---Shaolin
- 《深入理解计算机系统》第七章读书笔记
- HttpRequest
- Android——列表视图 ListView(三)BaseAdapter
- 哈希表原理及hashmap简单实现
- UVA 11827 Maximum GCD【GCD,stringstream】
- 线程安全/不安全的讨论