最大流:HDU-4289(CONTROL)
来源:互联网 发布:2016淘宝视频开店教程 编辑:程序博客网 时间:2024/06/05 03:03
#include
#include
#include
#define INF 0x7fffffff
#define MAXV 1000
#define MAXE 20000
using namespace std;
struct edge{ int to,c,next;}e[MAXE<<3];
int size,head[MAXV];
void addedge(int from,int to,int c)
{
e[size].to=to; e[size].c = c; e[size].next=head[from]; head[from]=size++;
e[size].to=from; e[size].c=0; e[size].next=head[to]; head[to]=size++;
}
int n,m,s,d;int cost[MAXV];int dis[MAXV],gap[MAXV],cur[MAXV],pre[MAXV];
int SAP_GAP()
{ int cur_flow,flow_ans=0,neck,u,tmp,i; int start=s*2-1,end=d*2,N=n*2;
memset(dis,0,sizeof(dis));memset(gap,0,sizeof(gap));
memset(pre,-1,sizeof(pre));
for(i=0;i<=N;i++) cur[i]=head[i];
gap[0]=N; u=start;
while(dis[start]<N)
{ if(u==end)
{ cur_flow=INF;
(i=start;i!=end;i=e[cur[i]].to) for
if(cur_flow>e[cur[i]].c) { cur_flow=e[cur[i]].c; neck=i; }
(i=start;i!=end;i=e[cur[i]].to) for
{ tmp=cur[i];
e[tmp].c-=cur_flow;
e[tmp^1].c+=cur_flow;
}
flow_ans+=cur_flow; u=neck;
}
for (i=cur[u];i!=-1;i=e[i].next)
if(e[i].c&&dis[u]==dis[e[i].to]+1) break;
if (i!=-1)
{
cur[u]=i;
pre[e[i].to]=u;
u=e[i].to;
}
else
{
if (--gap[dis[u]]==0) break;
cur[u]=head[u];
for (tmp=N,i=head[u];i!=-1;i=e[i].next)
if (e[i].c) tmp=min(tmp,dis[e[i].to]);
dis[u]=tmp+1;
gap[dis[u]]++;
if (u!=start) u=pre[u];
}
}
return flow_ans;
}
void init()
{ int from,to; size=0;
memset(head,-1,sizeof(head));
memset(cost,0,sizeof(cost));
scanf("%d%d",&s,&d);
for (int i=1;i<=n;i++)
{ scanf("%d",&cost[i]);
addedge(i*2-1,i*2,cost[i]);
//addedge(i*2,i*2-1,cost[i]);
}
for (int i=1;i<=m;i++)
{ scanf("%d%d",&from,&to);
addedge(from*2,to*2-1,INF);
addedge(to*2,from*2-1,INF);
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
printf("%d\n",SAP_GAP());
}
0; return
}
0 0
- 最大流:HDU-4289(CONTROL)
- 【HDU】4289 Control 最大流
- hdu 4289 Control (最大流)
- HDU 4289 Control 最大流
- HDU 4289 Control(拆点,最大流)
- hdu 4289 Control (最大流)
- HDU 4289 Control(拆点+最大流)
- hdu 4289 Control (最大流)
- 最大流最小割-HDU-4289-Control
- hdu 4289 Control 【图论-网络流-最大流-拆点】
- HDU 4289 Control(最大流、最小割)
- HDU-4289-Control(最大流+拆点)
- HDU 4289 Control 最小割最大流 拆点
- HDU 4289 Control(最大流+拆点)
- HDU 4289 - Control(最大流-ISAP模板)
- HDU 4289 Control (最大流最小割)
- HDU 4289 Control(最大流+拆点)
- hdu 4289 Control【最大流Dinic----拆点+最大流最小割定理】
- ubuntu 13.10 64bit装BeyondCompare
- 使用ALAssetsLibrary读取所有照片
- 退出、测试、判断及操作符
- 循环与结构化命令
- 最大流:HDU-3572 ( Tas…
- 最大流:HDU-4289(CONTROL)
- 最大流:HDU-4292(Food)
- pl/sql 中操作DML语句
- mySQL alter使用方法
- MySQL 外键
- mysql一些命令
- java技巧-连接数据库
- Java技巧-日期
- ubuntu下-配置和管理msyql