HDU 3549 Flow Problem 流问题(最大流,入门)

题意:给个赤裸的最大流问题。

思路:EK+BFS解决。跟HDU1532几乎一样的。

 1 #include <bits/stdc++.h>
 2 #define LL long long
 3 #define pii pair<int,int>
 4 #define INF 0x7f7f7f7f
 5 using namespace std;
 6 const int N=16;
 7 int cap[N][N];
 8 int flow[N][N];
 9
10 int a[N];
11 int path[N];
12
13 vector<int> vect[N];
14
15 int BFS(int n)
16 {
17     deque<int> que;
18     que.push_back(1);
19     a[1]=INF;
20     while( !que.empty() )
21     {
22         int x=que.front();
23         que.pop_front();
24         for(int i=0; i<vect[x].size(); i++)
25         {
26             int t=vect[x][i];
27             if(!a[t] && cap[x][t]>flow[x][t])
28             {
29                 path[t]=x;
30                 a[t]=min(a[x],cap[x][t]-flow[x][t]);
31                 que.push_back(t);
32             }
33         }
34         if(a[n])    return a[n];
35     }
36     return 0;   //没有增广路了
37 }
38
39
40
41
42 int cal(int n)
43 {
44     int ans_flow=0;
45     while(true)
46     {
47         memset(a, 0, sizeof(a));
48         memset(path, 0, sizeof(path));
49
50         int tmp=BFS(n);
51         if(!tmp)    return ans_flow;
52         ans_flow+=tmp;
53
54         int ed=n;
55         while(ed!=1)
56         {
57             int from=path[ed];
58             flow[from][ed]+=tmp;
59             flow[ed][from]-=tmp;
60             ed=from;
61         }
62     }
63
64 }
65
66
67 int main()
68 {
69     freopen("input.txt", "r", stdin);
70     int t, n, m, st, ed, ca, j=0;
71     cin>>t;
72     while(t--)
73     {
74         scanf("%d%d",&n,&m);
75         for(int i=0; i<=n; i++) vect[i].clear();
76         memset(cap, 0, sizeof(cap));
77         memset(flow, 0, sizeof(flow));
78
79         for(int i=0; i<m; i++)
80         {
81             scanf("%d%d%d", &st, &ed, &ca);
82             vect[st].push_back(ed);
83             vect[ed].push_back(st);
84             cap[st][ed]+=ca;
85         }
86
87         printf("Case %d: %d\n",++j, cal(n));
88     }
89     return 0;
90 }

AC代码

时间: 2024-10-09 17:13:50

HDU 3549 Flow Problem 流问题(最大流,入门)的相关文章

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(最大流模板题)

题目链接: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

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

HDU 3549 Flow Problem (最大流ISAP)

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

HDU 3549 Flow Problem(最大流)

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

[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

hdoj 3549 Flow Problem【网络流最大流入门】

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