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;
}
#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;
const int infinity =1000000000;
class Network
{
private:
	int vexnum, edgenum, maxflow, source, destination;
	vector<int>remnant, path;
	vector<vector<int>>flow, capacity;
public:
	void addEdge()
	{
		int from, to, c; cin >> from >> to >> c;
		capacity[from - 1][to - 1] += c;
		capacity[to - 1][from - 1] += c;
	}
	Network(const int &n)
	{
		vexnum = n;
		cin >> source >> destination >> edgenum;
		source--, destination--;
		flow.resize(vexnum, (vector<int>)vexnum), capacity.resize(vexnum, (vector<int>)vexnum);//最开始每条边的流量都是0
		for (int i = 0; i < edgenum; i++)
		{
			addEdge();
		}
	}
	void getMaxFlow_EK() 
	{
		maxflow = 0;
		queue<int> Q;
		while (!Q.empty())
		{
			Q.pop();
		}
		vector<int>path(vexnum);
		while (true) 
		{
			vector<int>remnant(vexnum);//残余流量得变0,一开始所有点都没流入对吧
			remnant[source] = infinity;   //源点嘛,剩余流量无限是必须的...
			Q.push(source);    //从源点开始进行BFS找增广路
			int from, to;
			while (!Q.empty())
			{
				from = Q.front();Q.pop();
				for (to = 0; to < vexnum; to++)//遍历所有点,找可行边
				{
					if (!remnant[to] && capacity[from][to]>flow[from][to])
					{    //该点剩余流量为0 且 容量大于流量,也就是找到了新的结点 
						path[to] = from;   //找到新结点,父节点得记录一下吧
						Q.push(to);
						remnant[to] = std::min(remnant[from], capacity[from][to] - flow[from][to]);    //如果u的剩余流量能填满uv就填满,不能的话就把u这点的流量全部流向uv 
					}
				}
			}
			if (remnant[destination] == 0)     //如果当前已经是最大流,汇点没有残余流量
			{
				break;
			}
			for (from = destination; from != source; from = path[from]) 
			{   //如果还能增广,那么回溯,从汇点往回更新每条走过的边的流量
				flow[path[from]][from] += remnant[destination];  //更新正向流量   (注意这里更新的是流量,而不是容量)
				flow[from][path[from]] -= remnant[destination];  //更新反向流量
			}
			maxflow += remnant[destination];
		}
	}
	void print()
	{
		cout << "The bandwidth is " << maxflow << "." << endl<<endl;
	}
};
int main()
{
	//freopen("input.txt", "r", stdin);  
	//freopen("output.txt", "w", stdout);  
	int n,count=0;
	while (cin>>n&&n)
	{
		cout << "Network " << ++count << endl;
		Network network(n);
		network.getMaxFlow_EK();
		network.print();
	}
	return 0;
}
<pre name="code" class="cpp">#include<iostream>  
#include<sstream>  
#include<string>  
#include<vector>  
#include<list>  
#include<set>  
#include<map>
#include<stack>  
#include<queue>  
#include<algorithm>  
#include<numeric>  
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#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;
const int MaxVexNum = 1000;
const int INF = 1000000000;
class Edge
{
public:
	int to, capacity, flow;
	Edge(int t, int c, int f)
	{
		to = t,capacity = c,flow = f;
	}
};
class Network
{
private:
	int vexnum, edgenum, source, destination,maxflow;
	vector<vector<Edge>>adjList;
public:
	void addEdge(int from, int to, int cap)
	{
		adjList[from].push_back({ to, cap, (int)adjList[to].size() });
		adjList[to].push_back({ from, 0, (int)adjList[from].size() - 1 });
	}
	Network()
	{
		adjList.resize(MaxVexNum);
		cin >> vexnum >> source >> destination >> edgenum;
		for (int i = 0; i < edgenum; i++)
		{
			int from, to, capacity;
			cin >> from >> to >> capacity;
			addEdge(from, to, capacity);
			addEdge(to, from, capacity);
		}
	}
	int dfs(int src, int sink, int flow,vector<bool>&used)
	{
		if (src == sink)
		{
			return flow;
		}
		used[src] = true;
		for (int i = 0; i<adjList[src].size(); i++)
		{
			Edge &edge = adjList[src][i];
			if (!used[edge.to] && edge.capacity>0)
			{
				int minflow = dfs(edge.to, sink, std::min(flow, edge.capacity),used);
				if (minflow>0)
				{
					edge.capacity -= minflow;
					adjList[edge.to][edge.flow].capacity += minflow;
					return minflow;
				}
			}
		}
		return 0;
	}
	void getMaxFlow()
	{
		maxflow = 0;
		while (1)
		{
			vector<bool>used(MaxVexNum);
			int f = dfs(source, destination, INF,used);
			if (!f)
			{
				return;
			}
			maxflow += f;
		}
	}
	void print()
	{
		cout << "The bandwidth is " << maxflow << '.' << endl<<endl;
	}
};
int main()
{
	//freopen("input.txt", "r", stdin);
	//freopen("output.txt", "w", stdout);
	char ch; int count = 0;
	while ((ch=cin.get())!='0')
	{
		printf("Network %d\n", ++count);
		ungetc(ch,stdin);
		Network network;
		network.getMaxFlow();
		network.print();
		cin.get();
	}
	return 0;
}



 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值