zoukankan      html  css  js  c++  java
  • ZOJ3792_Romantic Value

    给出图,使得两点无流量,剩余其他边的总容量与删除边数的比值。

    要机智啊。。。

    因为原图给的边数不超过1000,容量也不超过1000,可以这样把边的容量变为2000*c+1。这样跑出最大流后,最大流除以2000就是最大流,最大流模2000就是所求的边割了。。。

     

     

     

    召唤代码君:

     

     

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <vector>
    #define maxn 111
    #define maxm 2222
    using namespace std;
    
    const int inf=~0U>>2;
    int to[maxm],c[maxm],next[maxm],first[maxn],work[maxn],edge;
    int Q[maxm],tag[maxn],d[maxn],bot,top,TAG=222;
    bool can[maxn];
    int n,m,s,t,T,ans,cur,hehe,num,tot_v,last;
    vector<int> TO[maxn];
    
    void _init()
    {
        edge=tot_v=0;
        memset(first,-1,sizeof(int)*(n+2));
        for (int i=1; i<=n; i++) TO[i].clear();
    }
    
    void addedge(int U,int V,int W)
    {
        to[edge]=V,c[edge]=W,next[edge]=first[U],first[U]=edge++;
        to[edge]=U,c[edge]=W,next[edge]=first[V],first[V]=edge++;
    }
    
    void addedge2(int U,int V,int W)
    {
        cout<<" addedge 2 : "<<U<<' '<<V<<' '<<W<<endl;
        to[edge]=V,c[edge]=W,next[edge]=first[U],first[U]=edge++;
        to[edge]=U,c[edge]=0,next[edge]=first[V],first[V]=edge++;
    }
    
    bool bfs()
    {
        Q[bot=top=1]=t,tag[t]=++TAG,d[t]=0,can[t]=false;
        while (bot<=top)
        {
            int cur=Q[bot++];
            for (int i=first[cur]; i!=-1; i=next[i])
                if (c[i^1]>0 && tag[to[i]]!=TAG)
                {
                    tag[to[i]]=TAG,d[to[i]]=d[cur]+1;
                    can[to[i]]=false,Q[++top]=to[i];
                    if (to[i]==s) return true;
                }
        }
        return false;
    }
    
    int dfs(int cur,int num)
    {
        if (cur==t) return num;
        int tmp=num,k;
        for (int& i=work[cur]; i!=-1; i=next[i])
            if (c[i]>0 && tag[to[i]]==TAG && d[to[i]]==d[cur]-1 && !can[to[i]])
            {
                k=dfs(to[i],min(num,c[i]));
                if (k) num-=k,c[i]-=k,c[i^1]+=k;
                if (!num) break;
            }
        if (num) can[cur]=true;
        return tmp-num;
    }
    
    int maxflow()
    {
        int Flow=0;
        while (bfs()){
            memcpy(work,first,sizeof(int)*(n+2));
            Flow+=dfs(s,inf);
        }
        return Flow;
    }
    
    void bfs(int cur)
    {
        for (int i=first[cur] ;i!=-1; i=next[i])
            if (c[i]==0 && c[i^1]>0){
                TO[cur].push_back(to[i]);
                bfs(to[i]);
            }
    }
    
    int main()
    {
        int U,V,W;
        scanf("%d",&T);
        while (T--){
            scanf("%d%d%d%d",&n,&m,&s,&t);
            _init();
            while (m--){
                scanf("%d%d%d",&U,&V,&W);
                tot_v+=W;
                addedge(U,V,W*2000+1);
            }
            hehe=ans=maxflow();
            if (ans==0){
                printf("Inf
    ");
                continue;
            }
    
            //cout<<tot_v<<' '<<ans<<' '<<num<<endl;
            printf("%.2f
    ",(tot_v-ans/2000)/(1.0*(ans%2000)));
        }
        return 0;
    }
  • 相关阅读:
    线性可分支持向量机
    谈对牛顿法的理解
    最大熵模型学习笔记
    Maven --- <distributionManagement>标签
    poi-word导出,导出多图片到word
    poi-excel导入导出
    ApplicationContextInitializer接口
    springboot伪静态
    springboot集成邮件服务
    多文件上传demo
  • 原文地址:https://www.cnblogs.com/lochan/p/3893999.html
Copyright © 2011-2022 走看看