当前位置: 代码迷 >> 综合 >> ZOJ 1734 Power Network (ISAP算法)
  详细解决方案

ZOJ 1734 Power Network (ISAP算法)

热度:16   发布时间:2023-11-15 12:27:52.0

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1734

#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e2+5;
const int ub=1e6;
const double inf=1e-4;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){return y?gcd(y,x%y):x;}int cap[maxn][maxn],from,to,val;
int n,np,nc,m;int ISAP(int s,int t){int level[maxn],gap[maxn],pre[maxn],curarc[maxn];///mst(level,0),mst(gap,0),mst(pre,0),mst(curarc,0);int u,v;u=pre[s]=s;int maxflow=0,minflow=mod;n+=2;///初始化gap[s]=t;///初始化间距while(level[s]<n){for(v=curarc[u];v<n;v++){if(cap[u][v]>0&&level[u]==level[v]+1)break;}if(v<n){///找到允许弧pre[v]=u;if(minflow>cap[u][v]) minflow=min(minflow,cap[u][v]);u=curarc[u]=v;if(u==t){maxflow+=minflow;for(v=t;v!=s;v=pre[v]){cap[pre[v]][v]-=minflow;cap[v][pre[v]]+=minflow;}u=s;minflow=mod;}}else{int minlabel=n;for(int v=0;v<n;v++){if(cap[u][v]>0&&minlabel>level[v]){minlabel=level[v];curarc[u]=v;}}if(--gap[level[u]]==0) break;level[u]=minlabel+1;gap[level[u]]++;u=pre[u];}}return maxflow;
}
int main(){while(scanf("%d%d%d%d",&n,&np,&nc,&m)!=EOF){mst(cap,0);while(m--){scanf(" (%d,%d)%d",&from,&to,&val);cap[from][to]=val;}while(np--){scanf(" (%d)%d",&from,&val);cap[n][from]=val;}while(nc--){scanf(" (%d)%d",&to,&val);cap[to][n+1]=val;}printf("%d\n",ISAP(n,n+1));}return 0;
}

 

  相关解决方案