当前位置: 代码迷 >> 综合 >> 狼抓兔子 HYSBZ - 1001 (最大流求最小割)
  详细解决方案

狼抓兔子 HYSBZ - 1001 (最大流求最小割)

热度:50   发布时间:2024-01-14 22:18:34.0

题目 https://cn.vjudge.net/problem/HYSBZ-1001

题意

最小割裸题

#include <bits/stdc++.h>using namespace std;
const int maxn = 1000005;
int s,t;//源点和汇点
int cnt;//边的数量,从0开始编号。
int Head[maxn];//每一个点最后一条边的编号
struct node
{int u,v,w,next;
}edge[10*maxn];
int Depth[maxn];//分层图中标记深度
int n,m;
void Add(int u,int v,int w)
{cnt++;edge[cnt].u = u;edge[cnt].v=v;edge[cnt].w = w;edge[cnt].next = Head[u];Head[u] = cnt;
}
void Add_edge(int u,int v,int w)
{Add(u,v,w); //双向图Add(v,u,w);}
int dfs(int u,int dist)
{if(u == t)return dist;for(int i = Head[u];i!=-1;i=edge[i].next){int v = edge[i].v;int w = edge[i].w;if((Depth[v] == Depth[u]+1)&&w !=0)//层数是当前层数加一流量不为0{int di = dfs(v,min(dist,w));if(di > 0){edge[i].w -= di;edge[i^1].w += di;return di;}}}Depth[u] = -1;return 0;
}bool bfs()
{queue<int> q;while(!q.empty()) q.pop();memset(Depth,0,sizeof(Depth));Depth[s] = 1;q.push(s);do{int u = q.front();q.pop();for(int i = Head[u];i!=-1;i = edge[i].next){int v = edge[i].v;int w = edge[i].w;if((w>0)&&Depth[v] == 0) //存在容量 并且没被访问{Depth[v] = Depth[u] + 1;  // 层数加一q.push(v);}}}while(!q.empty());if(Depth[t] == 0) return 0;else return 1;
}
int Dinic()
{int Ans = 0;while(bfs()) //建立分层图{
//        cout<<"YES"<<endl;while(int d = dfs(s,0x3f3f3f3f)) //增广Ans += d;}return Ans;
}int main()
{int i,j,k;int x;while(scanf("%d%d",&n,&m) != EOF){memset(Head,-1,sizeof(Head));cnt =  -1;for(i=1;i<=n;i++){for(j=1;j<=m-1;j++){scanf("%d", &x);Add_edge((i-1)*m+j, (i-1)*m+j+1, x);}}for(i = 1;i <= n-1;i++){for(j=1;j<=m;j++){scanf("%d", &x);Add_edge((i-1)*m+j, i*m + j, x);}}for(i=1;i<=n-1;i++){for(j=1;j<=m-1;j++){scanf("%d", &x);Add_edge((i-1)*m + j, i*m + j + 1, x);}}//cout<<"YES"<<endl;s = 1,t = n*m;int ans = Dinic();printf("%d\n",ans);}return 0;
}

 

  相关解决方案