牛客练习赛43(网络流)

链接:https://ac.nowcoder.com/acm/contest/548/E
来源:牛客网
思路:建立源点S终点T,把每一个城市看成两个点,如第i个城市可分为第i和第i+1个点,S->i为第i个城市排水量,i+n->T为第i个城市一天得清洁水量

#include<bits/stdc++.h>

using namespace std;

using LL=long long;

const LL inf=0x3f3f3f3f3f3f3f3f;

const int up=1e9;

const int maxn=405;

const int M=162000;

int head[maxn],nxt[M],to[M],w[M],tot=-1;

int S,T;

int n,m;

int sum;

 

void add(int u,int v,int we)

{

    tot++;

    nxt[tot]=head[u];

    head[u]=tot;

    to[tot]=v;

    w[tot]=we;

}

 

void add_edge(int u,int v,int we)

{

    add(u,v,we);

    add(v,u,0);

}

 

LL cost[maxn][maxn];

struct node

{

    int x,y;

}a[M/4];

 

int cur[maxn],dep[maxn];

queue<int>q;

bool bfs()

{

    memset(dep,0,sizeof(dep));

    while(q.size())

        q.pop();

    dep[S]=1;

    q.push(S);

    int u;

    while(q.size())

    {

        u=q.front();

        q.pop();

        for(int i=head[u];~i;i=nxt[i])

        {

            if(dep[to[i]]==0&&w[i]>0)

            {

                dep[to[i]]=dep[u]+1;

                q.push(to[i]);

            }

        }

    }

    if(dep[T])

        return 1;

    return 0;

}

 

int dfs(int u,int dist)

{

    if(u==T||dist==0)

        return dist;

    for(int &i=cur[u];~i;i=nxt[i])

    {

        if(dep[to[i]]==dep[u]+1&&w[i]>0)

        {

            int d=dfs(to[i],min(dist,w[i]));

            if(d>0)

            {

                w[i]-=d;

                w[i^1]+=d;

                return d;  

            }

        }

    }  

    return 0;

}

 

int dinic()

{

    int ans=0;

    while(bfs())

    {

        for(int i=0;i<=2*n+1;i++)

            cur[i]=head[i];

        while(int d=dfs(S,up))

        {

            ans+=d;

        }

    }  

    return ans;

}

 

bool check(LL now)

{

    memset(head,-1,sizeof(head));

    tot=-1;

    int i,j;

    for(i=1;i<=n;i++)

    {

        add_edge(S,i,a[i].x);

        add_edge(i+n,T,a[i].y);

        add_edge(i,i+n,up);

    }  

    for(i=1;i<=n;i++)

        for(j=i+1;j<=n;j++)

        {

            if(cost[i][j]<=now)

            {

                add_edge(i,j+n,up);

                add_edge(j,i+n,up);

            }

        }

    if(dinic()>=sum)

        return 1;

    return 0;

}

 

int main()

{

     

    scanf("%d%d",&n,&m);

    int x,y;

    int i,j,k;

    S=0,T=2*n+1;

    for(i=1;i<=n;i++)

    {

        scanf("%d%d",&a[i].x,&a[i].y);

        sum+=a[i].x;

    }  

    for(i=1;i<=n;i++)

        for(j=1;j<=n;j++)

            cost[i][j]=inf;

    for(i=1;i<=n;i++)

        cost[i][i]=0;

    LL val;

    for(i=0;i<m;i++)

    {

        scanf("%d%d%lld",&x,&y,&val);

        cost[x][y]=min(cost[x][y],val);

        cost[y][x]=min(cost[y][x],val);

    }

    LL l=0,r=0,mid,ans=-1;

    for(k=1;k<=n;k++)

        for(i=1;i<=n;i++)

            for(j=1;j<=n;j++)

            {

                if(cost[i][j]>cost[i][k]+cost[k][j])

                {

                    cost[i][j]=cost[i][k]+cost[k][j];

                    r=max(r,cost[i][j]);

                }

            }

    while(l<=r)

    {

        mid=l+r>>1;

        if(check(mid))

        {

            ans=mid;

            r=mid-1;

        }

        else

            l=mid+1;

    }

    printf("%lld\n",ans);

    return 0;


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值