UVALive 5220 Internet Bandwidth(最大流,模板题)

题目:Internet Bandwidth

题意:其实就是给定一个无向图,给出每条边的最大流量,问从起点到终点的最大流量。

赤裸裸的最大流。

由于边无向,添加的时候当成正反两条有向边添加即可。

然后就是跑一下最大流的模板。

PS:每组输出后面要跟一个空行,如果少了OJ上面没有PE,会直接判WA。

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
const int N = 110;
const int inf = 0x7fffffff;
#define pb push_back
struct Edge{
    int from, to, cap, flow;
    Edge(){}
    Edge(int from, int to, int cap, int flow):from(from),to(to),cap(cap),flow(flow){}
};
int n, m, s, t;
int d[N], cur[N];
vector<Edge> G;
vector<int> V[N];
void add(int from, int to, int cap){
    G.pb(Edge(from,to,cap,0));
    G.pb(Edge(to,from,0,0));
    int x = G.size();
    V[from].pb(x-2);
    V[to].pb(x-1);
}
bool bfs(){
    memset(d, -1, sizeof(d));
    d[s] = 0;
    queue<int> Q;
    Q.push(s);
    while(!Q.empty()){
        int x=Q.front(); Q.pop();
        for(int i=0; i<V[x].size(); i++){
            Edge &e = G[V[x][i]];
            if(d[e.to]==-1 && e.cap>e.flow){
                d[e.to] = d[x]+1;
                Q.push(e.to);
            }
        }
    }
    return d[t]!=-1;
}
int dfs(int x, int v){
    if(x==t || v==0)    return v;
    int flow=0, f;
    for(int &i=cur[x]; i<V[x].size(); i++){
        int j = V[x][i];
        Edge &e = G[j];
        if(d[e.to]==d[x]+1 && (f=dfs(e.to, min(v, e.cap-e.flow)))>0){
            flow += f;
            v -= f;
            e.flow+=f;
            G[j^1].flow-=f;
            if(!v)  break;
        }
    }
    return flow;
}
int max_flow(){
    int flow=0;
    while(bfs()){
        memset(cur,0,sizeof(cur));
        flow += dfs(s, inf);
    }
    return flow;
}
int main(){
    int ct=0;
    while(~scanf("%d", &n) && n){
        scanf("%d %d %d", &s, &t, &m);
        for(int i=1; i<=n; i++) V[i].clear();
        G.clear();
        int a, b, c;
        while(m--){
            scanf("%d %d %d", &a, &b, &c);
            add(a, b, c);
            add(b, a, c);
        }
        printf("Network %d\nThe bandwidth is %d.\n\n", ++ct, max_flow());
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值