列表

详情


NC24820. [USACO 2009 Jan S]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 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.

输入描述

* 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

输出描述

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

示例1

输入:

5 
A B 3 
B C 3 
C D 5 
D Z 4 
B Z 6 

输出:

3

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 4ms, 内存消耗: 376K, 提交时间: 2019-11-21 23:37:56

#include <bits/stdc++.h>
#pragma GCC optimize(2)
using namespace std;

typedef long long ll;
const static int INF = 0x3f3f3f3f;


struct edge{
	int to;
	int cap;
	int rev;//去向,容量,以及其反向边的编号 
};

vector<edge> G[110];//邻接表表示 
bool used[110];//访问标记 

inline void pushe(int from, int to, int cap)
{
	G[from].push_back((edge){to, cap, G[to].size()});
	G[to].push_back((edge){from, 0, G[from].size() - 1});
}

int dfs(int v, int t, int f)//寻找增广路 
{
	if(v == t) return f;
	used[v] = true;
	for(int i = 0; i < G[v].size(); i++)
	{
		edge &e = G[v][i];
		
		if(!used[e.to] and e.cap > 0)
		{
			int d = dfs(e.to, t, min(f, e.cap));
			if(d > 0)
			{
				e.cap -= d;
				G[e.to][e.rev].cap += d;
				return d;
			}
		}
	}
	return 0;
}

ll max_flow(int s, int t)
{
	ll flow = 0;
	for(; ;)
	{
		memset(used, 0, sizeof(used));
		int f = dfs(s, t, INF);
		if(f == 0) return flow;//找不到增广路了 
		flow += f;
	}
}

int main()
{
	
	std::ios::sync_with_stdio(false);
	int n, m, s, t, u, v, c;
	//cin >> n >> m >> s >> t;
	cin >> n;
	char a, b;
	while(n--)
	{
		cin >> a >> b >> c;
		pushe(a - 'A' + 1, b - 'A' + 1, c);
	}
	cout << max_flow(1, 26) << endl;
	return 0;
} 

C++(clang++ 11.0.1) 解法, 执行用时: 3ms, 内存消耗: 508K, 提交时间: 2023-07-17 10:25:17

#include<bits/stdc++.h>
using namespace std;

typedef struct {
    int v, w;
}ppi;
vector<ppi> edge[60];
int n, vis[60];

int dfs(int p = 0, int flow = 0x3f3f3f3f3f3f) {
    if(p == 25) return flow;
    vis[p] = 1;
    for(int i = 0; i < edge[p].size(); i++) {
        int c, to = edge[p][i].v, val = min(edge[p][i].w, flow);
        if(edge[p][i].w > 0 && !vis[edge[p][i].v] && (c = dfs(to,val)) != -1) {
            edge[p][i].w -= c;
            for(int j = 0; j < edge[to].size(); j++) {
                if(edge[to][j].v == p) edge[to][j].w +=c;
            }
            return c;
        }
    }
    return -1;
}

int ff() {
    int ans = 0, c;
    while((c = dfs()) != -1) {
        memset(vis,0,sizeof(vis));
        ans += c;
    }
    return ans;
}

int main() {
    cin >> n;
    for(int i = 0; i < n; i++) {
        int u, v, w;
        char s,t;
        cin >> s >> t >> w;
        u = s - 'A';
        v = t - 'A';
        edge[u].push_back({v,w});
        edge[v].push_back({u,0});
    }
    printf("%d\n", ff());
}

上一题