洛谷 P2936 [USACO09JAN]全流Total Flow

简介: 题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1

题目描述

Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an apparently haphazard way. He wants to calculate the flow through the pipes.

Two pipes connected in a row allow water flow that is the minimum of the values of the two pipe's flow values. The example of a pipe with flow capacity 5 connecting to a pipe of flow capacity 3 can be reduced logically to a single pipe of flow capacity 3:

+---5---+---3---+    ->    +---3---+

Similarly, pipes in parallel let through water that is the sum of their flow capacities:

+---5---+

---+       +---    ->    +---8---+

+---3---+

Finally, a pipe that connects to nothing else can be removed; it contributes no flow to the final overall capacity:

+---5---+

---+               ->    +---3---+

+---3---+--

All the pipes in the many mazes of plumbing can be reduced using these ideas into a single total flow capacity.

Given a map of the pipes, determine the flow capacity between the well (A) and the barn (Z).

Consider this example where node names are labeled with letters:

+-----------6-----------+

A+---3---+B                      +Z

+---3---+---5---+---4---+

C       D

Pipe BC and CD can be combined:

+-----------6-----------+

A+---3---+B                      +Z

+-----3-----+-----4-----+

D Then BD and DZ can be combined:

+-----------6-----------+

A+---3---+B                      +Z

+-----------3-----------+

Then two legs of BZ can be combined:

B A+---3---+---9---+Z

Then AB and BZ can be combined to yield a net capacity of 3:

A+---3---+Z

Write a program to read in a set of pipes described as two endpoints and then calculate the net flow capacity from 'A' to 'Z'. All

networks in the test data can be reduced using the rules here.

Pipe i connects two different nodes a_i and b_i (a_i in range

'A-Za-z'; b_i in range 'A-Za-z') and has flow F_i (1 <= F_i <= 1,000). Note that lower- and upper-case node names are intended to be treated as different.

The system will provide extra test case feedback for your first 50 submissions.

约翰总希望他的奶牛有足够的水喝,因此他找来了农场的水管地图,想算算牛棚得到的水的 总流量.农场里一共有N根水管.约翰发现水管网络混乱不堪,他试图对其进行简 化.他简化的方式是这样的:

两根水管串联,则可以用较小流量的那根水管代替总流量.

两根水管并联,则可以用流量为两根水管流量和的一根水管代替它们

当然,如果存在一根水管一端什么也没有连接,可以将它移除.

请写个程序算出从水井A到牛棚Z的总流量.数据保证所有输入的水管网络都可以用上述方法 简化.

输入输出格式

输入格式:

  • Line 1: A single integer: N

  • Lines 2..N + 1: Line i+1 describes pipe i with two letters and an integer, all space-separated: a_i, b_i, and F_i

 

输出格式:

  • Line 1: A single integer that the maximum flow from the well ('A') to the barn ('Z')

 

输入输出样例

输入样例#1:
5 
A B 3 
B C 3 
C D 5 
D Z 4 
B Z 6 
输出样例#1:
3 

吐槽//或者这次该叫反思感想什么的吧

  不知怎么了,可能是连续一周每天4.5h的睡眠造成的吧。难题根本不想看不想写,就写道裸题放松一下吧。国赛将近,亚历山大啊……

解题思路

  最大流裸题,套板子吧。不过这题字符输入方法值得学习。

源代码

#include<queue>
#include<cstdio>
#include<cstring>
#include<algorithm>
int n,m;
struct Edge{
    int next,to,flow;
}e[10010]={0};
int cnt=2,head[500]={0};
void add(int u,int v,int f)
{
    e[cnt]={head[u],v,f};
    head[u]=cnt++;
    e[cnt]={head[v],u,0};
    head[v]=cnt++;
}
int s=1,t=26;
int dis[500]={0};
bool bfs()
{
    std::queue<int> q;
    memset(dis,0,sizeof(dis));
    q.push(s);
    dis[s]=1;
    while(!q.empty())
    {
        int u=q.front();
        q.pop();
        for(int i=head[u];i;i=e[i].next)
        {
            int v=e[i].to;
            if(dis[v]||!e[i].flow) continue;
            dis[v]=dis[u]+1;
            q.push(v);
        }
    }
    return dis[t]!=0;
}

int dfs(int u,int f)
{
    if(f==0||u==t) return f;
    int flow_sum=0;
    for(int i=head[u];i;i=e[i].next)
    {
        int v=e[i].to;
        if(dis[v]!=dis[u]+1||!e[i].flow) continue;
        int temp=dfs(v,std::min(e[i].flow,f-flow_sum));
        e[i].flow-=temp;
        e[i^1].flow+=temp;
        flow_sum+=temp;
        if(flow_sum>=f) break;
    }
    if(flow_sum==0) dis[u]=-1;
    return flow_sum;
}

int dinic()
{
    int ans=0;
    while(bfs())
    {
        while(int temp=dfs(s,0x7fffffff)) ans+=temp;
    }
    return ans;
}

int main()
{
    scanf("%d",&n);
    for(int i=1,f;i<=n;i++)
    {
        char u[2],v[2];
        scanf("%s%s%d",u,v,&f);
        add(u[0]-'A'+1,v[0]-'A'+1,f);
    }
    printf("%d\n",dinic());
    return 0;
}

 

目录
相关文章
|
Android开发 iOS开发 MacOS
APP备案公钥、证书MD5指纹/签名MD5值获取最简单方法
APP备案公钥、证书MD5指纹/签名MD5值获取方法,Android安卓平台、Windows平台、macOS平台,三个平台获取方法, Android平台使用 APP备案助手,各大安卓应用市场搜索 APP备案助手 即可,Windows/macOS平台使用jadx-gui工具。
11778 3
|
负载均衡 Java Nacos
SpringCloud基础1——远程调用、Eureka,Nacos注册中心、Ribbon负载均衡
微服务介绍、SpringCloud、服务拆分和远程调用、Eureka注册中心、Ribbon负载均衡、Nacos注册中心
SpringCloud基础1——远程调用、Eureka,Nacos注册中心、Ribbon负载均衡
|
移动开发 小程序 安全
对比多个笔记软件后,我选择了语雀
去年开始带团队,在面临团队文档管理共享软件选择时,我毫不犹豫的选择了语雀,语雀的协作功能非常出色。可以邀请团队成员加入我的笔记工作空间,共同协作编辑和维护笔记和文档。通过语雀,可以很方便地进行团队内部的协作和交流,并对笔记进行版本控制,以避免多人编辑时出现不同步的问题。通过团队空间、知识库、目录的层级管理,能很好的将知识做分类。
2525 0
|
算法 程序员
拿捏汉诺塔问题(附有动图)
相传在古印度圣庙中,有一种被称为汉诺塔(Hanoi)的游戏。该游戏是在一块铜板装置上,有三根杆(编号A、B、C),在A杆自下而上、由大到小按顺序放置64个金盘。游戏的目标:把A杆上的金盘全部移到C杆上,并仍保持原有顺序叠好。操作规则:每次只能移动一个盘子,并且在移动过程中三根杆上都始终保持大盘在下,小盘在上,操作过程中盘子可以置于A、B、C任一杆上。
1454 0
拿捏汉诺塔问题(附有动图)
|
8天前
|
域名解析 人工智能 Linux
|
18天前
|
Linux Go 开发工具
手机上跑 DeepSeek 智能体:开源安卓客户端 DSH Go 安装与使用指南
DSH Go 是一款开源安卓客户端,将 DeepSeek Harness 智能体引擎装进手机:免 Root、免 Termux,一键安装即用;支持本机运行(内置 Ubuntu+Node.js)或远程接管电脑会话;具备插话打断、过程折叠、屏幕操控、端到端加密分享等特色功能,真正实现智能体“口袋化”。
16801 62
|
16天前
|
人工智能
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
进不去 Antigravity 或 Gemini,提示所在地区不可用?三步查清 Google 账号关联地区、官方更正表单理由选法与改区注意事项。
Google 提示「所在地区不可用」?进不去 Antigravity 或 Gemini,先查账号这个设置
|
2天前
|
人工智能 安全 网络安全
|
5天前
|
云安全 数据采集 人工智能
|
6天前
|
Linux API iOS开发
【DSH】DeepSeek Harness新手保姆教程(下载+安装+配置+使用,2026最新实测)
DeepSeek Harness(dsh)是 DeepSeek AI 开源的智能体框架,秉持“一切皆插件”理念,支持读文件、改代码、执行命令、处理表格等自动化任务;免费开源,兼容 Win/macOS,需配 API 密钥使用。(239字)
1206 2