【HDOJ】3549 Flow Problem

网络流基础题目,Edmonds_Karp可解。

  1 /* 3549 */
  2 #include <iostream>
  3 #include <string>
  4 #include <map>
  5 #include <queue>
  6 #include <set>
  7 #include <stack>
  8 #include <vector>
  9 #include <deque>
 10 #include <algorithm>
 11 #include <cstdio>
 12 #include <cmath>
 13 #include <ctime>
 14 #include <cstring>
 15 #include <climits>
 16 #include <cctype>
 17 #include <cassert>
 18 #include <functional>
 19 using namespace std;
 20
 21 #define rep(i, a, n)     for (int i=a;i<n;++i)
 22 #define per(i, a, n)     for (int i=n-1;i>=a;--i)
 23 #define pb                 push_back
 24 #define mp                 make_pair
 25 #define all(x)             (x).begin(),(x).end()
 26 #define SZ(x)             ((int)(x).size())
 27 #define lson            l, mid, rt<<1
 28 #define rson            mid+1, r, rt<<1|1
 29
 30 const int maxn = 20;
 31 int F[maxn][maxn];
 32 int P[maxn], a[maxn];
 33 int n, m, ans;
 34 int s, t;
 35
 36 bool bfs() {
 37     int u, v;
 38     queue<int> Q;
 39
 40     memset(a, 0, sizeof(a));
 41     a[s] = INT_MAX;
 42     Q.push(s);
 43     P[s] = s;
 44
 45     while (!Q.empty()) {
 46         u = Q.front();
 47         Q.pop();
 48         for (v=1; v<=n; ++v) {
 49             if (!a[v] && F[u][v]) {
 50                 P[v] = u;
 51                 Q.push(v);
 52                 a[v] = min(a[u], F[u][v]);
 53             }
 54         }
 55     }
 56
 57     return a[t] == 0;
 58 }
 59
 60 void Edmonds_Karp() {
 61     int u, v;
 62
 63     s = 1;
 64     t = n;
 65     ans = 0;
 66
 67     while (true) {
 68         if (bfs())
 69             break;
 70         for (u=t; u!=s; u=P[u]) {
 71             F[u][P[u]] += a[t];
 72             F[P[u]][u] -= a[t];
 73         }
 74         ans += a[t];
 75     }
 76 }
 77
 78 int main() {
 79     int i, j, k;
 80     int t, tt;
 81
 82     #ifndef ONLINE_JUDGE
 83         freopen("data.in", "r", stdin);
 84         freopen("data.out", "w", stdout);
 85     #endif
 86
 87     scanf("%d", &tt);
 88     for (t=1; t<=tt; ++t) {
 89         scanf("%d %d", &n, &m);
 90         memset(F, 0, sizeof(F));
 91         while (m--) {
 92             scanf("%d %d %d", &i, &j, &k);
 93             F[i][j] += k;
 94         }
 95         Edmonds_Karp();
 96         printf("Case %d: %d\n", t, ans);
 97     }
 98
 99     #ifndef ONLINE_JUDGE
100         printf("time = %d.\n", (int)clock());
101     #endif
102
103     return 0;
104 }
时间: 2024-11-13 06:34:44

【HDOJ】3549 Flow Problem的相关文章

【HDOJ】5296 Annoying problem

LCA+RMQ.挺不错的一道题目. 思路是如何通过LCA维护费用.当加入新的点u是,费用增量为dis[u]-dis[lca(u, lower_u)] - dis[lca(u, greater_u)] + dis[lca(lower_u, greater_u)].若beg[u]大于当前最大值或小于最小值,lower_u=min of current, greater_u = max of current. 1 /* 5296 */ 2 #include <iostream> 3 #include

hdoj 3549 Flow Problem 【最大流】

题目:hdoj 3549 Flow Problem 题意:给出一个图,让你求最大流. 分析:这个题目用dinci写的,因为点比较少,而dinci复杂度O(m*n^2),但是还是跑了160ms,不知道15的神牛怎么写的. dinci的写法要注意的地方就是存图的时候要考虑怎么存,因为要更新网络残量,即反向的流量,所以这里要注意一下. 思想就不讲了,很多地方有讲. 代码: #include <cstdio> #include <cstring> #include <iostream

【HDOJ】3509 Buge&#39;s Fibonacci Number Problem

快速矩阵幂,系数矩阵由多个二项分布组成.第1列是(0,(a+b)^k)第2列是(0,(a+b)^(k-1),0)第3列是(0,(a+b)^(k-2),0,0)以此类推. 1 /* 3509 */ 2 #include <iostream> 3 #include <string> 4 #include <map> 5 #include <queue> 6 #include <set> 7 #include <stack> 8 #incl

【HDOJ】4956 Poor Hanamichi

基本数学题一道,看错位数,当成大数减做了,而且还把方向看反了.所求为最接近l的值. 1 #include <cstdio> 2 3 int f(__int64 x) { 4 int i, sum; 5 6 i = sum = 0; 7 while (x) { 8 if (i & 1) 9 sum -= x%10; 10 else 11 sum += x%10; 12 ++i; 13 x/=10; 14 } 15 return sum; 16 } 17 18 int main() { 1

【HDOJ】1099 Lottery

题意超难懂,实则一道概率论的题目.求P(n).P(n) = n*(1+1/2+1/3+1/4+...+1/n).结果如果可以除尽则表示为整数,否则表示为假分数. 1 #include <cstdio> 2 #include <cstring> 3 4 #define MAXN 25 5 6 __int64 buf[MAXN]; 7 8 __int64 gcd(__int64 a, __int64 b) { 9 if (b == 0) return a; 10 else return

【HDOJ】2844 Coins

完全背包. 1 #include <stdio.h> 2 #include <string.h> 3 4 int a[105], c[105]; 5 int n, m; 6 int dp[100005]; 7 8 int mymax(int a, int b) { 9 return a>b ? a:b; 10 } 11 12 void CompletePack(int c) { 13 int i; 14 15 for (i=c; i<=m; ++i) 16 dp[i]

【HDOJ】1818 It&#39;s not a Bug, It&#39;s a Feature!

状态压缩+优先级bfs. 1 /* 1818 */ 2 #include <iostream> 3 #include <queue> 4 #include <cstdio> 5 #include <cstring> 6 #include <cstdlib> 7 #include <algorithm> 8 using namespace std; 9 10 #define MAXM 105 11 12 typedef struct {

【HDOJ】2424 Gary&#39;s Calculator

大数乘法加法,直接java A了. 1 import java.util.Scanner; 2 import java.math.BigInteger; 3 4 public class Main { 5 public static void main(String[] args) { 6 Scanner cin = new Scanner(System.in); 7 int n; 8 int i, j, k, tmp; 9 int top; 10 boolean flag; 11 int t

HDU 3549 Flow Problem ( 最大流 -EK 算法)

C++,G++的读取速度差距也太大了 Flow Problem 题意:n,m表示n个点m条有向带权边 问:从1-n最大流多少 裸最大流,拿来练手,挺不错的 #include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <queue> #include <algorithm> const int N = 210; #define