POJ 3204 Ikki's Story I - Road Reconstruction
|
Ikki's Story I - Road Reconstruction
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 N, M (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 a, b, c, which means that there is a road from city a to city b with a transportation capacity of c (0 ≤ a, b < n, c ≤ 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 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的更多相关文章
- POJ3204 Ikki's Story I - Road Reconstruction
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- POJ 3204 Ikki's Story I-Road Reconstruction (网络流关键边)
[题意]给定一个N个节点M条边的网络流,求有多少条边,使得当增其中加任何一个边的容量后,整个网络的流将增加. 挺好的一道题,考察对网络流和增广路的理解. [思路] 首先关键边一定是满流边.那么对于一个 ...
- POJ3184 Ikki's Story I - Road Reconstruction(最大流)
求一次最大流后,分别对所有满流的边的容量+1,然后看是否存在增广路. #include<cstdio> #include<cstring> #include<queue& ...
- poj 3204(最小割--关键割边)
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- 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 ...
- POJ 3207 Ikki's Story IV - Panda's Trick(2-sat)
POJ 3207 Ikki's Story IV - Panda's Trick id=3207" target="_blank" style=""& ...
- POJ3204 Ikki's Story - Road Reconstruction 网络流图的关键割边
题目大意:一个有源有汇的城市,问最少增加城市中的多少道路可以增加源到汇上各个路径上可容纳的总车流量增加. 网络流关键割边集合指如果该边的容量增加,整个网络流图中的任意从原点到汇点的路径的流量便可增加. ...
- 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 ...
- 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 ...
随机推荐
- Qt 中使用Singleton模式需小心
在qt中,使用Singleton模式时一定要小心.因为Singleton模式中使用的是静态对象,静态对象是直到程序结束才被释放的,然而,一旦把该静态对象纳入了Qt的父子对象体系,就会导致不明确的行为. ...
- Java实现约瑟夫环
什么是约瑟夫环呢? 约瑟夫环是一个数学的应用问题:已知n个人(以编号1,2,3...n分别表示)围坐在一张圆桌周围.从编号为k的人开始报数,数到m的那个人出列;他的下一个人又从1开始报数,数到m的那个 ...
- Dynamics CRM 2015-如何修改Optionset Default Value
在日常工作中,我们时不时会遇到在CRM测试环境上添加Optionset的时候,Default Value是某个值,但换到Production环境或者其他环境,添加的时候,Default Value可能 ...
- SqlServer表结构查询
一.前言 近两天项目升级数据迁移,将老版本(sqlserver)的数据迁移到新版本(mysql)数据库,需要整理一个Excel表格出来,映射两个库之间的表格字段,示例如下: Mysql数据库查询表结构 ...
- 优化SQLServer--表和索引的分区(二)
简介 之前一篇简单的介绍了语法和一些基本的概念,隔了一段时间,觉得有必要细致的通过实例来总结一下这部分内容.如之前所说,分区就是讲大型的对象(表)分成更小的块来管理,基本单位是行.这也就产生了很大优势 ...
- postgres创建表的过程以及部分源码分析
背景:修改pg内核,在创建表时,表名不能和当前的用户名同名. 首先我们知道DefineRelation此函数是最终创建表结构的函数,最主要的参数是CreateStmt这个结构,该结构如下 typede ...
- VS 母版使用配置技巧
采用web.config配置母版,方便母版的变更,处理方法: 1.在web.config配置如下内容: <configuration> <system.web> <pag ...
- 为 suse linux 设置程序自动启动
1.suse linux 程序自动启动 在部署面安装的的时候,重启之后需要去tomcat/bin/startup.sh 下面去执行启动脚本. 设置开机自动启动该服务 在 vim /etc/i ...
- APUE学习之出错处理
当UNIX函数发生错误时,通常会返回一个负值,而且整形变量errno通常被设置为具有特定信息的值. errno是全局变量,仅当函数出错才有被改变.对待errno,应注意两条规则 ...
- 简单socket()编程
客户端: 1.socket( int af, int type, int protocol) socket()函数用于根据指定的地址族.数据类型和协议来分配一个套接口的描述字及其所用的资源.如果协议p ...