UVA820 Internet Bandwidth

很裸的模版题,就是敲起来稍微麻烦一点。

#include<bits/stdc++.h>
using namespace std;

struct Edge
{
    int v,cap;
};

const int maxn = 101;
vector<Edge> E;
vector<int> G[maxn];
#define PB push_back
void AddEdge(int u,int v,int c)
{
    G[u].PB(E.size());
    E.PB({v,c});
    G[v].PB(E.size());
    E.PB({u,0});
}

int S,T;

bool vis[maxn];
int d[maxn];
bool bfs()
{
    memset(vis,0,sizeof(vis));
    queue<int> q; q.push(S); d[S] = 0; vis[S] = true;
    while(q.size()){
        int u = q.front(); q.pop();
        for(int i = 0; i < G[u].size(); i++){
            Edge &e = E[G[u][i]];
            if(!vis[e.v] && e.cap>0){
                vis[e.v] = true;
                d[e.v] = d[u]+1;
                q.push(e.v);
            }
        }
    }
    return vis[T];
}

int cur[maxn];
int dfs(int u,int a)
{
    if(u == T||!a) return a;
    int flow = 0,f;
    for(int &i = cur[u]; i < G[u].size(); i++){
        Edge &e = E[G[u][i]];
        if(d[e.v] == d[u]+1 && (f = dfs(e.v,min(e.cap,a)))>0){
            flow += f;
            e.cap -= f;
            E[G[u][i]^1].cap += f;
            a -= f;
            if(!a) break;
        }
    }
    return flow;
}

const int INF = 0x3f3f3f3f;
int MaxFlow()
{
    int flow = 0;
    while(bfs()){
        memset(cur,0,sizeof(cur));
        flow += dfs(S,INF);
    }
    return flow;
}

int cap[maxn][maxn];

int main()
{
    //freopen("in.txt","r",stdin);
    int n,kas = 0;
    while(scanf("%d",&n),n){
        for(int i = 1; i <= n; i++) G[i].clear();
        E.clear();
        memset(cap,0,sizeof(cap));
        int m; scanf("%d%d%d",&S,&T,&m);
        while(m--){
            int u,v,c; scanf("%d%d%d",&u,&v,&c);
            cap[u][v] += c;
            cap[v][u] = cap[u][v];
        }
        for(int i = 1; i <= n; i++)
        for(int j = i+1; j <= n; j++) if(cap[i][j]>0){
            AddEdge(i,j,cap[i][j]); AddEdge(j,i,cap[i][j]);
        }
        printf("Network %d\nThe bandwidth is %d.\n\n",++kas,MaxFlow());
    }
    return 0;
}
时间: 2024-10-08 10:17:45

UVA820 Internet Bandwidth的相关文章

解题报告 之 UVA820 Internet Bandwidth

解题报告 之 UVA820 Internet Bandwidth Description On the Internet, machines (nodes) are richly interconnected, and many paths may exist between a given pair of nodes. The total message-carrying capacity (bandwidth) between two given nodes is the maximal a

UVa820 Internet Bandwidth (最大流)

链接:http://bak3.vjudge.net/problem/UVA-820 分析:最大流模板题. 1 #include <cstdio> 2 #include <queue> 3 #include <cstring> 4 #include <vector> 5 using namespace std; 6 7 const int maxn = 100 + 5; 8 const int INF = 0x3f3f3f3f; 9 10 struct Edg

UVA-820 Internet Bandwidth (最大流)

题目大意:单源单汇无项网络求最大流. 题目分析:入门级别的题.但是ISAP在这儿好像不大好使?... 代码如下: # include<iostream> # include<cstdio> # include<cstring> # include<vector> # include<queue> # include<algorithm> using namespace std; const int INF=1<<30; c

light oj 1153 - Internet Bandwidth【网络流无向图】

1153 - Internet Bandwidth   PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB On the Internet, machines (nodes) are richly interconnected, and many paths may exist between a given pair of nodes. The total message-carrying cap

网络流专栏

最大流 POJ 1273 Drainage Ditches POJ 1274 The Perfect Stall (二分图匹配) POJ 1698 Alice's Chance(构图) POJ 1459 Power Network(构图) POJ 2112 Optimal Milking (二分) POJ 2455 Secret Milking Machine (二分) POJ 3189 Steady Cow Assignment (枚举) POJ 1637 Sightseeing tour (

Soj题目分类

-----------------------------最优化问题------------------------------------- ----------------------常规动态规划  SOJ1162 I-Keyboard  SOJ1685 Chopsticks SOJ1679 Gangsters SOJ2096 Maximum Submatrix  SOJ2111 littleken bg SOJ2142 Cow Exhibition  SOJ2505 The County

网络流柱

最大流量 POJ 1273 Drainage Ditches POJ 1274 The Perfect Stall (二分图匹配) POJ 1698 Alice's Chance(构图) POJ 1459 Power Network(构图) POJ 2112 Optimal Milking (二分) POJ 2455 Secret Milking Machine (二分) POJ 3189 Steady Cow Assignment (枚举) POJ 1637 Sightseeing tour

待刷题目分类

各大OJ题目归类 Posted on 2012 年 8 月 8 日 by admin ---------–最优化问题------------- --------动态规划 SOJ1162 I-Keyboard SOJ2096 Maximum Submatrix SOJ2111 littleken bg SOJ2505 The County Fair SOJ2818 QQ音速 SOJ2469 Exploring Pyramids SOJ1833 Base Numbers SOJ2009 Zeros

网络流题集

最大流POJ 1273 Drainage DitchesPOJ 1274 The Perfect Stall (二分图匹配)POJ 1698 Alice's Chance(构图)POJ 1459 Power Network(构图)POJ 2112 Optimal Milking (二分)POJ 2455 Secret Milking Machine (二分)POJ 3189 Steady Cow Assignment (枚举)POJ 1637 Sightseeing tour (混合图欧拉回路)