hdu 3549 Flow Problem 【最大流】

其实还是不是很懂dinic-----

抄了一个模板---

http://www.cnblogs.com/naturepengchen/articles/4403408.html

先放在这里~~~

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<queue>
 6 using namespace std;
 7
 8 const int maxn = 10005;
 9 const int INF = (1 << 30) - 1;
10
11 struct Edge{
12     int v,next,c;
13 }e[maxn];
14
15 int st,ed,lev[maxn],first[maxn],now[maxn],ecnt;
16 int n,m;
17
18 void init(int a,int b){
19     st = a; ed = b;
20     memset(first,-1,sizeof(first));
21     ecnt = 0;
22 }
23
24 void addedges(int u,int v,int c){
25     e[ecnt].next = first[u];
26     e[ecnt].v = v;
27     e[ecnt].c = c;
28     first[u] = ecnt++;
29
30     e[ecnt].next = first[v];
31     e[ecnt].v = u;
32     e[ecnt].c = 0;
33     first[v] = ecnt++;
34 }
35
36 bool bfs(){
37     queue<int> q;
38     while(!q.empty()) q.pop();
39     q.push(st);
40     memset(lev,-1,sizeof(lev));
41     lev[st] = 0;
42     while(!q.empty()){
43         int x = q.front();q.pop();
44         for(int i = first[x];~i;i = e[i].next){
45             int v = e[i].v;
46             if(lev[v] < 0 && e[i].c > 0){
47                 lev[v] = lev[x] + 1;
48                 q.push(v);
49             }
50         }
51     }
52     return lev[ed] != -1;
53 }
54
55 int dfs(int p,int minf){
56     if(p == ed || minf == 0) return minf;
57     for(int &i = now[p];~i;i = e[i].next){
58         int v = e[i].v;
59         if(lev[v] == lev[p] + 1 && e[i].c > 0){
60             int d = dfs(v,min(e[i].c,minf));
61             if(d > 0){
62                 e[i].c -= d;
63                 e[i^1].c += d;
64                 return d;
65             }
66         }
67     }
68     return 0;
69 }
70
71 int dinic(){
72     int max_flow = 0,p1;
73     while(bfs()){
74         memcpy(now,first,sizeof(first));
75         while((p1 = dfs(st,INF)) > 0)
76         max_flow += p1;
77     }
78     return max_flow;
79 }
80
81 int main(){
82     int T;
83     int kase = 0;
84     scanf("%d",&T);
85     while(T--){
86         scanf("%d %d",&n,&m);
87         init(1,n);
88         for(int i = 0;i < m;i++){
89             int a,b,c;
90             scanf("%d %d %d",&a,&b,&c);
91             addedges(a,b,c);
92         }
93         printf("Case %d: %d\n",++kase,dinic());
94     }
95     return 0;
96 }

时间: 2024-10-30 00:14:28

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

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

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

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