hdu------(3549)Flow Problem(最大流(水体))

Flow Problem

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

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<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<queue>
 5 using namespace std;
 6 const int inf=0x3f3f3f3f;
 7 int mat[30][30];
 8 int dist[31];
 9 int n,m;
10 int min(int a,int b)
11 {
12 return a<b?a:b;
13 }
14 bool bfs(int st,int to){
15     memset(dist,-1,sizeof(dist));
16     queue<int>q;
17     q.push(st);
18     dist[st]=0;
19     int t;
20     while(!q.empty()){
21        t=q.front();
22        q.pop();
23     for(int i=1;i<=n;i++){
24         if(dist[i]<0&&mat[t][i]>0){
25             dist[i]=dist[t]+1;
26             if(i==to)return 1;
27             q.push(i);
28         }
29     }
30     }
31   return 0;
32 }
33 int dfs(int st,int to,int flow)
34 {
35     int tem;
36     if(st==to||flow==0) return flow;
37     for(int i=1;i<=n;i++){
38       if((dist[i]==dist[st]+1)&&mat[st][i]>0&&(tem=dfs(i,to,min(mat[st][i],flow))))
39       {
40           mat[st][i]-=tem;
41           mat[i][st]+=tem;
42           return tem;
43       }
44     }
45   return 0;
46 }
47 int Dinic(int st,int en)
48 {
49     int ans=0;
50     while(bfs(st,en))
51         ans+=dfs(st,en,inf);
52    return ans;
53 }
54 int main(){
55   int cas,i,a,b,c;
56   scanf("%d",&cas);
57   for(i=1;i<=cas;i++){
58       scanf("%d%d",&n,&m);
59       memset(mat,0,sizeof(mat));
60      while(m--){
61        scanf("%d%d%d",&a,&b,&c);
62        mat[a][b]+=c;
63      }
64    printf("Case %d: %d\n",i,Dinic(1,n));
65   }
66   return 0;
67 }

时间: 2024-12-21 23:13:10

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,