Ikki's Story I - Road Reconstruction
Time Limit: 2000MS   Memory Limit: 131072K
Total Submissions: 7659   Accepted: 2215

Description

Ikki is the king of a small country – Phoenix, Phoenix is so small that there is only one city that is responsible for the production of daily goods, and uses the road network to transport the goods to the capital. Ikki finds that the biggest problem in the country is that transportation speed is too slow.

Since Ikki was an ACM/ICPC contestant before, he realized that this, indeed, is a maximum flow problem. He coded a maximum flow program and found the answer. Not satisfied with the current status of the transportation speed, he wants to increase the transportation ability of the nation. The method is relatively simple, Ikki will reconstruct some roads in this transportation network, to make those roads afford higher capacity in transportation. But unfortunately, the country of Phoenix is not so rich in GDP that there is only enough money to rebuild one road. Ikki wants to find such roads that if reconstructed, the total capacity of transportation will increase.

He thought this problem for a loooong time but cannot get it. So he gave this problem to frkstyc, who put it in this POJ Monthly contest for you to solve. Can you solve it for Ikki?

Input

The input contains exactly one test case.

The first line of the test case contains two integers NM (N ≤ 500, M ≤ 5,000) which represents the number of cities and roads in the country, Phoenix, respectively.

M lines follow, each line contains three integers abc, which means that there is a road from city a to city b with a transportation capacity of c (0 ≤ ab < nc ≤ 100). All the roads are directed.

Cities are numbered from 0 to n − 1, the city which can product goods is numbered 0, and the capital is numbered n − 1.

Output

You should output one line consisting of only one integer K, denoting that there are K roads, reconstructing each of which will increase the network transportation capacity.

Sample Input

2 1
0 1 1

Sample Output

1

Source

[Submit]   [Go Back]   [Status]   [Discuss]

最小割的必须边。

 #include <cstdio>
#include <cstring> #define fread_siz 1024 inline int get_c(void)
{
static char buf[fread_siz];
static char *head = buf + fread_siz;
static char *tail = buf + fread_siz; if (head == tail)
fread(head = buf, , fread_siz, stdin); return *head++;
} inline int get_i(void)
{
register int ret = ;
register int neg = false;
register int bit = get_c(); for (; bit < ; bit = get_c())
if (bit == '-')neg ^= true; for (; bit > ; bit = get_c())
ret = ret * + bit - ; return neg ? -ret : ret;
} inline int min(int a, int b)
{
return a < b ? a : b;
} const int inf = 2e9;
const int maxn = ; int n, m;
int s, t;
int edges;
int hd[maxn];
int to[maxn];
int fl[maxn];
int nt[maxn]; inline void add(int u, int v, int f)
{
nt[edges] = hd[u]; to[edges] = v; fl[edges] = f; hd[u] = edges++;
nt[edges] = hd[v]; to[edges] = u; fl[edges] = ; hd[v] = edges++;
} int dep[maxn]; inline bool bfs(void)
{
static int que[maxn];
static int head, tail; memset(dep, , sizeof(dep));
head = , tail = ;
que[tail++] = s;
dep[s] = ; while (head != tail)
{
int u = que[head++], v;
for (int i = hd[u]; ~i; i = nt[i])
if (!dep[v = to[i]] && fl[i])
dep[v] = dep[u] + , que[tail++] = v;
} return dep[t];
} int dfs(int u, int f)
{
if (u == t || !f)
return f; int used = , flow; for (int i = hd[u], v; ~i; i = nt[i])
if (dep[v = to[i]] == dep[u] + && fl[i])
{
flow = dfs(v, min(f - used, fl[i])); used += flow;
fl[i] -= flow;
fl[i^] += flow; if (used == f)
return f;
} if (!used)
dep[u] = ; return used;
} inline bool dfs(void)
{
return dfs(s, inf) != ;
} inline void maxFlow(void)
{
while (bfs())
while (dfs());
} int dfn[maxn];
int scc[maxn]; void tarjan(int u)
{
static int low[maxn];
static int stk[maxn];
static int top, cnt, tim; stk[++top] = u;
dfn[u] = low[u] = ++tim; for (int i = hd[u]; ~i; i = nt[i])if (fl[i])
{
if (!dfn[to[i]])
tarjan(to[i]), low[u] = min(low[u], low[to[i]]);
else if (!scc[to[i]])
low[u] = min(low[u], dfn[to[i]]);
} if (low[u] == dfn[u])
{
++cnt; int t;
do
scc[t = stk[top--]] = cnt;
while (t != u);
}
} inline void tarjan(void)
{
for (int i = ; i < n; ++i)
if (!dfn[i])tarjan(i);
} inline void solve(void)
{
int ans = ; for (int i = ; i < edges; i += )if (!fl[i])
{
int u = to[i^], v = to[i];
if (scc[u] == scc[s])
if (scc[v] == scc[t])
++ans;
} printf("%d\n", ans);
} signed main(void)
{
n = get_i();
m = get_i(); memset(hd, -, sizeof(hd)); for (int i = ; i <= m; ++i)
{
int u = get_i();
int v = get_i();
int f = get_i(); add(u, v, f);
} s = , t = n - ; maxFlow(); tarjan(); solve();
}

@Author: YouSiki

POJ 3204 Ikki's Story I - Road Reconstruction的更多相关文章

  1. POJ3204 Ikki's Story I - Road Reconstruction

    Ikki's Story I - Road Reconstruction Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 7 ...

  2. POJ 3204 Ikki's Story I-Road Reconstruction (网络流关键边)

    [题意]给定一个N个节点M条边的网络流,求有多少条边,使得当增其中加任何一个边的容量后,整个网络的流将增加. 挺好的一道题,考察对网络流和增广路的理解. [思路] 首先关键边一定是满流边.那么对于一个 ...

  3. POJ3184 Ikki's Story I - Road Reconstruction(最大流)

    求一次最大流后,分别对所有满流的边的容量+1,然后看是否存在增广路. #include<cstdio> #include<cstring> #include<queue& ...

  4. poj 3204(最小割--关键割边)

    Ikki's Story I - Road Reconstruction Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 7 ...

  5. POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题)

    POJ 3207 Ikki's Story IV - Panda's Trick(2-sat问题) Description liympanda, one of Ikki's friend, likes ...

  6. POJ 3207 Ikki&#39;s Story IV - Panda&#39;s Trick(2-sat)

    POJ 3207 Ikki's Story IV - Panda's Trick id=3207" target="_blank" style=""& ...

  7. POJ3204 Ikki's Story - Road Reconstruction 网络流图的关键割边

    题目大意:一个有源有汇的城市,问最少增加城市中的多少道路可以增加源到汇上各个路径上可容纳的总车流量增加. 网络流关键割边集合指如果该边的容量增加,整个网络流图中的任意从原点到汇点的路径的流量便可增加. ...

  8. POJ 3207 Ikki's Story IV - Panda's Trick

    Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 7296   ...

  9. poj 3207 Ikki's Story IV - Panda's Trick (2-SAT)

    http://poj.org/problem?id=3207 Ikki's Story IV - Panda's Trick Time Limit: 1000MS   Memory Limit: 13 ...

随机推荐

  1. java web学习总结(二十九) -------------------JavaBean的两种开发模式

    SUN公司推出JSP技术后,同时也推荐了两种web应用程序的开发模式,一种是JSP+JavaBean模式,一种是Servlet+JSP+JavaBean模式. 一.JSP+JavaBean开发模式 1 ...

  2. SharePoint2016母版页的直接依赖项的数目限制超过10的限制解决方案Direct Dependencies Limit with Master page User Controls

    前言 最近母版制作站点模板,根据站点模板新建后发现如下错误: 解决方案 1. 打开C:\inetpub\wwwroot\wss\VirtualDirectories\,如下图: 2. 打开web.co ...

  3. Android Weekly Notes Issue #229

    Android Weekly Issue #229 October 30th, 2016 Android Weekly Issue #229 Android Weekly笔记, 本期内容包括: 性能库 ...

  4. 浅谈DrawerLayout(抽屉效果)

    DrawerLayout是V4包下提供的一种左滑右滑抽屉布局效果. 实现效果如下: 因为是官方提供的,所以使用起来也相对的比较简单. DrawerLayout 提供 1.当界面弹出的时候,主要内容区会 ...

  5. Hibernate 系列 06 - 对象在JVM中的生命周期

    引导目录: Hibernate 系列教程 目录 Java对象通过new命令进行创建,Java虚拟机(Java Virtual Machine,JVM)会为新的Java对象在内存中开辟一个新空间以存放次 ...

  6. ZooKeeper:数据模型

    ZooKeeper数据模型 ZNode ZNode 分类 Stat Watcher Watcher工作原理 Watcher事件说明 Watcher注册 事件发布 示例 ZooKeeper 数据模型 整 ...

  7. Spark编译与打包

    编译打包 Spark支持Maven与SBT两种编译工具,这里使用了Maven进行编译打包: 在执行make-distribution脚本时它会检查本地是否已经存在Maven还有当前Spark所依赖的S ...

  8. WinXP/Win7/Win8本地用户配置文件迁移至域用户

    一.概述 最近在进行加域的工作,PC大部分是Win7,使用过微软USMT 4.0和5.0工具进行迁移,但命令行报错,目前还没找到好的文章研究. 本文迁移方法很特殊,利用的是Windows默认配置文件. ...

  9. 火狐浏览器URL中传中文参数乱码问题

    火狐浏览器:前端页面传中文 <span data-bind=" check_action:'roleMenuPriv'"> <a data-bind=" ...

  10. 又见SpringMVC

    一.如何让一个普通类成为Controller? 方案一:实现接口Controller解析:handleRequest(request,response) 方案二:继承AbstractControlle ...