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. 隔天开启tomcat spring报错

    严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderLis ...

  2. java入门笔记001--java环境搭建

    1. 常见dos命令 •dir : 列出当前目录下的文件以及文件夹 •md : 创建目录 •rd : 删除目录 •cd : 进入指定目录 •cd.. : 退回到上一级目录 •cd\: 退回到根目录 • ...

  3. atitit.日期,星期,时候的显示方法ISO 8601标准

    atitit.日期,星期,时候的显示方法ISO 8601标准 1. ISO 86011 2. DAte日期的显示1 2.1. Normal1 2.2. 顺序日期表示法(可以将一年内的天数直接表示)1 ...

  4. python语言中的编码问题

    在编程的过程当中,常常会遇到莫名其妙的乱码问题.很多人选择出了问题直接在网上找答案,把别人的例子照搬过来,这是快速解决问题的一个好办法.然而,作为一个严谨求实的开发者,如果不从源头上彻底理解乱码产生的 ...

  5. 30分钟让网站支持HTTPS

    对于一个良好和安全的网络——并且也为了更快的性能,新的API网络例如Service Workers,更佳的搜索排名,还有——在你的网站上使用HTTPS是关键.这里我会指导大家如何轻松搞定. 我不是安全 ...

  6. ViewPager轮播图

    LoopViewPagerLayout无限轮播 项目地址:https://github.com/why168/LoopViewPagerLayout 支持三种动画: 支持修改轮播的速度: 支持修改滑动 ...

  7. TFFS格式化到创建成功过程

    True FFS内核编程 1.格式化FLASH 即使FLASH没有和块设备驱动绑定,也可对其进行格式化. tffsDevFormat (int tffsDriveNo, int formatArg); ...

  8. IOS 网络浅析-(十二 UIWebView简介)

    在这篇随笔里,我们只要知道UIWebView是什么就可以了. UIWebView 是苹果提供的用来展示网页的UI控件,它也是最占内存的控件. iOS8.0之后出现了webkit框架,WKWebView ...

  9. zend studio 快捷键

    复制当前行:ctrl+alt+↓ 删除当前行:Ctrl+d 行注释:Ctrl+/ 快注释(先选中要注释的代码):Ctrl+shift+/ 提示助手(方便函数等补全):alt+/ 代码格式化:Ctrl+ ...

  10. 让你少走弯路的搭建树莓派的Net与NodeJS运行环境

      树莓派是当前最火的嵌入计算平台没有之一,树莓派可以给我们无数的想象,树莓派的高性能.低功耗.低成本.可扩展性(最新的树莓派原生支持WIFI和蓝牙,这功能太赞了)深受大家的喜爱.虽然树莓派到目前为止 ...