zoj 3792 求最小割边集

题意:给你n个点,m条无向边,让你求出最小割,并输出(图中所有边权和--最小割)/(最小割边数)。

代码:

//Isap算法,复杂度O(n^2m)
#pragma comment(linker,"/STACK:102400000,102400000")
#include <iostream>
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <vector>
#include <string>
#include <math.h>
#include <queue>
#include <stack>
#include <map>
#include <set>
using namespace std;
typedef long long ll;   //记得必要的时候改成无符号
const int maxn=55;
const int maxm=1000005;
const int INF=1000000000;
struct EdgeNode
{
    int from;
    int to;
    int cost;
    int next;
}edge[maxm];
int head[maxn],cnt;
void add(int x,int y,int z)
{
    edge[cnt].from=x;edge[cnt].to=y;edge[cnt].cost=z;edge[cnt].next=head[x];head[x]=cnt++;
    edge[cnt].from=y;edge[cnt].to=x;edge[cnt].cost=0;edge[cnt].next=head[y];head[y]=cnt++;
}

void init()
{
    cnt=0;
    memset(head,-1,sizeof(head));
}

int flag;
int vis[maxn];
vector<int>V;
int S,T,n,m;
int d[maxn],gap[maxn],curedge[maxn],pre[maxn];
//curedge[]为当前弧数组,pre为前驱数组

int sap(int S,int T,int n)  //n为点数
{
    int cur_flow,flow_ans=0,u,tmp,neck,i;
    memset(d,0,sizeof(d));
    memset(gap,0,sizeof(gap));
    memset(pre,-1,sizeof(pre));
    for(i=0;i<=n;i++)curedge[i]=head[i]; //初始化当前弧为第一条邻接表
    gap[0]=n;
    u=S;
    while(d[S]<n)             //当d[S]>=n时,网络中肯定出现了断层
    {
        if(u==T)
        {
            flag=1;
            cur_flow=INF;
            for(i=S;i!=T;i=edge[curedge[i]].to)
            {                           //增广成功,寻找瓶颈边
                if(cur_flow>edge[curedge[i]].cost)
                {
                    neck=i;
                    cur_flow=edge[curedge[i]].cost;
                }
            }
            for(i=S;i!=T;i=edge[curedge[i]].to)
            {                             //修改路径上的边容量
                tmp=curedge[i];
                edge[tmp].cost-=cur_flow;
                edge[tmp^1].cost+=cur_flow;
            }
            flow_ans+=cur_flow;
            u=neck;                     //下次增广从瓶颈边开始
        }
        for(i=curedge[u];i!=-1;i=edge[i].next)
            if(edge[i].cost&&d[u]==d[edge[i].to]+1)
               break;
        if(i!=-1)
        {
            curedge[u]=i;
            pre[edge[i].to]=u;
            u=edge[i].to;
        }
        else
        {
            if(0==--gap[d[u]])break;    //gap优化
            curedge[u]=head[u];
            for(tmp=n,i=head[u];i!=-1;i=edge[i].next)
                if(edge[i].cost)
                   tmp=min(tmp,d[edge[i].to]);
            d[u]=tmp+1;
            ++gap[d[u]];
            if(u!=S)u=pre[u];           //重标号并且从当前点前驱重新增广
        }
    }
    return flow_ans;
}

void dfs(int u)
{
    vis[u]=1;
    V.push_back(u);
    for(int i=head[u];i!=-1;i=edge[i].next)
    {
        int v=edge[i].to;
        if(!vis[v]&&edge[i].cost)dfs(v);
    }
}

int main()
{
    int cs,p,q,i,j,t,he,x,y,z;
    scanf("%d",&cs);
    while(cs--){
        init(); flag=0; he=0;
        scanf("%d%d%d%d",&n,&m,&p,&q);
        for(i=1;i<=m;i++){
            scanf("%d%d%d",&x,&y,&z);
            he+=z;
            add(x,y,z);
            add(y,x,z);
        }
        int chu=sap(p,q,n);
        if(flag==0)printf("Inf\n");
        else{
            int sum=0;
            memset(vis,0,sizeof(vis));
            V.clear();
            dfs(p);
            for(i=0;i<V.size();i++){
                t=V[i];
                for(j=head[t];j!=-1;j=edge[j].next){
                    if(vis[edge[j].to]==0)
                        sum++;
                }
            }
            //printf("%d   %d   %d\n",he,chu,sum);
            printf("%.2f\n",(he-chu)*1.0/sum*2);
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值