hdu 3652 最大流入门

明天再拍一遍

 1 #include <iostream>
 2 #include <queue>
 3 using namespace std;
 4 const int N = 210;
 5 const int INF = 0x7FFFFFFF;
 6 int n,m,map[N][N],path[N],flow[N],start,end;
 7 queue<int> q;
 8 int bfs()
 9 {
10     int i,t;
11     while(!q.empty()) q.pop();
12     memset(path,-1,sizeof(path));
13     path[start]=0,flow[start]=INF;
14     q.push(start);
15     while(!q.empty())
16     {
17         t=q.front();
18         q.pop();
19         if(t==end) break;
20         for(i=1;i<=m;i++)
21         {
22             if(i!=start && path[i]==-1 && map[t][i])
23             {
24                 flow[i]=flow[t]<map[t][i]?flow[t]:map[t][i];
25                 q.push(i);
26                 path[i]=t;
27             }
28         }
29     }
30     if(path[end]==-1) return -1;
31     return flow[m];                   //一次遍历之后的流量增量
32 }
33 int Edmonds_Karp()
34 {
35     int max_flow=0,step,now,pre;
36     while((step=bfs())!=-1)
37     {          //找不到增路径时退出
38         max_flow+=step;
39         now=end;
40         while(now!=start)
41         {
42             pre=path[now];
43             map[pre][now]-=step;      //更新正向边的实际容量
44             map[now][pre]+=step;      //添加反向边
45             now=pre;
46         }
47     }
48     return max_flow;
49 }
50  int main()
51  {
52     int i,u,v,cost;
53     while(scanf("%d %d",&n,&m)!=EOF)
54     {
55         memset(map,0,sizeof(map));
56         for(i=0;i<n;i++)
57         {
58             scanf("%d %d %d",&u,&v,&cost);
59             map[u][v]+=cost;           //not just only one input
60         }
61         start=1,end=m;
62         printf("%d\n",Edmonds_Karp());
63     }
64     return 0;
65 }
时间: 2024-10-23 22:45:25

hdu 3652 最大流入门的相关文章

HDU 3549 最大流入门

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

hdu 3549 最大流入门题

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 [科普]什么是BestCoder?如何参加? Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Total Submission(s): 8862    Accepted Submission(s): 4168 Problem Description

NYOJ 323 &amp;&amp; HDU 1532 &amp;&amp; POJ 1273 Drainage Ditches (网络流之最大流入门)

链接:click here 题意:给出n个河流,m个点,以及每个河流的流量,求从1到m点的最大流量. 思路:最裸的网络流题目  意思就是求从源点到汇点的最大流. 第一道网络流,一边看着书上的介绍,一边敲下代码: 用的是网络流算法ford-fulkerson 题目数据量小,邻接表和邻接矩阵都可以过 代码: #include <ctype.h> //最大流 入门 #include <stdio.h> #include <vector> #include <stdlib

【HDU 3652】 B-number (数位DP)

B-number Problem Description A wqb-number, or B-number for short, is a non-negative integer whose decimal form contains the sub- string "13" and can be divided by 13. For example, 130 and 2613 are wqb-numbers, but 143 and 2639 are not. Your task

HDU 2084 数塔 --- 入门DP

HDU 2084 数塔 从下往上递推,状态转移方程 dp[i][j] = max( dp[i+1][j], dp[i+1][j+1]) + a[i][j]; /* HDU 2084 数塔 --- 入门DP */ #include <cstdio> const int N = 105; int dp[N][N]; int MAX(int a, int b){ return a > b ? a : b; } int main() { #ifdef _LOCAL freopen("D

HDU 1241 Oil Deposits --- 入门DFS

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1241 /* HDU 1241 Oil Deposits --- 入门DFS */ #include <cstdio> int m, n; //n行m列 char mapp[105][105]; /* 将和i,j同处于一个连通块的字符标记出来 */ void dfs(int i, int j){ if (i < 0 || j < 0 || i >= n || j >= m

hdu 4888 最大流给出行列和求矩阵

第一步,考虑如何求是否有解.使用网络流求解,每一行和每一列分别对应一个点,加上源点和汇点一共有N+M+2个点.有三类边: 1. 源点 -> 每一行对应的点,流量限制为该行的和 2. 每一行对应的点 -> 每一列对应的点,流量限制为 K 3. 每一列对应的点 -> 汇点,流量限制为该列的和 对上图做最大流,若源点出发的边和到达汇点的边全都满流,则有解,否则无解.若要求构造方案,则 (i,j) 对应的整数就是行 i–> 列 j 的流量. 第二步,考虑解是否唯一.显然,解唯一的充分必要条

hdu 4975最大流与4888类似但是有很吊的优化最大流

//来自潘神的优化 #include<stdio.h> #include<string.h> #include<queue> using namespace std; #define inf 0x3fffffff #define N 1100 struct node { int u,v,w,next; }bian[N*N*4]; int head[N],yong,dis[N],work[N]; void init(){ yong=0; memset(head,-1,si

hdu 1086(计算几何入门题——计算线段交点个数)

链接:http://acm.hdu.edu.cn/showproblem.php?pid=1086 You can Solve a Geometry Problem too Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 7167    Accepted Submission(s): 3480 Problem Description Ma