UVA_820_Internet Bandwidth

#include<iostream>
#include<sstream>
#include<string>
#include<vector>
#include<list>
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<algorithm>
#include<numeric>
#include<cmath>
#pragma warning(disable:4996)
using std::cin;
using std::cout;
using std::endl;
using std::stringstream;
using std::string;
using std::vector;
using std::list;
using std::pair;
using std::set;
using std::multiset;
using std::map;
using std::multimap;
using std::stack;
using std::queue;
using std::priority_queue;
class Edge
{
public:
	int from,to, flow, capacity;
	Edge(const int &v,const int &a, const int &c,const int &f)
	{
		from=v,to = a, capacity = c,flow=f;
	}
};
class Network
{
private:
	int n, m;//表示顶点个数和边个数
	int src, sink;//表示源点和汇点
	int max_flow;//最大流
	vector<Edge>edge;//边数的两倍
	vector<vector<int>>adjList;//邻接表
public:
	void addEdge(const int &from,const int &to,const int &capacity)
	{
		edge.push_back({from,to,capacity,0});
		adjList[from].push_back(edge.size()-1);
		edge.push_back({to,from,0,0});
		adjList[from].push_back(edge.size() - 1);
	}
	Network(const int &N)
	{
		max_flow = 0,n = N;
		cin >> src >> sink >> m;
		adjList.resize(n + 1);
		for (int i = 0; i < m; i++)
		{
			int from, to, capacity;
			cin >> from >> to >> capacity;
			addEdge(from,to,capacity);
			addEdge(to, from, capacity);
		}
	}
	void getMaxFlow()
	{
		vector<int>path(n + 1);
		while (1)
		{
			vector<int>augument(n + 1);
			queue<int>Q;
			Q.push(src);
			augument[src] = 2000000000;
			while (!Q.empty())
			{
				auto x = Q.front();
				Q.pop();
				for (int i = 0; i < adjList[x].size(); i++)
				{
					Edge&e = edge[adjList[x][i]];
					if (!augument[e.to]&&e.capacity>e.flow)
					{
						path[e.to] = adjList[x][i];
						augument[e.to] = std::min(augument[x],e.capacity-e.flow);
						Q.push(e.to);
					}
				}
				if (augument[sink])
				{
					break;
				}
			}
			if (!augument[sink])
			{
				break;
			}
			for (auto u = sink;u!=src;u=edge[path[u]].from)
			{
				edge[path[u]].flow += augument[sink];
				edge[path[u]^1].flow-= augument[sink];
			}
			max_flow += augument[sink];
		}
	}
	void printMaxFlow()
	{
		cout << "The bandwidth is " << max_flow << "." << endl<<endl;
	}
};
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	int count = 0,n;
	while (cin >> n&&n)
	{
		cout << "Network " << ++count << endl;
		Network network(n);
		network.getMaxFlow();
		network.printMaxFlow();
	}
	return 0;
}

你可能感兴趣的:(UVA_820_Internet Bandwidth)