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;
  }
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值