hdu3549Flow Problem【最大流】

Flow Problem


Time Limit: 5000/5000 MS
(Java/Others)    Memory Limit: 65535/32768 K
(Java/Others)
Total Submission(s): 6817    Accepted
Submission(s): 3178

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 number of test cases.
For each test case, the first line
contains two integers N and M, denoting the number of vertexes and edges in the
graph. (2 <= N <= 15, 0 <= M <= 1000)
Next M lines, each line
contains three integers X, Y and C, there is an edge from X to Y and the
capacity of it is C. (1 <= X, Y <= N, 1 <= C <= 1000)

Output

For each test cases, you should output the maximum
flow from source 1 to sink N.

Sample Input

2 3 2 1 2 1 2 3 1 3 3 1 2 1 2 3 1 1 3 1

Sample Output

Case 1: 1 Case 2: 2

Author

HyperHexagon

Source

HyperHexagon‘s
Summer Gift (Original tasks)

题意:最大流

分析:最大流模板

代码:

  1 #include <iostream>
2 #include <cstdio>
3 #include <cstring>
4 #include <queue>
5 #include <vector>
6 using namespace std;
7
8 const int maxn = 20 << 1;
9 const int INF = 1000000000;
10
11 struct Edge
12 {
13 int from, to, cap, flow;
14 };
15
16 struct Dinic
17 {
18 int n, m, s, t;
19 vector<Edge> edges;
20 vector<int>G[maxn];
21 bool vis[maxn];
22 int d[maxn];
23 int cur[maxn];
24
25 void ClearAll(int n) {
26 for(int i = 0; i <= n; i++) {
27 G[i].clear();
28 }
29 edges.clear();
30 }
31
32 void AddEdge(int from, int to, int cap) {
33 edges.push_back((Edge){from, to, cap, 0} );
34 edges.push_back((Edge){to, from, 0, 0} );
35 m = edges.size();
36 G[from].push_back(m - 2);
37 G[to].push_back(m - 1);
38 //printf("%din end\n",m);
39 }
40
41 bool BFS()
42 {
43 memset(vis, 0, sizeof(vis) );
44 queue<int> Q;
45 Q.push(s);
46 vis[s] = 1;
47 d[s] = 0;
48 while(!Q.empty() ){
49 int x = Q.front(); Q.pop();
50 for(int i = 0; i < G[x].size(); i++) {
51 Edge& e = edges[G[x][i]];
52 if(!vis[e.to] && e.cap > e.flow) {
53 vis[e.to] = 1;
54 d[e.to] = d[x] + 1;
55 Q.push(e.to);
56 }
57 }
58 }
59 return vis[t];
60 }
61
62 int DFS(int x, int a) {
63 if(x == t || a == 0) return a;
64 int flow = 0, f;
65 for(int& i = cur[x]; i < G[x].size(); i++) {
66 Edge& e = edges[G[x][i]];
67 if(d[x] + 1 == d[e.to] && (f = DFS(e.to, min(a, e.cap - e.flow))) > 0) {
68 e.flow += f;
69 edges[G[x][i]^1].flow -= f;
70 flow += f;
71 a -= f;
72 if(a == 0) break;
73 }
74 }
75 return flow;
76 }
77
78 int Maxflow(int s, int t) {
79 this -> s = s; this -> t = t;
80 int flow = 0;
81 while(BFS()) {
82 memset(cur, 0, sizeof(cur) );
83 flow += DFS(s, INF);
84 }
85 return flow;
86 }
87 };
88
89 Dinic g;
90
91 int main()
92 {
93 int t;
94 scanf("%d",&t);
95 int n, m;
96 int a, b, c;
97 for(int kase = 1; kase <= t; kase++) {
98 scanf("%d %d",&n, &m);
99 g.ClearAll(maxn);
100 for(int i = 0;i < m; i++ ) {
101 scanf("%d %d %d",&a, &b, &c);
102 g.AddEdge(a, b, c);
103 }
104 printf("Case %d: %d\n",kase, g.Maxflow(1, n) );
105 }
106 return 0;
107 }

View
Code

hdu3549Flow Problem【最大流】,布布扣,bubuko.com

时间: 2024-12-18 04:39:39

hdu3549Flow Problem【最大流】的相关文章

hdu3549--Flow Problem(初识最大流)

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

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

HDU3549Flow Problem(模板题)

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

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

UVA 10779 Collectors Problem[最大流]

from:neopenx 题目大意:Bob有一些贴纸,他可以和别人交换,他可以把自己独有的贴纸拿出去,也可以把重复的贴纸拿出去(有时候把独有的贴纸而不是重复的贴纸拿出去能换到更多贴纸). Bob的朋友也有一些贴纸,但是他们只会拿自己重复的贴纸和Bob换,而且换的是自己没有的贴纸. 求Bob最后最多能有多少种贴纸. 解题思路: 题目意思很明确了.就算把重复的贴纸拿出去也不一定最优,贪心就不用尝试了. 全局资源调配得使用网络流模型. 建图方式: ①S点(看作是Bob)->所有物品:连一条边,cap是

hdu 4975 A simple Gaussian elimination problem 最大流+找环

原题链接 http://acm.hdu.edu.cn/showproblem.php?pid=4975 这是一道很裸的最大流,将每个点(i,j)看作是从Ri向Cj的一条容量为9的边,从源点除法连接每个Ri,再从每个Ci连接至汇点.如若最大流不是滿流,则问题无解.这道题的关键就是在于如何判断是否有多解.考虑这样一个事实,若残余网络上有多个点构成一个环,那么流量可在这个环上调整,某条边上多余的流量可以被环上的其他的边弥补回来.所以如果残余网络上存在一个边数大于2的环,那么问题则是多解.我判断是否有环

zoj3362 Beer Problem费用流

费用流 双向边 (u,v,f,c)  拆分成4条边 (u,v,f,c)  (v,u,0,-c)  (v,u,f,c)  (u,v,0,-c) 建立城市->汇点(u,T,inf,-price) #include <iostream> #include <string.h> #include <stdio.h> #include <algorithm> #include <queue> #define V 800+10 #define E 80

HDOJ3549-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 number of test

HDU3549:Flow Problem(最大流入门EK)

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <queue> #include <math.h> #define inf 0x3f3f3f3f using namespace std; int map[50][50],v[50],pre[1000]; int n,m; int bfs(int s,int t) { memset(v,0,sizeof(v));