【蓝桥杯】ALGO-247 网络流裸题

试题 算法训练 网络流裸题

资源限制

时间限制:1.0s   内存限制:256.0MB

问题描述

  一个有向图,求1到N的最大流

输入格式

  第一行N M,表示点数与边数
  接下来M行每行s t c表示一条从s到t的容量为c的边

输出格式

  一个数最大流量

样例输入

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

样例输出

8

数据约定:
n<=1000 m<=10000

不能理解

#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;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值