解题报告 之 SOJ3353 Total Flow

解题报告 之 SOJ3353 Total Flow

Description

Time Limit: 2000 MS Memory Limit: 65536 K


The Problem

PROBLEM NAME: flow

Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an apparently haphazard
way. He wants to calculate the flow through the pipes.

Two pipes connected in a row allow water flow that is the minimum of the values of the two pipe‘s flow values. The example of a pipe with flow capacity 5 connecting to a pipe of flow capacity 3 can be reduced logically to a single pipe of flow capacity 3:

+---5---+---3---+ -> +---3---+

Similarly, pipes in parallel let through water that is the sum of their flow capacities:

+---5---+

---+       +--- -> +---8---+

+---3---+

Finally, a pipe that connects to nothing else can be removed; it contributes no flow to the final overall capacity:

+---5---+

---+             -> +---3---+

+---3---+--

All the pipes in the many mazes of plumbing can be reduced using these ideas into a single total flow capacity.

Given a map of the pipes, determine the flow capacity between the well (A) and the barn (Z).

Consider this example where node names are labeled with letters:

+-----------6-----------+

A+---3---+B                      +Z

+---3---+---5---+---4---+

C        D

Pipe BC and CD can be combined:

+-----------6-----------+

A+---3---+B                      +Z

+-----3-----+-----4-----+

D

Then BD and DZ can be combined:

+-----------6-----------+

A+---3---+B                      +Z

+-----------3-----------+

Then two legs of BZ can be combined:

B

A+---3---+---9---+Z

Then AB and BZ can be combined to yield a net capacity of 3:

A+---3---+Z

Write a program to read in a set of pipes described as two endpoints and then calculate the net flow capacity from ‘A‘ to ‘Z‘. All networks in the test data can be reduced using the rules here.

Pipe i connects two different nodes a_i and b_i (a_i in range ‘A-Za-z‘; b_i in range ‘A-Za-z‘) and has flow F_i (1 <= F_i <= 1,000). Note that lower- and upper-case node names are intended to be treated as different.

The system will provide extra test case feedback for your first 50 submissions.

INPUT FORMAT:

* Line 1: A single integer: N

* Lines 2..N + 1: Line i+1 describes pipe i with two letters and an integer, all space-separated: a_i, b_i, and F_i

The input contains multiple test cases.

SAMPLE INPUT:

5

A B 3

B C 3

C D 5

D Z 4

B Z 6

OUTPUT FORMAT:

* Line 1: A single integer that the maximum flow from the well (‘A‘) to the barn (‘Z‘)

SAMPLE OUTPUT:

3

题目大意:同样是一道可耻的阅读理解,给你n个水管的连接方式和流量,‘A‘为水源,‘Z‘为牲口棚,问你水源到牲口棚的最大流量是多少?

分析:直接看样例发现是裸的最大流。字母的处理可以直接将就ASCII码来给节点编号(注意节点要留够),源点为‘A‘而汇点为‘Z‘。不存在任何难度就是一道模板题。

上代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;

const int MAXN = 210;
const int MAXM = 10000;
const int INF = 0x3f3f3f3f;

struct Edge
{
	int from, to, next, cap;
};

Edge edge[MAXM];
int level[MAXN];
int head[MAXN];
int src, des, cnt;

void addedge( int from, int to, int cap )
{
	edge[cnt].from = from;
	edge[cnt].to = to;
	edge[cnt].cap = cap;
	edge[cnt].next = head[from];
	head[from] = cnt++;

	swap( from, to );

	edge[cnt].from = from;
	edge[cnt].to = to;
	edge[cnt].cap = 0;
	edge[cnt].next = head[from];
	head[from] = cnt++;
}

int bfs()
{
	memset( level, -1, sizeof level );
	cnt = 0;
	queue<int> q;
	while (!q.empty())
		q.pop();
	level[src] = 0;
	q.push( src );

	while (!q.empty())
	{
		int u = q.front();
		q.pop();

		for (int i = head[u]; i != -1; i = edge[i].next)
		{
			int v = edge[i].to;
			if (edge[i].cap > 0 && level[v] == -1)
			{
				level[v] = level[u] + 1;
				q.push( v );
			}
		}
	}
	return level[des] != -1;
}

int dfs( int u, int f )
{
	if (u == des) return f;
	int tem;

	for (int i = head[u]; i != -1; i = edge[i].next)
	{
		int v = edge[i].to;
		if (edge[i].cap > 0 && level[v] == level[u] + 1)
		{
			tem = dfs( v, min( f, edge[i].cap ) );
			if (tem > 0)
			{
				edge[i].cap -= tem;
				edge[i^1].cap += tem;
				return tem;
			}
		}
	}
	level[u] = -1;
	return 0;
}

int Dinic()
{
	int ans = 0, tem;
	while (bfs())
	{
		while ((tem = dfs( src, INF )) > 0)
		{
			ans += tem;
		}
	}
	return ans;
}

int main()
{
	int n;
	src = 'A', des = 'Z';
	while (cin >> n)
	{
		memset( head, -1, sizeof head );
		cnt = 0;
		char a, b;
		int c;
		for (int i = 1; i <= n; i++)
		{
			cin >> a >> b >> c;
			addedge( a, b, c );
			//把节点数留够,直接用字符的ASCII码作为编号
		}

		cout << Dinic() << endl;
	}
	return 0;
}
时间: 2024-11-06 21:48:59

解题报告 之 SOJ3353 Total Flow的相关文章

解题报告 之 SGU326 Perspective

解题报告 之 SGU326 Perspective Description Breaking news! A Russian billionaire has bought a yet undisclosed NBA team. He's planning to invest huge effort and money into making that team the best. And in fact he's been very specific about the expected res

解题报告【pat-1076】

最近一直在忙项目都没时间好好总结写博客,说起来真实惭愧啊. 下面就把自己最近做的几题好好总结一下,主要记录一些注意点,以防以后遇到再犯. 1076. Forwards on Weibo (30) 时间限制 3000 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Weibo is known as the Chinese version of Twitter.  One user on Weibo may have many

leetCode解题报告5道题(十)

Disk Schedule Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2368    Accepted Submission(s): 333 Problem Description 有很多从磁盘读取数据的需求,包括顺序读取.随机读取.为了提高效率,需要人为安排磁盘读取.然而,在现实中,这种做法很复杂.我们考虑一个相对简单的场景.磁

洛谷 P2936 [USACO09JAN]全流Total Flow

题目描述 Farmer John always wants his cows to have enough water and thus has made a map of the N (1 <= N <= 700) water pipes on the farm that connect the well to the barn. He was surprised to find a wild mess of different size pipes connected in an appa

习题:codevs 1035 火车停留解题报告

本蒟蒻又来写解题报告了.这次的题目是codevs 1035 火车停留. 题目大意就是给m个火车的到达时间.停留时间和车载货物的价值,车站有n个车道,而火车停留一次车站就会从车载货物价值中获得1%的利润,让你来求一种安排方法,使得车站收益最大,并输出收益值. 蒟蒻的思路是这样的: 一眼看出:最大费用最大流(MCMF)显然cost:表示车站收益然后……以车站为点建立图求流?同一个车站可能经过好几辆火车……,貌似很麻烦……:那么以什么建图.连边,还有怎么连?貌似有点类似于方格取数2之中的拆点……:那么

LeetCode: Pascal&#39;s Triangle II 解题报告

Pascal's Triangle II Total Accepted: 19384 Total Submissions: 63446 My Submissions Question SolutionGiven an index k, return the kth row of the Pascal's triangle. For example, given k = 3,Return [1,3,3,1]. Note:Could you optimize your algorithm to us

pat解题报告【1074】

1074. Reversing Linked List (25) 时间限制 300 ms 内存限制 32000 kB 代码长度限制 16000 B 判题程序 Standard 作者 CHEN, Yue Given a constant K and a singly linked list L, you are supposed to reverse the links of every K elements on L.  For example, given L being 1→2→3→4→5→

leetCode解题报告5道题(十一)

题目一:Subsets Given a set of distinct integers, S, return all possible subsets. Note: Elements in a subset must be in non-descending order. The solution set must not contain duplicate subsets. For example, If S = [1,2,3], a solution is: [ [3], [1], [2]

ACM: Just a Hook 解题报告 -线段树

E - Just a Hook Time Limit:2000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Description In the game of DotA, Pudge’s meat hook is actually the most horrible thing for most of the heroes. The hook is made up of several consecutive met