Flow Problem
Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 21438    Accepted Submission(s): 10081
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

C/C++:

 #include <map>
#include <queue>
#include <cmath>
#include <vector>
#include <string>
#include <cstdio>
#include <cstring>
#include <climits>
#include <iostream>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
const int my_max = ; int N, M, my_map[my_max][my_max], my_source, my_sink
, my_dis[my_max]; int my_dfs(int my_step, int my_ans)
{
if (my_step == my_sink) return my_ans; int my_temp = my_ans;
for (int i = ; i <= N && my_ans; ++ i)
{
if (my_dis[my_step] == my_dis[i] + && my_map[my_step][i])
{
int t = my_dfs(i, min(my_ans, my_map[my_step][i]));
my_map[my_step][i] -= t;
my_map[i][my_step] += t;
my_ans -= t;
}
}
return my_temp - my_ans;
} bool my_bfs()
{
memset(my_dis, -, sizeof(my_dis));
queue <int> Q;
my_dis[my_sink] = ;
Q.push(my_sink);
while (!Q.empty())
{
int now = Q.front();
for (int i = ; i <= N; ++ i)
{
if (my_map[i][now] > && my_dis[i] == -)
{
my_dis[i] = my_dis[now] + ;
Q.push(i);
}
}
if (now == my_source) return true;
Q.pop();
}
return false;
} int my_dinic()
{
int my_ans = ;
while (my_bfs())
my_ans += my_dfs(my_source, INF); return my_ans;
} int main()
{
int t;
scanf("%d", &t);
for (int i = ; i <= t; ++ i)
{
memset(my_map, , sizeof(my_map));
scanf("%d%d", &N, &M);
my_source = , my_sink = N; while (M --)
{
int x, y, x_y;
scanf("%d%d%d", &x, &y, &x_y);
my_map[x][y] += x_y;
}
printf("Case %d: %d\n", i, my_dinic());
}
return ;
}

hdu 3549 Flow Problem (Dinic)的更多相关文章

  1. HDU 3549 Flow Problem (dinic模版 && isap模版)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3549 题意: 给你一个有向图,问你1到n的最大流. dinic模版 (n*n*m) #include ...

  2. HDU 3549 Flow Problem(最大流)

    HDU 3549 Flow Problem(最大流) Time Limit: 5000/5000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/ ...

  3. 网络流 HDU 3549 Flow Problem

    网络流 HDU 3549 Flow Problem 题目:pid=3549">http://acm.hdu.edu.cn/showproblem.php?pid=3549 用增广路算法 ...

  4. hdu 3549 Flow Problem

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Description Network flow is a well- ...

  5. hdu 3549 Flow Problem【最大流增广路入门模板题】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...

  6. 网络流--最大流--HDU 3549 Flow Problem

    题目链接 Problem Description Network flow is a well-known difficult problem for ACMers. Given a graph, y ...

  7. hdu 3549 Flow Problem Edmonds_Karp算法求解最大流

    Flow Problem 题意:N个顶点M条边,(2 <= N <= 15, 0 <= M <= 1000)问从1到N的最大流量为多少? 分析:直接使用Edmonds_Karp ...

  8. HDU 3549 Flow Problem 网络流(最大流) FF EK

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others) Tot ...

  9. hdu 3549 Flow Problem (网络最大流)

    Flow Problem Time Limit: 5000/5000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Tota ...

随机推荐

  1. Orleans 知多少 | 3. Hello Orleans

    1. 引言 是的,Orleans v3.0.0 已经发布了,并已经完全支持 .NET Core 3.0. 所以,Orleans 系列是时候继续了,抱歉,让大家久等了. 万丈高楼平地起,这一节我们就先来 ...

  2. 巨杉Tech | SequoiaDB数据域及存储规划

    1 背景近年来,企业的各项业务发展迅猛,客户数目不断增加,后台服务系统压力也越来越大,系统的各项硬件资源也变得非常紧张.因此,在技术风险可控的基础上,希望引入大数据技术,利用大数据技术优化现有IT系统 ...

  3. kali更新源地址更改

    问题: Hit:1 http://mirrors.ustc.edu.cn/kali kali-rolling InReleaseIgn:2 http://mirrors.ustc.edu.cn/kal ...

  4. javascript获取坐标/滚动/宽高/距离

    坐标(鼠标/触摸) event.screenX 鼠标/触摸,相对于显示屏的X坐标 event.screenY 鼠标/触摸,相对于显示屏的Y坐标 event.clientX 鼠标/触摸,相对于浏览器视口 ...

  5. SpringData-Redis发布订阅自动重连分析

    SpringData-Redis发布订阅自动重连分析 RedisMessageListenerContainer 配置 @Bean @Autowired RedisMessageListenerCon ...

  6. django-模板之comment标签(六)

    index.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  7. .NET Core 3.1 编写混合 C++ 程序

    前言 随着 .NET Core 3.1 的第二个预览版本发布,微软正式将 C++/CLI 移植到 .NET Core 上,从此可以使用 C++ 编写 .NET Core 的程序了. 由于目前仅有 MS ...

  8. Tomcat项目部署

    一 之前一直是在ecplise 利用tomcat插件的形式启动项目,这里可以通过选择server.xml和context files两种方式这里选择这两者方式,都是会在tomcat/bin下产生对应的 ...

  9. find 小案例

    说明:前几天对生产环境的一些重要数据进行备份时用到了find,查找特定符合条件的文件名后拷贝至指定目录,但是只拷贝了部分匹配到的文件 小案例模拟还原: [root@centos- ~]# ll /te ...

  10. [知识图谱]Neo4j知识图谱构建(neo4j-python-pandas-py2neo-v3)

    neo4j-python-pandas-py2neo-v3 利用pandas将excel中数据抽取,以三元组形式加载到neo4j数据库中构建相关知识图谱 Neo4j知识图谱构建 1.运行环境: pyt ...