码迷,mamicode.com
首页 > 移动开发 > 详细

luogu1345 [USACO5.4]奶牛的电信Telecowmunication

时间:2018-01-11 23:56:30      阅读:217      评论:0      收藏:0      [点我收藏+]

标签:bool   cst   down   return   scanf   queue   log   奶牛   end   

对于每个点,把它拆成有权值为1的边相连的两个点,原边是inf。
边的起点统一加n,ss也加n
这就成了最小割

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
using namespace std;
int n, m, ss, tt, hea[205], cnt, uu, vv, maxFlow=0, lev[205];
const int oo=0x3f3f3f3f;
queue<int> d;
struct Edge{
    int too, nxt, val;
}edge[4005];
void add_edge(int fro, int too, int val){
    edge[cnt].nxt = hea[fro];
    edge[cnt].too = too;
    edge[cnt].val = val;
    hea[fro] = cnt++;
}
void addEdge(int fro, int too, int val){
    add_edge(fro, too, val);
    add_edge(too, fro, 0);
}
bool bfs(){
    memset(lev, 0, sizeof(lev));
    d.push(ss);
    lev[ss] = 1;
    while(!d.empty()){
        int x=d.front();
        d.pop();
        for(int i=hea[x]; i!=-1; i=edge[i].nxt){
            int t=edge[i].too;
            if(!lev[t] && edge[i].val>0){
                lev[t] = lev[x] + 1;
                d.push(t);
            }
        }
    }
    return lev[tt]!=0;
}
int dfs(int x, int lim){
    if(x==tt)   return lim;
    int addFlow=0;
    for(int i=hea[x]; i!=-1 && addFlow<lim; i=edge[i].nxt){
        int t=edge[i].too;
        if(lev[t]==lev[x]+1 && edge[i].val>0){
            int tmp=dfs(t, min(lim-addFlow, edge[i].val));
            edge[i].val -= tmp;
            edge[i^1].val += tmp;
            addFlow += tmp;
        }
    }
    return addFlow;
}
void dinic(){
    while(bfs())    maxFlow += dfs(ss, oo);
}
int main(){
    memset(hea, -1, sizeof(hea));
    cin>>n>>m>>ss>>tt;
    ss += n;
    for(int i=1; i<=n; i++)
        addEdge(i, i+n, 1);
    for(int i=1; i<=m; i++){
        scanf("%d %d", &uu, &vv);
        addEdge(uu+n, vv, oo);
        addEdge(vv+n, uu, oo);
    }
    dinic();
    cout<<maxFlow<<endl;
    return 0;
}

luogu1345 [USACO5.4]奶牛的电信Telecowmunication

标签:bool   cst   down   return   scanf   queue   log   奶牛   end   

原文地址:https://www.cnblogs.com/poorpool/p/8270743.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!