zoukankan      html  css  js  c++  java
  • COGS 11. 运输问题1

    ★★☆   输入文件:maxflowa.in   输出文件:maxflowa.out   简单对比
    时间限制:1 s   内存限制:128 MB

    【问题描述】
        一个工厂每天生产若干商品,需运输到销售部门进行销售。从产地到销地要经过某些城镇,有不同的路线可以行走,每条两城镇间的公路都有一定的流量限制。请你计算,在不考虑其它车辆使用公路的前提下,如何充分利用所有的公路,使产地运输到销地的商品最多,最多能运输多少商品。
    【输入格式】
    输入文件有若干行
    第一行,一个整数n,表示共有n个城市(2<=n<=100),产地是1号城市,销地是n号城市。
    下面有n行,每行有n个数字。第p行第q列的数字表示城镇p与城镇q之间有无公路连接。数字为0表示无,大于0表示有公路,且该数字表示该公路流量。
    【输出格式】
    输出文件有一行
    第一行,1个整数max,表示最大流量为max。
    【输入输出样例】
    输入文件名: maxflowa.in
    6
    0 4 8 0 0 0
    0 0 4 4 1 0
    0 0 0 2 2 0
    0 0 0 0 0 7
    0 0 0 6 0 9
    0 0 0 0 0 0
    输出文件名:maxflowa.out
    8
     
    最大流 
    #include <cstdio>
    #include <queue>
    #define Max 1000
    #define inf 1e9
    
    using namespace std;
    
    struct Edge
    {
        int next,to,dis;
    }edge[Max*Max*2];
    int n,head[Max*Max*2],cnt=1,dep[Max*Max*2];
    void add(int u,int v,int l)
    {
        Edge*now=&edge[++cnt];
        now->next=head[u];
        now->to=v;
        now->dis=l;
        head[u]=cnt;
    }
    bool bfs(int s,int t)
    {
        queue<int>q;
        q.push(s);
        for(int i=1;i<=n;++i) dep[i]=inf;
        dep[s]=0;
        while(!q.empty())
        {
            int tp=q.front();
            q.pop();
            for(int i=head[tp];i;i=edge[i].next)
            {
                int v=edge[i].to;
                if(dep[v]>dep[tp]+1&&edge[i].dis)
                {
                    dep[v]=dep[tp]+1;
                    if(v==t) return 1;
                    q.push(v);
                }
            }
        }
        return 0;
    }
    int dfs(int now,int t,int came_flow)
    {
        if(t==now||came_flow==0)
        return came_flow;
        int res=0,f;
        for(int i=head[now];i;i=edge[i].next)
        {
            int v=edge[i].to;
            if(dep[v]==dep[now]+1&&edge[i].dis&&(f=dfs(v,t,min(came_flow,edge[i].dis))))
            {
                res+=f;
                came_flow-=f;
                edge[i].dis-=f;
                edge[i^1].dis+=f;
                if(came_flow==0)
                break;
            }
        }
        return res;
    } 
    int dinic(int s,int t)
    {
        int ans=0;
        while(bfs(s,t))
        ans+=dfs(s,t,inf);
        return ans;
    }
    int main()
    {
        freopen("maxflowa.in","r",stdin);
        freopen("maxflowa.out","w",stdout);
        scanf("%d",&n);
        for(int a,i=1;i<=n;++i)
        {
            for(int j=1;j<=n;++j)
            {
                scanf("%d",&a);
                if(a)
                {
                    add(i,j,a);
                    add(j,i,0); 
                }
            }
        }
        printf("%d",dinic(1,n));
        return 0;
    }
    我们都在命运之湖上荡舟划桨,波浪起伏着而我们无法逃脱孤航。但是假使我们迷失了方向,波浪将指引我们穿越另一天的曙光。
  • 相关阅读:
    软件测试七年之痒,依然热爱!我还是从前那个少年!
    我想从功能测试转向自动化测试,怎么办?
    python中的一些内置函数
    python中eval()
    集合
    列表的切片:取出来还是一个列表,可用在复制列表元素的操作
    字符串常用的方法
    dict字典,以及字典的一些基本应用
    list列表(也叫数组),以及常用的一些方法
    jsonpath的用法
  • 原文地址:https://www.cnblogs.com/ruojisun/p/6574906.html
Copyright © 2011-2022 走看看