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
 #include<stdio.h>
#include<string>
#include<string.h>
#include<vector>
#include<queue>
using namespace std;
const int maxn = 1e5 + ;
const int INF = ;
struct node {
int from, to, cap, flow;
};
struct Dinic {
int n, m, s, t;
vector<node>nodes;
vector<int>g[maxn];
int vis[maxn];
int d[maxn];
int cur[maxn];
void clearall(int n) {
for (int i = ; i < n ; i++) g[i].clear();
nodes.clear();
}
void clearflow() {
int len = nodes.size();
for (int i = ; i < len ; i++) nodes[i].flow = ;
}
void add(int from, int to, int cap) {
nodes.push_back((node) {
from, to, cap,
});
nodes.push_back((node) {
to, from, ,
});
m = nodes.size();
g[from].push_back(m - );
g[to].push_back(m - );
}
bool bfs() {
memset(vis, , sizeof(vis));
queue<int>q;
q.push(s);
d[s] = ;
vis[s] = ;
while(!q.empty()) {
int x = q.front();
q.pop();
int len = g[x].size();
for (int i = ; i < len ; i++) {
node &e = nodes[g[x][i]];
if (!vis[e.to] && e.cap > e.flow ) {
vis[e.to] = ;
d[e.to] = d[x] + ;
q.push(e.to);
}
}
}
return vis[t];
}
int dfs(int x, int a) {
if (x == t || a == ) return a;
int flow = , f, len = g[x].size();
for (int &i = cur[x] ; i < len ; i++) {
node & e = nodes[g[x][i]];
if (d[x] + == d[e.to] && (f = dfs(e.to, min(a, e.cap - e.flow))) > ) {
e.flow += f;
nodes[g[x][i] ^ ].flow -= f;
flow += f;
a -= f;
if (a == ) break;
}
}
return flow;
}
int maxflow(int a, int b) {
s = a;
t = b;
int flow = ;
while(bfs()) {
memset(cur, , sizeof(cur));
flow += dfs(s, INF);
}
return flow;
}
vector<int>mincut() {
vector<int>ans;
int len = nodes.size();
for (int i = ; i < len ; i++) {
node & e = nodes[i];
if ( vis[e.from] && !vis[e.to] && e.cap > ) ans.push_back(i);
}
return ans;
}
void reduce() {
int len = nodes.size();
for (int i = ; i < len ; i++) nodes[i].cap -= nodes[i].flow;
}
} f;
int main() {
int n, m;
while(~scanf("%d%d", &m, &n)) {
f.clearall(n);
f.clearflow();
for (int i = ; i < m ; i++) {
int u, v, c;
scanf("%d%d%d", &u, &v, &c);
f.add(u, v, c);
}
printf("%d\n", f.maxflow(, n));
}
return ;
}

最新文章

  1. C# Just want 入门
  2. Eclipse中10个最有用的快捷键组合
  3. opencv 简单模糊和高斯模糊 cvSmooth
  4. 在Web.config或App.config中的添加自定义配置
  5. 图解SSL/TLS协议
  6. ubuntu12.04 修复Grub2
  7. 【Android 界面效果45】ViewPager源码分析
  8. [Effective C++ --031]将文件间的编译依存关系降至最低
  9. TestNG 与 Junit的比较(转)
  10. 微信小程序使用场景及取名“潜”规则
  11. 国外支付PayPal
  12. OpenTSDB-Querying or Reading Data
  13. [51nod1329]路径游戏
  14. 在Spring Boot框架下使用WebSocket实现聊天功能
  15. ROS_Kinetic_22 使用ROS的qt插件即ros_qtc_plugin实现Hi ROS!!!!
  16. luogu P1723 高手过愚人节
  17. PHP笔记:单引号与双引号区别
  18. 使用vue+koa实现一个简单的图书小程序(1)
  19. win10企业版激活
  20. shiro 密码的MD5盐值加密

热门文章

  1. vue-axios基本用法
  2. 如何用Word编辑参考文献------这是引用一位大师的
  3. Maven+SSM框架搭建【spring+springmvc+mybatis】
  4. 第1次作业:我与我的IT梦
  5. C语言第十一次博客作业---函数嵌套调用
  6. 高级软件工程第四次作业(C++)
  7. 开启Linux的share
  8. 限定 edittext 的 输入内容
  9. 日志 --BUG记录
  10. HNOI 2012 永无乡