POJ---1273 Drainage Ditches【最大流】

本文介绍了一种使用Ford-Fulkerson算法或Dinic算法解决网络流问题的方法,具体展示了如何通过这两种算法来寻找最大流,包括代码实现。

Description

Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 
Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

Input

The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

Output

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

Sample Input

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

SOURCE:点击打开链接


题意:

   赤裸裸的网络流题目。给定点数,边数,每条边的容量,以及源点,汇点,求最大流。

解析:

   通过Ford-Fulkerson算法或者是Dinic算法,通过dfs搜索最小流进行增广路。

Ford-Fulkerson算法:

#include <iostream>
#include <cstring>
#include <algorithm>
#define MAX 205
#define INF 0x7fffffff
using namespace std;

int n,m;
int c[MAX][MAX];
bool pass[MAX];
int dfs(int s,int t,int low);//s为源点,t为汇点,low为当前流
int dfs_max_flow(int s,int t);

int main(void)
{
    int a,b,d;
    while (cin>>n>>m)
    {
        memset(c,0,sizeof(c));
        for (int i=1; i<=n; ++i)
        {
            cin>>a>>b>>d;
            c[a][b]+=d;
        }
        cout<<dfs_max_flow(1,m)<<endl;
    }
    return 0;
}

int dfs(int s,int t,int low)
{
    if (s==t)
        return low;
    pass[s]=1;
    for (int i=1; i<=m; ++i)
    {
        if (!pass[i]&&c[s][i])
        {
            int flow=dfs(i,t,min(low,c[s][i]));
            if(flow>0)  //增广该路径
            {
                c[s][i]-=flow;
                c[i][s]+=flow;
                return flow;
            }
        }
    }
    return 0;
}

int dfs_max_flow(int s,int t)
{
    memset(pass,0,sizeof(pass));
    int maxflow=0,flow;
    while (flow=dfs(s,t,INF)) //不断地增广路径进行搜索,直到无法继续增加才结束
    {
        memset(pass,0,sizeof(pass));
        maxflow+=flow;
    }
    return maxflow;
}

Dinic算法:

#include<iostream>
#include<cstring>
#include<queue>
#define maxn 205
#define INF 0x7fffffff
using namespace std;

int n, m;
int c[maxn][maxn];
int dis[maxn];//dis[i]表示到原点s的层数

bool bfs();
int  dfs(const int x,const int low);

int main(void)
{
    int s,t,d;
    while(cin>>n>>m)
    {
        memset(c,0,sizeof(c));
        for(int i = 0  ; i < m; i++)
        {
            cin>>s>>t>>d;
            c[s][t] += d;
        }
        int ans = 0;
        int res;
        while(bfs())
            while(res = dfs(1,INF))
                ans+= res;
        cout<<ans<<endl;
    }
    return 0;
}

bool bfs()// 重新建图按层数建图
{
    memset(dis,-1,sizeof(dis));
    int k;
    dis[1] = 0;
    queue<int> que;
    que.push(1);
    while(que.size())
    {
        k = que.front();
        que.pop() ;
        for( int i = 1; i<= m; i++)
            if(c[k][i] > 0 && dis[i] < 0 )// 如果 可以  可以到达 但 还没有 访问
            {
                dis[i] = dis[k]+ 1;
                que.push(i);
            }
    }
    if(dis[n] > 0)
        return true;
    else
        return false;
}

int  dfs(const int x,const int low)// 查找路径上的最小的流量
{
    int flow;
    if(x == n)
        return low;
    for(int i = 1; i<= n; i++)
        if(c[x][i] > 0 && dis[i] == dis[x] + 1)
        {
            flow=dfs(i,min(low,c[x][i]));
            if(flow>0)
            {
                c[x][i] -= flow;
                c[i][x] += flow;
                return flow;
            }
        }
    return 0;
}



根据原作 https://pan.quark.cn/s/459657bcfd45 的源码改编 Classic-ML-Methods-Algo 引言 建立这个项目,是为了梳理和总结传统机器学习(Machine Learning)方法(methods)或者算法(algo),和各位同仁相互学习交流. 现在的深度学习本质上来自于传统的神经网络模型,很大程度上是传统机器学习的延续,同时也在不少时候需要结合传统方法来实现. 任何机器学习方法基本的流程结构都是通用的;使用的评价方法也基本通用;使用的一些数学知识也是通用的. 本文在梳理传统机器学习方法算法的同时也会顺便补充这些流程,数学上的知识以供参考. 机器学习 机器学习是人工智能(Artificial Intelligence)的一个分支,也是实现人工智能最重要的手段.区别于传统的基于规则(rule-based)的算法,机器学习可以从数据中获取知识,从而实现规定的任务[Ian Goodfellow and Yoshua Bengio and Aaron Courville的Deep Learning].这些知识可以分为四种: 总结(summarization) 预测(prediction) 估计(estimation) 假想验证(hypothesis testing) 机器学习主要关心的是预测[Varian在Big Data : New Tricks for Econometrics],预测的可以是连续性的输出变量,分类,聚类或者物品之间的有趣关联. 机器学习分类 根据数据配置(setting,是否有标签,可以是连续的也可以是离散的)和任务目标,我们可以将机器学习方法分为四种: 无监督(unsupervised) 训练数据没有给定...
本系统采用微信小程序作为前端交互界面,结合Spring Boot与Vue.js框架实现后端服务及管理后台的构建,形成一套完整的电子商务解决方案。该系统架构支持单一商户独立运营,亦兼容多商户入驻的平台模式,具备高度的灵活性与扩展性。 在技术实现上,后端以Java语言为核心,依托Spring Boot框架提供稳定的业务逻辑处理与数据接口服务;管理后台采用Vue.js进行开发,实现了直观高效的操作界面;前端微信小程序则为用户提供了便捷的移动端购物体验。整套系统各模块间紧密协作,功能链路完整闭环,已通过严格测试与优化,符合商业应用的标准要求。 系统设计注重业务场景的全面覆盖,不仅包含商品展示、交易流程、订单处理等核心电商功能,还集成了会员管理、营销工具、数据统计等辅助模块,能够满足不同规模商户的日常运营需求。其多店铺支持机制允许平台方对入驻商户进行统一管理,同时保障各店铺在品牌展示、商品销售及客户服务方面的独立运作空间。 该解决方案强调代码结构的规范性与可维护性,遵循企业级开发标准,确保了系统的长期稳定运行与后续功能迭代的可行性。整体而言,这是一套技术选型成熟、架构清晰、功能完备且可直接投入商用的电商平台系统。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值