网络流最小割 POJ 3469

题意  2个CPU n个任务 给出在第一个 第二个运行时的花费

m  个  a  b 不在同一个CPU运行的额外花费

建图 源点 ->   n    -> 汇点

权          a1          b1

反向边权 0;

还有  a->b   权 w;

不知道 为什么b->a 权也是w

 #include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue> using namespace std; int n,m;
int S,T,cnt;
#define inf 100000000
#define MAXN 20000
#define MAXN1 250000
int head[MAXN+];
int vis[MAXN+]; struct edg
{
int to,next,w;
}x[MAXN1*]; void add(int u,int v,int w)
{
x[cnt].next=head[u];
x[cnt].to=v;
x[cnt].w=w;
head[u]=cnt++;
}
int bfs()
{
queue<int>q1;
memset(vis,-,sizeof(vis));
vis[S]=;
q1.push(S); while(!q1.empty())
{
int now=q1.front();
q1.pop();
int j; for(j=head[now];j!=-;j=x[j].next)
{
if(vis[x[j].to]<&&x[j].w)
{
vis[x[j].to]=vis[now]+;
q1.push(x[j].to);
}
}
}
return vis[T]!=-; }
int dfs(int u,int w)
{
int ans=;
if(u==T)
return w;
int j;
for(j=head[u];j!=-;j=x[j].next)
{
if(vis[x[j].to]==vis[u]+&&x[j].w)
{
int b=dfs(x[j].to,min(w-ans,x[j].w));
x[j].w-=b;
x[j^].w+=b;
ans+=b;
}
}
return ans;
} int main()
{ while(scanf("%d%d",&n,&m)!=EOF)
{
int i;
memset(head,-,sizeof(head));
cnt=;
S=;
T=n+; for(i=;i<=n;i++)
{
int a,b;
scanf("%d%d",&a,&b);
add(S,i,a),add(i,S,);
add(i,T,b),add(T,i,);
}
for(i=;i<=m;i++)
{
int a,b,w;
scanf("%d%d%d",&a,&b,&w);
add(a,b,w),add(b,a,w);
}
int ans=;
while(bfs())
ans+=dfs(S,inf);
printf("%d\n",ans);
}
return ;
}
上一篇:同一个服务器部署多个tomcat


下一篇:npm教程_脚手架原理以及bootstrap引入