zoukankan      html  css  js  c++  java
  • UVAL1277_Cops and Thieves

    单源点汇点无向图,要阻隔某个点的流量,必须在一个点上消耗一定的价值,问你能否在消耗价值不超过k的前提下,阻隔源点到汇点的流量。

    直接对于有权值的点拆点,拆后边容量即为点权。其余的点的容量无穷,最大流即可。

    召唤代码君:

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #define maxn 5555
    #define maxm 555555
    using namespace std;
    
    const int inf=11111;
    int to[maxm],c[maxm],next[maxm],first[maxn],edge;
    int a[maxn],d[maxn],tag[maxn],TAG=222;
    bool can[maxn];
    int Q[maxm],bot,top;
    int n,m,l,s,t;
    
    void _init()
    {
        edge=-1;
        for (int i=1; i<=n+n; i++) first[i]=-1;
    }
    
    void addedge(int U,int V,int W)
    {
        edge++;
        to[edge]=V,c[edge]=W,next[edge]=first[U],first[U]=edge;
        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] && 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=first[cur]; i!=-1; i=next[i])
            if (c[i] && d[to[i]]==d[cur]-1 && tag[to[i]]==TAG && !can[to[i]])
            {
                k=dfs(to[i],min(c[i],num));
                if (k) num-=k,c[i]-=k,c[i^1]+=k;
                if (!num) break;
            }
        if (num) can[cur]=true;
        return tmp-num;
    }
    
    int main()
    {
        int U,V,Flow,K;
        while (scanf("%d",&K)!=EOF)
        {
            scanf("%d%d%d%d",&n,&m,&s,&t);
            for (int i=1; i<=n; i++) scanf("%d",&a[i]);
            _init();
            for (int i=1; i<=m; i++)
            {
                scanf("%d%d",&U,&V);
                addedge(U+n,V,inf),addedge(V+n,U,inf);
            }
            if (s==t)
            {
                puts("NO");
                continue;
            }
            s+=n;
            for (int i=1; i<=n; i++) addedge(i,i+n,a[i]);
            for (Flow=0; Flow<=K && bfs(); bfs()) Flow+=dfs(s,inf);
            if (K>=Flow) puts("YES");
                else puts("NO");
        }
        return 0;
    }
  • 相关阅读:
    梦断代码阅读笔记之三
    梦断代码阅读笔记二
    输入法评价
    1的个数
    团队冲刺-10
    第一阶段个人总结03
    第一阶段个人总结02
    第一阶段个人总结01
    学习进度条第十周
    学习进度条第九周
  • 原文地址:https://www.cnblogs.com/lochan/p/3870718.html
Copyright © 2011-2022 走看看