标签:io ar for sp on 问题 ad amp ef
给一个无向图,告知敌人的起点和终点,你要在图上某些点安排士兵,使得敌人无论从哪条路走都必须经过士兵。
每个点安排士兵的花费不同,求最小花费。
分析:
题意可抽象为,求一些点,使得去掉这些点之后,图分成了两部分,敌人的起点和终点分别在这两部分里。即求最小割。
问题是最小割是边,这里把点拆成两个,自己到自己连边,边权为该点点权。其他题目给的边照连就可以了。
为了方便,对于点i,拆成(i,i+n)。
因此,对于题目给的边(a,b),就连双向边边:(a+n,b,inf)、(b+n,a,inf)
#include<cstdio>
#include<vector>
#include<cstring>
#include<queue>
#define maxn 410
#define inf 0x3f3f3f3f
using namespace std;
struct node
{
int from,to,cap,flow;
};
struct dinic
{
int n,m,s,t;
vector<node> e;
vector<int> g[maxn];
bool vis[maxn];
int d[maxn];
int cur[maxn];
void init(int n)
{
e.clear();
for(int i=0;i<=n+2;i++)
g[i].clear();
}
void addedge(int a,int b,int c,int d)
{
e.push_back((node){a,b,c,0});
e.push_back((node){b,a,d,0});
m=e.size();
g[a].push_back(m-2);
g[b].push_back(m-1);
}
bool bfs()
{
memset(vis,0,sizeof vis);
queue<int> q;
q.push(s);
d[s]=0;
vis[s]=1;
while(!q.empty())
{
int x=q.front();q.pop();
for(int i=0;i<g[x].size();i++)
{
node& ee=e[g[x][i]];
if(!vis[ee.to]&&ee.cap>ee.flow)
{
vis[ee.to]=1;
d[ee.to]=d[x]+1;
q.push(ee.to);
}
}
}
return vis[t];
}
int dfs(int x,int a)
{
if(x==t||a==0) return a;
int flow=0,f;
for(int& i=cur[x];i<g[x].size();i++)
{
node& ee=e[g[x][i]];
if(d[x]+1==d[ee.to]&&(f=dfs(ee.to,min(a,ee.cap-ee.flow)))>0)
{
ee.flow+=f;
e[g[x][i]^1].flow-=f;
flow+=f;
a-=f;
if(a==0) break;
}
}
return flow;
}
int maxflow(int s,int t)
{
this->s=s;
this->t=t;
int flow=0;
while(bfs())
{
memset(cur,0,sizeof cur);
flow+=dfs(s,inf);
}
return flow;
}
};
dinic solve;
int main()
{
int i,a,b,st,en,n,m;
while(~scanf("%d%d",&n,&m))
{
scanf("%d%d",&st,&en);
solve.init(n+n);
for(i=1;i<=n;i++)
{
scanf("%d",&a);
solve.addedge(i,i+n,a,0);
}
for(i=0;i<m;i++)
{
scanf("%d%d",&a,&b);
solve.addedge(a+n,b,inf,0);
solve.addedge(b+n,a,inf,0);
}
printf("%d\n",solve.maxflow(st,en+n));
}
return 0;
}
标签:io ar for sp on 问题 ad amp ef
原文地址:http://blog.csdn.net/u011032846/article/details/40748085