zoukankan      html  css  js  c++  java
  • 2418-nyoj备用 song ming ti(网络流,最小割)

    2418 : song ming ti

    题目描述
    After a few years, the outbreak of the crisis in a biochemical virus K, tens of thousands of people infected with the virus into a zombie.The train is the country's main vehicle. In order to prevent the virus from spreading, the government decided to block some of the train lines.However, some of the most important line blockade will bring certain economic losses, assuming that K owned m transport tracks, each track is connected with the two different city, every city representation with a string, each track has a value of loss, said the blockade of the track of economic losses.The country's leaders focus on the city's S and the city T, and he wants to ask you how much of the minimum economic loss makes S cities unable to reach the city of T.The test data guarantee that the number of different cities is not more than 20000.
    输入
    The input contains a number of groups of data, each of which is two lines.The first line is M, S, T. 
    M represents the K state-owned M rail transit track.S and T represent two cities.Next M lines, two strings and an integer loss per line.The two strings represent two cities, and the loss represents the economic loss of the rail between the two cities(m<=4e4,loss<=1e9). 
    输出
    For each group of data, you only need to output an integer to represent the least economic loss. 

    样例输入
    5 Pusan Jeju
    Daegu Pohang 1
    Pusan Daegu 2
    Daejeon Pusan 6
    Jeju Daegu 3
    Daejeon Jeju 3
    样例输出
    5

    求最小割,根据最小割定理可以知道求出最大流就能过;

    ISAP模版

    #include<map>
    #include<set>
    #include<queue>
    #include<math.h>
    #include<vector>
    #include<string>
    #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<algorithm>
    #define inf 0x3f3f3f3f3f3f3f3f
    #define ll long long
    #define maxn 100010
    #define maxm 1000100
    using namespace std;
    struct Edge{
    int to,next;ll cap,flow;
    }edge[maxm];
    int tol;
    int head[maxn];
    int gap[maxn],dep[maxn],pre[maxn],cur[maxn];
    map<string,int>m;
    void init(){tol=0;memset(head,-1,sizeof(head));memset(edge,0,sizeof(edge));}
    void addedge(int u,int v,ll w,ll rw){
        edge[tol].to=v;edge[tol].cap=w;edge[tol].next=head[u];edge[tol].flow=0;head[u]=tol++;
        edge[tol].to=u;edge[tol].cap=rw;edge[tol].next=head[v];edge[tol].flow=0;head[v]=tol++;
    }
    ll sap(int s,int t,int N){
        memset(gap,0,sizeof(gap));memset(dep,0,sizeof(dep));memset(pre,0,sizeof(pre));
        memcpy(cur,head,sizeof(head));
        int u=s;pre[u]=-1;gap[0]=N;ll ans=0;
        while(dep[s]<N){
            if(u==t){
                ll minn=inf;for(int i=pre[u];i!=-1;i=pre[edge[i^1].to])
                if(minn>edge[i].cap-edge[i].flow)
                    minn=edge[i].cap-edge[i].flow;
                for(int i=pre[u];i!=-1;i=pre[edge[i^1].to]){
                    edge[i].flow+=minn;edge[i^1].flow-=minn;
                }u=s;ans+=minn;continue;
            }
            bool flag=false;int v;
            for(int i=cur[u];i!=-1;i=edge[i].next){
                v=edge[i].to;
                if(edge[i].cap-edge[i].flow && dep[v]+1==dep[u]){
                    flag=true;cur[u]=pre[v]=i;break;
                }
            }if(flag){u=v;continue;}
            int minn=N;
            for(int i=head[u];i!=-1;i=edge[i].next)
            if(edge[i].cap-edge[i].flow && dep[edge[i].to]<minn){
                minn=dep[edge[i].to];cur[u]=i;
                }
            gap[dep[u]]--;if(!gap[dep[u]])return ans;
            dep[u]=minn+1;gap[dep[u]]++;if(u!=s)u=edge[pre[u]^1].to;
            }
            return ans;
    }
    int main(){
      int n;string st,en;
      while(~scanf("%d",&n)){
            m.clear();cin>>st>>en;init();
            m[st]=1;m[en]=2;int s=1,t=2;
            int k=3;for(int i=0;i<n;i++){
            string s1,s2;ll x;cin>>s1>>s2>>x;
            if(m[s1]==0)m[s1]=k++;
            if(m[s2]==0)m[s2]=k++;
            addedge(m[s1],m[s2],x,x);
        }
        cout<<sap(s,t,k)<<endl;
      }
    }
    


  • 相关阅读:
    EF实现增删改查
    托管代码与非托管代码的区别
    堆、栈以及队列
    C#装箱和拆箱
    Leecode刷题之旅-C语言/python-349两个数组的交集
    Leecode刷题之旅-C语言/python-344反转字符串
    Leecode刷题之旅-C语言/python-217存在重复元素
    Leecode刷题之旅-C语言/python-206反转链表
    Leecode刷题之旅-C语言/python-204计数质数
    Leecode刷题之旅-C语言/python-203移除链表元素
  • 原文地址:https://www.cnblogs.com/da-mei/p/9053268.html
Copyright © 2011-2022 走看看