洛谷P 2936

题目链接:全流

最大流模板题!

#include<bits/stdc++.h>
using namespace std;
const int N = 100100;
const int INF = 10000000;
struct Node{
	int to;
	int ne;
	int w;
}e[N<<1];
int head[N],dis[N],deth[N];
int n,m,x,y,val,cnt;
void init()
{
	memset(head,-1,sizeof(head));
	memset(dis,0x7f,sizeof(dis));
	cnt = 0;
}
void add(int u,int v,int val)
{
	e[cnt].to = v;
	e[cnt].w = val;
	e[cnt].ne = head[u];
	head[u] = cnt ++; 
}
int bfs()
{
	queue<int>q;
	memset(deth,0,sizeof(deth));
	deth[1] = 1;
	q.push(1);
	while(!q.empty())
	{
		int now = q.front();
		q.pop();
		for(int i=head[now];~i;i=e[i].ne)
		{
			int to = e[i].to;
			if(deth[to] == 0 && e[i].w > 0)
			{
				deth[to] = deth[now] + 1;
				q.push(to);
			}
		}
	}
	if(deth[26] == 0)
		return 0;
	return 1;
}
int dfs(int k,int dist)
{
	if(k == 26)
	return dist;
	int temp = dist;
	for(int i=head[k];~i;i=e[i].ne)
	{
		int to = e[i].to;
		if(deth[to] == deth[k] + 1 && e[i].w>0)
		{
			int di = dfs(to,min(temp,e[i].w));
			if(di>0)
			{
				temp -= di;
				e[i].w -= di;
				e[i^1].w += di;
				if(temp <= 0)
				return dist;
			} 
		}
	}
	return dist - temp;
}
int dinic()
{
	int ans = 0;
	while(bfs())
	{
		while(int di = dfs(1,INF))
		 
			ans += di;
		}
	}
	return ans;
}
int main()
{
	scanf("%d",&n);
	init();
	for(int i=1;i<=n;i++)
	{
		char ch1,ch2;
		cin>>ch1>>ch2>>val;
		x = ch1 - 'A' + 1;
		y = ch2 - 'A' + 1;
		add(x,y,val);
		add(y,x,0);
	}
	cout<<dinic()<<endl;
	return 0;
}

很奇怪,只能得 90 分,第二个样例输出完全没问题!(我下载过样例试了

跪求看到这篇博客的孩纸指点一下!感激不尽    Orz

好吧,挖个坑,先留着,想想再来填!

猜你喜欢

转载自blog.csdn.net/lizhiwei2017/article/details/80084068