• Gasoline (网络流+二分)


    G. Gasoline
    time limit per test0.5 seconds
    memory limit per test1024 megabytes
    inputstandard input
    outputstandard output
    After the end of the truck drivers' strike, you and the rest of Nlogônia logistics specialists now have the task of planning the refueling of the gas stations in the city. For this, we collected information on stocks of R refineries and about the demands of P gas stations. In addition, there are contractual restrictions that some refineries cannot supply some gas stations; When a refinery can provide a station, the shorter route to transport fuel from one place to another is known.
    
    The experts' task is to minimize the time all stations are supplied, satisfying their demands. The refineries have a sufficiently large amount of trucks, so that you can assume that each truck will need to make only one trip from a refinery to a gas station. The capacity of each truck is greater than the demand of any gas station, but it may be necessary to use more than one refinery.
    
    Input
    The first line of the input contains three integers P,R,C, respectively the number of gas stations, the number of refineries and the number of pairs of refineries and gas stations whose time will be given (1≤P,R≤1000; 1≤C≤20000). The second line contains P integers Di (1≤Di≤104), representing the demands in liters of gasoline of the gas stations i=1,2,…,P, in that order. The third line contains R integers Ei (1≤Ei≤104), representing stocks, in liters of gasoline, of refineries i=1,2,…,R, in that order. Finally, the latest C lines describe course times, in minutes, between stations and refineries. Each of these rows contains three integers, I,J,T (1≤I≤P; 1≤J≤R; 1≤T≤106), where I is the ID of a post, J is the ID of a refinery and T is the time in the course of a refinery truck J to I. No pair (J,I) repeats. Not all pairs are informed; If a pair is not informed, contractual restrictions prevents the refinery from supplying the station.
    
    Output
    Print an integer that indicates the minimum time in minutes for all stations to be completely filled up. If this is not possible, print −1.
    
    Examples
    inputCopy
    3 2 5
    20 10 10
    30 20
    1 1 2
    2 1 1
    2 2 3
    3 1 4
    3 2 5
    outputCopy
    4
    inputCopy
    3 2 5
    20 10 10
    25 30
    1 1 3
    2 1 1
    2 2 4
    3 1 2
    3 2 5
    outputCopy
    5
    inputCopy
    4 3 9
    10 10 10 20
    10 15 30
    1 1 1
    1 2 1
    2 1 3
    2 2 2
    3 1 10
    3 2 10
    4 1 1
    4 2 2
    4 3 30
    outputCopy
    -1
    inputCopy
    1 2 2
    40
    30 10
    1 1 100
    1 2 200
    outputCopy
    200
    View problem

    Gym - 101908G

    思路:

    • 看到题目:这种注满类型的题,明显就是网络流
    • 但是建图的时候有一个 t 的元素,很不好处理,
    • 发现 t 就是题目要求的答案,就利用二分答案,来把 t 这个元素分开来。
    #include <bits/stdc++.h>
    using namespace std;
    #define ri register int 
    #define M 2010
    
    template <class G> void read(G &x)
    {
        x=0;int f=0;char ch=getchar();
        while(ch<'0'||ch>'9'){f|=ch=='-';ch=getchar();}
        while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}
        x=f?-x:x;
        return ;
    }
    int vv[M];
    int nxt[200005],head[M],to[200005],cent=1;long long val[200005];
    void add(int a,int b,int c)
    {
        nxt[++cent]=head[a];
        to[cent]=b;
        val[cent]=c;
        head[a]=cent;
    }
    struct dain{
        int a,b,val;
        bool operator <(const dain &t)const
        {
            return val<t.val;
        }
    }p[200005];
    int P,R,C;
    long long kk;
    int dep[M];
    int bfs(int a)
    {
        memset(dep,0,sizeof(dep));//
        queue <int> q;
        q.push(a);
        dep[a]=1;//
        while(!q.empty())
        {
            int a=q.front();q.pop();
            for(ri i=head[a];i;i=nxt[i])
            {
                int b=to[i];
                if(!dep[b]&&val[i])
                { 
                    dep[b]=dep[a]+1;q.push(b);
                }
            }
        }
        return dep[P+R+1];
    }
    long long dfs(int a,long long in)
    {
        if(a==P+R+1) return in;
        long long out=0;
        for(ri i=head[a];i&&in;i=nxt[i])
        {
            int b=to[i];
            if(dep[b]==dep[a]+1&&val[i])
            {
                long long  res=dfs(b,min(in,val[i]));
                out+=res;
                in-=res;
                val[i]-=res;
                val[i^1]+=res;
            }
        }
        if(out==0) dep[a]=0;
        return out;
    }
    bool ck(int a)
    {
        memset(head,0,sizeof(head));
        cent=1;
        for(ri i=1;i<=P;i++)
        {
            add(0,i,vv[i]);
            add(i,0,0);
        }
        for(ri i=1;i<=C;i++)
        {
            if(p[i].val>a) break;
            add(p[i].a,p[i].b+P,1e9);
            add(p[i].b+P,p[i].a,0); 
        }
        for(ri i=1;i<=R;i++)
        {
            add(i+P,P+R+1,vv[i+P]);
            add(P+R+1,i+P,0);
        }
        long long ans=0;
        while(bfs(0))
        {
            ans+=dfs(0,1e18);
        }
        if(ans!=kk) return 0;
        else return 1;
    }
    int main(){
        
        read(P);read(R);read(C);
        for(ri i=1;i<=P;i++)
        {
            read(vv[i]);
            kk+=vv[i];
        }
        for(ri i=1;i<=R;i++)
        {
            read(vv[i+P]);
        }
        for(ri i=1;i<=C;i++)
        {
            read(p[i].a);
            read(p[i].b);
            read(p[i].val);
        }
        sort(p+1,p+1+C);
        
        int l=1,r=1e6;
        while(l<r)
        {
            int mid=(l+r)>>1;
            if(ck(mid))
            {
                r=mid;
            }
            else l=mid+1;
        }
        if(l==1e6)
        {
            if(ck(l)) printf("%d",l);
            else printf("-1");
            return 0;
        }
        
        printf("%d",l);
        return 0;
    } 
    View Code

    后记:

    • 建图的时候一定要想清楚如何建,一般这种河2边类型的时候,河的左边连接s,右边连接t,连的时候分别保存搁置的容量
    • 在更具需要,建立桥梁,一般桥梁大小直接 最大就行拉。
    • 最个人编程习惯: 有val ,不要写 vv,写zhi,容易区分,不然很容易搞混了,在代码里面,还不容易找错QAQ
    • 当然还有一些写网络流易漏掉的地方。
  • 相关阅读:
    linux 添加、删除 route
    linux 添加、删除 ip
    oracle virtualbox 添加共享硬盘2
    centos 7.4 + udev + 12.2.0.1 + asm 单点安装
    initdb 简介
    EBS 修改数据库用户apps、网页登录用户sysadmin密码
    postgresql 的几个 timeout 参数
    postgresql 控制命令执行时长 statement_timeout
    postgresql 连接客户端存在 CLOSE_WAIT、TIME_WAIT 状态
    postgresql lsn/location 获取 wal/xlog 文件名
  • 原文地址:https://www.cnblogs.com/Lamboofhome/p/16199313.html
Copyright © 2020-2023  润新知