HDU 3549 Flow Problem(最大流)

Time Limit: 5000/5000 MS (Java/Others)

Memory Limit: 65536/32768 K (Java/Others)

【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.

网络流是ACMers众所周知的一个难题。给定一幅图,你的任务是找出这个加权有向图的最大流。

【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)

输入的第一行是一个整数T,表示测试用例的数量。

对于每个测试用例,第一行有两个整数N和M,表示图中顶点与边的数量。(2 <= N <= 15, 0 <= M <= 1000)接着M行,每行三个整数X,Y和C,有一条从X到Y的边,其容量为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.

对于每个测试样例,输出从源点1到汇点N的最大流。

【Sample Input - 输入样例】

【Sample Output - 输出样例】

2

3 2

1 2 1

2 3 1

3 3

1 2 1

2 3 1

1 3 1

Case 1: 1

Case 2: 2

【题解】

最大流问题

题目描述得太简单,自己从来没做过,就去百度了一下大概的规则

然后……就当成水管来看吧…………

比较简单的方式的用DFS实现,注意处理好搜索与回溯即可。

搜索时可以每次只找一条1→N的路径,也可以在当前已走过的路径下尽可能地推送流量到N,即多条路径(不过这种方式比较费时,因为会在重复尝试一些无效的路径)

为了方便回溯,每次A→B推送的流量等于添加B→A的容量。

vector的查询速度并不是很快,多次使用的时候用指针或是继承可以提高速度,以及看/写起来舒服点……

【代码 C++】

 #include<cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
int n;
struct edge{
int next, capacity, last_i;
};
std::vector<edge> data[];
bool us[];
void read(){
int m, x, y, c, i, j, map[][];
memset(map, , sizeof(map));
scanf("%d%d", &n, &m);
for (i = ; i <= n; ++i) data[i].clear();
for (i = ; i < m; ++i) scanf("%d%d%d", &x, &y, &c), map[x][y] += c;
edge temp = { , , };
for (i = ; i < n; ++i){
for (j = i + ; j <= n; ++j){
if (map[i][j] + map[j][i] == ) continue;
temp.next = j; temp.capacity = map[i][j]; temp.last_i = data[j].size();
data[i].push_back(temp);
temp.next = i; temp.capacity = map[j][i]; temp.last_i = data[i].size() - ;
data[j].push_back(temp);
}
}
}
int send(int nowEdge, int flowWait){
if (nowEdge == n) return flowWait;
us[nowEdge] = ;
int hadSend, temp, i, ed = data[nowEdge].size();
for (i = hadSend = ; i < ed; ++i){
edge &now = data[nowEdge][i];
if (!us[now.next] && now.capacity){
temp = send(now.next, std::min(now.capacity, flowWait - hadSend));
if (temp){
hadSend += temp; now.capacity -= temp;
data[now.next][now.last_i].capacity += temp;
}
}
}
return hadSend;
}
int main(){
int t, i, opt, temp;
for (i = scanf("%d", &t); i <= t; ++i){
printf("Case %d: ", i);
read();
for (opt = ; memset(us, , sizeof(us));){
temp = send(, );
if (temp) opt += temp;
else break;
}
printf("%d\n", opt);
}
return ;
}

HDU 3549 Flow Problem(最大流)的更多相关文章

  1. hdu - 3549 Flow Problem (最大流模板题)

    http://acm.hdu.edu.cn/showproblem.php?pid=3549 Ford-Fulkerson算法. #include <iostream> #include ...

  2. hdu 3549 Flow Problem (最大流)

    裸最大流,做模板用 m条边,n个点,求最大流 #include <iostream> #include <cstdio> #include <cstring> #i ...

  3. hdu 3549 Flow Problem 最大流 Dinic

    题目链接 题意 裸的最大流. 学习参考 http://www.cnblogs.com/SYCstudio/p/7260613.html Code #include <bits/stdc++.h& ...

  4. 网络流 HDU 3549 Flow Problem

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

  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

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

  7. HDU 3549 Flow Problem (最大流ISAP)

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

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

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

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

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

随机推荐

  1. ss 公共代理的必要设置(转)

    转: https://gist.github.com/fqrouter/95c037f9a3ba196fd4dd 本文只关注于确保ss服务还“活着”,如果你希望让其跑得更快,请参考 https://g ...

  2. Using Controls in a Form Design [AX 2012]

    Using Controls in a Form Design [AX 2012] This topic has not yet been rated - Rate this topic Update ...

  3. 8、web入门回顾/ Http

    1 web入门回顾 web入门 1)web服务软件作用: 把本地资源共享给外部访问 2)tomcat服务器基本操作      : 启动:  %tomcat%/bin/startup.bat 关闭: % ...

  4. 4、BOM编程/正则表达式

    1.    BOM编程 1.1. BOM编程基础 全称 Browser Object Model,浏览器对象模型. JavaScript是由浏览器中内置的javascript脚本解释器程序来执行jav ...

  5. Oracle性能优化--AUTOTRACE 操作

    AUTOTRACE是一个SQL*Plus工具,用于跟踪SQL的执行计划,收集执行时所耗用资源的统计信息,是SQL优化工具之一,下面给出启用 AUTOTRACE 功能步骤. 一 .启用AUTOTRACE ...

  6. C++中的类型重定义

    发现重复定义是由于从两个不同的路径包含了同一个头文件而引起的,同事也建议从另外一个路径打开工程试试, 这才慢慢发现了原因.这个原因可能有些拗口,而事实上要出现这种错误也有些"曲折" ...

  7. Android网络连接之HttpURLConnection和HttpClient

    1.概念   HTTP 协议可能是现在 Internet 上使用得最多.最重要的协议了,越来越多的 Java 应用程序需要直接通过 HTTP 协议来访问网络资源.在 JDK 的 java.net 包中 ...

  8. Android 自定义ScrollView ListView 体验各种纵向滑动的需求

      分类: [android 进阶之路]2014-08-31 12:59 6190人阅读 评论(10) 收藏 举报 Android自定义ScrollView纵向拖动     转载请标明出处:http: ...

  9. 20150612_Andriod contextual action mode 菜单

    参考地址:http://www.xuebuyuan.com/1114028.html              http://www.cnblogs.com/mengdd/p/3564782.html ...

  10. AreYouBusy

    AreYouBusy Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Su ...