洛谷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 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.

约翰总希望他的奶牛有足够的水喝,因此他找来了农场的水管地图,想算算牛棚得到的水的 总流量.农场里一共有N根水管.约翰发现水管网络混乱不堪,他试图对其进行简 化.他简化的方式是这样的:

两根水管串联,则可以用较小流量的那根水管代替总流量.

两根水管并联,则可以用流量为两根水管流量和的一根水管代替它们

当然,如果存在一根水管一端什么也没有连接,可以将它移除.

请写个程序算出从水井A到牛棚Z的总流量.数据保证所有输入的水管网络都可以用上述方法 简化.

输入输出格式

输入格式:

  • 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 
输出样例#1: 
3 

题目大意是给一些路径,求从‘A'到’Z'的最大流。

注意,最好不用 map ,直接减去‘A'即可。
附代码:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#define MAXN 710
#define MAX 999999999
using namespace std;
int n,m,s,t,c=2,d=1;
int head[MAXN],deep[MAXN];
struct node{
	int next,to,w;
}a[MAXN<<1];
inline int read(){
	int date=0,w=1;char c=0;
	while(c<='0'||c>'9'){if(c=='-')w=-1;c=getchar();}
	while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
	return date*w;
}
void add(int u,int v,int w){
	a[c].to=v;a[c].w=w;
	a[c].next=head[u];
	head[u]=c++;
	a[c].to=u;a[c].w=0;
	a[c].next=head[v];
	head[v]=c++;
}
bool bfs(){
	int u,v;
	queue<int> q;
	memset(deep,0,sizeof(deep));
	q.push(s);
	deep[s]=1;
	while(!q.empty()){
		u=q.front();
		q.pop();
		for(int i=head[u];i;i=a[i].next){
			v=a[i].to;
			if(a[i].w&&!deep[v]){
				deep[v]=deep[u]+1;
				if(v==t)return true;
				q.push(v);
			}
		}
	}
	return false;
}
int dfs(int x,int limit){
	if(x==t)return limit;
	int v,sum,cost=0;
	for(int i=head[x];i;i=a[i].next){
		v=a[i].to;
		if(a[i].w&&deep[v]==deep[x]+1){
			sum=dfs(v,min(limit-cost,a[i].w));
			if(sum>0){
				a[i].w-=sum;
				a[i^1].w+=sum;
				cost+=sum;
				if(limit==sum)break;
			}
			else deep[v]=-1;
		}
	}
	return cost;
}
int dinic(){
	int ans=0;
	while(bfs())
	ans+=dfs(s,MAX);
	return ans;
}
int main(){
	int x;
	char ch[2],dh[2];
	n=read();
	for(int i=1;i<=n;i++){
		scanf("%s%s%d",ch,dh,&x);
		add((ch[0]-'A'+1),(dh[0]-'A'+1),x);
	}
	s='A'-'A'+1;t='Z'-'A'+1;
	printf("%d\n",dinic());
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值