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 Edge {
11     int from, to, cap, flow;
12     Edge(int u, int v, int c, int f):from(u), to(v), cap(c), flow(f) {}
13 };
14
15 struct EdmondsKarp {
16     int n, m;
17     vector<Edge> edges;
18     vector<int> G[maxn];
19     int a[maxn];
20     int p[maxn];
21
22     void init(int n) {
23         for (int i = 0; i < n; i++) G[i].clear();
24         edges.clear();
25     }
26
27     void AddEdge(int from, int to, int cap) {
28         edges.push_back(Edge(from, to, cap, 0));
29         edges.push_back(Edge(to, from, 0, 0));
30         m = edges.size();
31         G[from].push_back(m - 2);
32         G[to].push_back(m - 1);
33     }
34
35     int Maxflow(int s, int t) {
36         int flow = 0;
37         for (;;) {
38             memset(a, 0, sizeof(a));
39             queue<int> Q;
40             Q.push(s);
41             a[s] = INF;
42             while (!Q.empty()) {
43                 int x = Q.front(); Q.pop();
44                 for (int i = 0; i < G[x].size(); i++) {
45                     Edge& e = edges[G[x][i]];
46                     if (!a[e.to] && e.cap > e.flow) {
47                         p[e.to] = G[x][i];
48                         a[e.to] = min(a[x], e.cap - e.flow);
49                         Q.push(e.to);
50                     }
51                 }
52                 if (a[t]) break;
53             }
54             if (!a[t]) break;
55             for (int u = t; u != s; u = edges[p[u]].from) {
56                 edges[p[u]].flow += a[t];
57                 edges[p[u] ^ 1].flow -= a[t];
58             }
59             flow += a[t];
60         }
61         return flow;
62     }
63 };
64
65 EdmondsKarp g;
66 int cap[maxn][maxn];
67
68 int main() {
69     int n, s, t, m, kase = 0;
70     while (scanf("%d", &n) == 1 && n) {
71         g.init(n + 1);
72         scanf("%d%d%d", &s, &t, &m);
73         memset(cap, 0, sizeof(cap));
74         for (int i = 0, u, v, w; i < m; i++) {
75             scanf("%d%d%d", &u, &v, &w);
76             cap[u][v] += w;
77             cap[v][u] = cap[u][v];
78         }
79         for (int i = 1; i <= n; i++)
80             for (int j = i + 1; j <= n; j++)
81                 if (cap[i][j]) {
82                     g.AddEdge(i, j, cap[i][j]);
83                     g.AddEdge(j, i, cap[i][j]);
84                 }
85         printf("Network %d\nThe bandwidth is %d.\n\n", ++kase, g.Maxflow(s, t));
86     }
87     return 0;
88 }
时间: 2024-10-03 23:06:48

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

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

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

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

部署Exchange2013之邮件流

在安装完Exchange以后,还需要对Exchange做一些额外的设置 首选创建发送连接器,才能将邮件发送到 Internet. 打开邮件流,选中发送连接器,创建internet类别. 在下一步中的添加域输入*,代表本域 在源服务器中选择邮箱角色服务器 除了发送连接器,还有接收连接器,不过默认已经在安装完Exchange的时候自动创建了, 就不需要再做额外的配置了 要往外网发送邮件,发送连接器必须得创建才行

网络流专栏

最大流 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