【Blue Bridge Cup】ALGO-247 Network Streaming Naked Questions

Test question algorithm training network streaming naked questions

Resource limit

Time limit: 1.0s Memory limit: 256.0MB

Problem Description

  A directed graph, find the maximum flow from 1 to N

Input format

  The first line NM indicates the number of points and the number of edges    The
  next M lines, each line stc indicates an edge with capacity c from s to t

Output format

  Maximum flow rate

Sample input

6 10
1 2 4
1 3 8
2 3 4
2 4 4
2 5 1
3 4 2
3 5 2
4 6 7
5 4 6
5 6 3

Sample output

8

Data convention:
n<=1000 m<=10000

Can't understand

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

int N, M;
int s, t;
int res;

map<int, map<int, int>> mp;
vector<int> a, pre;

void Solution(){
	queue<int> que;
	while(1){
		a.assign(N+1, 0);
		a[s] = INT_MAX;
		que.push(s);
		
		while(!que.empty()){
			int v = que.front();
			que.pop();
			for(map<int, int>::iterator i = mp[v].begin(); i != mp[v].end(); i++){
				if(!a[i->first] && i->second > 0){
					pre[i->first] = v;
					a[i->first] = min(a[v], i->second);
					que.push(i->first);
				}
			}
		}
		if(a[t] == 0){
			return ;	//没有流量了 
		}
		res += a[t];
		for(int i = N; i != 1; i = pre[i]){
			mp[pre[i]][i] -= a[t];
			mp[i][pre[i]] += a[t];
		}
	} 
}

int main(int argc, char** argv) {
	cin >> N >> M;
	for(int i = 0; i < M; i++){
		int k, j , l;
		cin >> k >> j >> l;
		mp[k][j] += l;
	}
	
	s = 1;
	t = N;
	pre.assign(N+1, 0);
	Solution();
	cout << res << endl;
	
	return 0;
}

 

Guess you like

Origin blog.csdn.net/weixin_44566432/article/details/114934648