POJ 3204 Ikki's Story I - Road Reconstruction

2023-06-05,,

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

POJ Monthly--2007.03.04, Ikki

[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的相关教程结束。

《POJ 3204 Ikki's Story I - Road Reconstruction.doc》

下载本文的Word格式文档,以方便收藏与打印。