码农知识堂 - 1000bd
  •   Python
  •   PHP
  •   JS/TS
  •   JAVA
  •   C/C++
  •   C#
  •   GO
  •   Kotlin
  •   Swift
  • hdu 3549 Flow Problem 网络流


    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549

    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.

    题意:不能再简单了这题意,很裸的告诉你根据输入来求解最大流。

    解法:网络流算法求解最大流,模板题,Dinic。

     1 #include
     2 #include
     3 #include
     4 #include
     5 #include
     6 #include
     7 #include
     8 #define inf 0x7fffffff
     9 using namespace std;
    10 const int maxn=16;
    11 
    12 int n,m;
    13 int graph[maxn][maxn],d[maxn];
    14 
    15 int bfs()
    16 {
    17     memset(d,0,sizeof(d));
    18     d[1]=1;
    19     queue Q;
    20     Q.push(1);
    21     while (!Q.empty())
    22     {
    23         int u=Q.front() ;Q.pop() ;
    24         for (int v=1 ;v<=n ;v++)
    25         {
    26             if (!d[v] && graph[u][v]>0)
    27             {
    28                 d[v]=d[u]+1;
    29                 Q.push(v);
    30                 if (v==n) return 1;
    31             }
    32         }
    33     }
    34     return 0;
    35 }
    36 
    37 int dfs(int u,int flow)
    38 {
    39     if (u==n || flow==0) return flow;
    40     int cap=flow;
    41     for (int v=1 ;v<=n ;v++)
    42     {
    43         if (d[v]==d[u]+1 && graph[u][v]>0)
    44         {
    45             int x=dfs(v,min(cap,graph[u][v]));
    46             cap -= x;
    47             graph[u][v] -= x;
    48             graph[v][u] += x;
    49             if (cap==0) return flow;
    50         }
    51     }
    52     return flow-cap;
    53 }
    54 
    55 int Dinic()
    56 {
    57     int sum=0;
    58     while (bfs()) sum += dfs(1,inf);
    59     return sum;
    60 }
    61 
    62 int main()
    63 {
    64     int t,ncase=1;
    65     scanf("%d",&t);
    66     while (t--)
    67     {
    68         scanf("%d%d",&n,&m);
    69         int a,b,c;
    70         memset(graph,0,sizeof(graph));
    71         for (int i=0 ;i 
    

  • 相关阅读:
    elasticsearch
    Python数据结构:数字与字符串
    【*E】leetcode-069.x的平方根
    【python】路径管理+路径拼接问题
    dubbo学习之本地存根实践
    区块链技术与应用 【全国职业院校技能大赛国赛题目解析】第五套智能合约安全漏洞测试
    git实用技巧:把我的文件内容改回去
    北斗智能终端:助力森林保护的新利器
    消息队列实现进程之间通信方式代码,现象
    ResourceLoader接口解读
  • 原文地址:https://blog.csdn.net/G11176593/article/details/127855252
  • 最新文章
  • 沪漂五周年了:我越来越迷茫了
    Agentic Skill Routing 实战:别再把所有 Skill 塞进 AI Agent 上下文
    MySQL-Seconds_behind_master的精度误差
    [MAF预定义ChatClient中间件-03]CachingChatClient——利用缓存省钱省时间
    AI的至暗历史:从万众期待到被政府撤资,AI的两次死亡徘徊
    Agent OS :五种驯服不确定性的范式
    PortSwigger SQL注入LAB11
    数据库即时编译JIT
    [Begin]AI Learn Data Day 0
    深度学习进阶(二十七)现代 LLM 的核心架构设计其二:SwiGLU
  • 热门文章
  • 十款代码表白小特效 一个比一个浪漫 赶紧收藏起来吧!!!
    奉劝各位学弟学妹们,该打造你的技术影响力了!
    五年了,我在 CSDN 的两个一百万。
    Java俄罗斯方块,老程序员花了一个周末,连接中学年代!
    面试官都震惊,你这网络基础可以啊!
    你真的会用百度吗?我不信 — 那些不为人知的搜索引擎语法
    心情不好的时候,用 Python 画棵樱花树送给自己吧
    通宵一晚做出来的一款类似CS的第一人称射击游戏Demo!原来做游戏也不是很难,连憨憨学妹都学会了!
    13 万字 C 语言从入门到精通保姆级教程2021 年版
    10行代码集2000张美女图,Python爬虫120例,再上征途
小工具 小游戏
Copyright © 2022 侵权请联系2656653265@qq.com    京ICP备2022015340号-1

京公网安备 11010502049817号