• poj3068


    题解:

    最小费用最大流

    每一次找最短的

    代码:

    #include<cstdio>
    #include<cmath>
    #include<cstring>
    #include<algorithm>
    #include<queue>
    using namespace std;
    const int N=1000005;
    int fi[N],n,t,cas,m,x,y,z,f[N],ne[N],num,zz[N],fl[N],gp[N],dist[N],pre[N],sl[N];
    void jb(int x,int y,int z,int s)
    {
        ne[num]=fi[x];
        fi[x]=num;
        zz[num]=y;
        sl[num]=z;
        fl[num++]=s;
        ne[num]=fi[y];
        fi[y]=num;
        zz[num]=x;
        sl[num]=0;
        fl[num++]=-s;
    }
    int spfa()
    {
        memset(dist,0x3f,sizeof dist);
        memset(pre,-1,sizeof pre);
        memset(gp,0,sizeof gp);
        memset(f,0,sizeof f);
        queue<int > Q;
        Q.push(1);
        dist[1]=0;
        while (!Q.empty())
         {
             int now=Q.front();
             Q.pop();
             f[now]=0;
             for (int i=fi[now];i!=-1;i=ne[i])
              if (sl[i]>0)
               {
                  int t=zz[i];
                  if (dist[t]>dist[now]+fl[i])
                   {
                       dist[t]=dist[now]+fl[i];
                       pre[t]=now;
                       gp[t]=i;
                       if (!f[t])
                        {
                            f[t]=1;
                            Q.push(t);
                        }
                   }
               }
         }
        if (pre[n+2]==-1)return 1;
        return 0; 
    }
    void Max_flow()
    {
        int cost=0,flow=0;
        while (!spfa())
         {
             int f=1e9;
             for (int i=n+2;i>1;i=pre[i])
              f=min(f,sl[gp[i]]);
             cost+=f;
            flow+=dist[n+2]*f;
            for (int i=n+2;i>1;i=pre[i])
             {
                 sl[gp[i]]-=f;
                 sl[gp[i]^1]+=f;
             } 
         }
        if (cost<2)puts("Not possible");
        else printf("%d
    ",flow); 
    }
    void init()
    {
        printf("Instance #%d: ",++cas);
        num=0;
        memset(fi,-1,sizeof fi);
    }
    void doit()
    {
        while (m--)
         {
             scanf("%d%d%d",&x,&y,&z);
             jb(x+2,y+2,1,z);
         }
        jb(1,2,2,0);
        jb(n+1,n+2,2,0);
        Max_flow();
    }
    int main()
    {
        while (~scanf("%d%d",&n,&m),n|m)
         {
             init();
             doit();
         }
    }
  • 相关阅读:
    python 赋值操作的知识点
    python while循环语句
    python dict遍历
    python列表的切片操作
    Python做下载器需要掌握哪些
    BeautifulSoup已经安装,但仍提示No module named
    python 列表循环输出中文
    python 字符串split (string split)
    python 调用解释器
    分享python字符串去除空格的知识点
  • 原文地址:https://www.cnblogs.com/xuanyiming/p/8394031.html
Copyright © 2020-2023  润新知