HDU 3549(网络流入门之最大流)

Flow Problem

Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 10327    Accepted Submission(s): 4866

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)

n个点m条边,找到1到n使得流量最大。

解题思路;找出所有的增广路径,然后再找增广路径上的边,找出最小的流量!!

#include <cstdio>
#include <cmath>
#include <queue>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
int n,m;
int map1[1001][1001];
int pre[1001];
int vis[1001];
int s,t;
bool BFS()
{
    int i,cur;
    queue<int>q;
    memset(pre,0,sizeof(pre));
    memset(vis,0,sizeof(vis));
    vis[s]=1;
    q.push(s);
    while(!q.empty())
    {
        cur=q.front();
        q.pop();
        if(cur==t)
            return 1;
        for(i=1; i<=n; i++)
        {
            if(!vis[i] &&map1[cur][i])
            {
                q.push(i);
                pre[i]=cur;
                vis[i]=1;
            }
        }
    }
    return 0;
}//找增广路径
int Max_flow()
{
    int i,ans=0;
    while(1)
    {
        if(!BFS())
            return ans;
        int Min=0x7f7f7f7f;
        for(i=t; i!=s; i=pre[i])
            Min=min(Min,map1[pre[i]][i]);
        for(i=t; i!=s; i=pre[i])
        {
            map1[pre[i]][i]-=Min;
            map1[i][pre[i]]+=Min;
        }
        ans+=Min;
    }
}//找增广路径上的最小流量
int main()
{
    int T;
    cin>>T;
    int v,u,c;
    int k=1;
    while(T--)
    {
        scanf("%d%d",&n,&m);
        memset(map1,0,sizeof(map1));
        s=1,t=n;
        while(m--)
        {
            scanf("%d%d%d",&u,&v,&c);
            map1[u][v]+=c;
        }
        printf("Case %d: %d\n",k++,Max_flow());
    }
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-22 20:15:10

HDU 3549(网络流入门之最大流)的相关文章

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 网络流最大流 Ford-Fulkerson

Ford-Fulkerson方法依赖于三种重要思想,这三个思想就是:残留网络,增广路径和割. Ford-Fulkerson方法是一种迭代的方法.开始时,对所有的u,v∈V有f(u,v)=0,即初始状态时流的值为0.在每次迭代中,可通过寻找一条"增广路 径"来增加流值.增广路径可以看成是从源点s到汇点t之间的一条路径,沿该路径可以压入更多的流,从而增加流的值.反复进行这一过程,直至增广路 径都被找出来,根据最大流最小割定理,当不包含增广路径时,f是G中的一个最大流. #include &

题解报告:hdu 3549 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

HDU 3549 Flow Problem(最大流模板)

http://acm.hdu.edu.cn/showproblem.php?pid=3549 刚接触网络流,感觉有点难啊,只好先拿几道基础的模板题来练练手. 最大流的模板题. 1 #include<iostream> 2 #include<cstring> 3 #include<string> 4 #include<algorithm> 5 #include<queue> 6 using namespace std; 7 8 int n, m,

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,

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

[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

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