HDU 3549 Flow Problem(最大流模板)

http://acm.hdu.edu.cn/showproblem.php?pid=3549

刚接触网络流,感觉有点难啊,只好先拿几道基础的模板题来练练手。

最大流的模板题。

 1 #include<iostream>
 2 #include<cstring>
 3 #include<string>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7
 8 int n, m, flow;
 9 int vis[205];
10 //路径记录
11 int pre[205];
12 //邻接表
13 int G[205][205];
14
15 void Maxflow()
16 {
17     for (;;)
18     {
19         memset(vis, 0, sizeof(vis));
20         memset(pre, 0, sizeof(pre));
21         queue<int> q;
22         q.push(1);
23         vis[1] = 1;
24         while (!q.empty())
25         {
26             int x = q.front();
27             q.pop();
28             //到达终点
29             if (x == n)  break;
30             for (int i = 1; i <= n; i++)
31             {
32                 if (!vis[i] && G[x][i] > 0)
33                 {
34                     vis[i] = 1;
35                     q.push(i);
36                     //记录好路径
37                     pre[i] = x;
38                 }
39             }
40         }
41         //没有找到增广路
42         if (!vis[n])  break;
43         int _min = 1000000;
44         for (int i = n; i != 1; i = pre[i])
45             _min = min(_min, G[pre[i]][i]);
46         for (int i = n; i != 1; i = pre[i])
47         {
48             G[i][pre[i]] += _min;
49             G[pre[i]][i] -= _min;
50         }
51         flow += _min;
52     }
53     return;
54 }
55
56 int main()
57 {
58     //freopen("D:\\txt.txt", "r", stdin);
59     int T;
60     int u, v, w;
61     cin >> T;
62     for (int kase = 1; kase <= T; kase++)
63     {
64         cin >> n >> m;
65         memset(G, 0, sizeof(G));
66         for (int i = 0; i < m; i++)
67         {
68             cin >> u >> v >> w;
69             G[u][v] += w;
70         }
71         flow = 0;
72         Maxflow();
73         cout << "Case " << kase << ": " << flow << endl;
74     }
75 }
时间: 2024-08-08 09:49:33

HDU 3549 Flow Problem(最大流模板)的相关文章

hdu - 3549 Flow Problem (最大流模板题)

http://acm.hdu.edu.cn/showproblem.php?pid=3549 Ford-Fulkerson算法. #include <iostream> #include <cstdio> #include <cmath> #include <vector> #include <cstring> #include <string> #include <algorithm> #include <stri

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

HDU 3549 Flow Problem (最大流)

链接:click here 题意:Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. 翻译:网络流量是一个众所周知的难题ACMers.给定一个图,你的任务是找出加权有向图的最大流. 输出格式: Case 1: 1 Case 2: 2 思路:跟hdu153

HDU 3549 Flow Problem(网络流模板题)

记录一下模板 #include <vector> #include <cstring> #include <algorithm> #include <cstdio> #include <queue> using namespace std; #define maxn 1100 #define INF 0x7f7f7f7f struct Edge { int from,to,cap,flow; }; struct Dinic { int n,m,s

hdu 3549 Flow Problem(最大流模板题)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input

hdu 3549 Flow Problem (网络最大流)

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Submission(s): 6674    Accepted Submission(s): 3112 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, yo

HDU 3549 Flow Problem 网络最大流问题 Edmonds_Karp算法

题目链接:HDU 3549 Flow Problem Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8218    Accepted Submission(s): 3824 Problem Description Network flow is a well-known difficult problem f

网络流 HDU 3549 Flow Problem

网络流 HDU 3549 Flow Problem 题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法进行求解,注意的问题有两个: 1. 每次增广的时候,反向流量也要进行更行,一开始没注意,WA了几次 ORZ 2. 对于输入的数据,容量要进行累加更新. // 邻接矩阵存储 #include <bits/stdc++.h> using namespace std; const int INF = 0x7fffffff; const i

[ACM] hdu 3549 Flow Problem (最大流模板题)

Flow Problem Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, your task is to find out the maximum flow for the weighted directed graph. Input The first line of input contains an integer T, denoting the nu

HDU 3549 Flow Problem (用一道最裸的最大流开启网络流算法之路)

Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 9423    Accepted Submission(s): 4405 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, y