蓝桥杯网络流裸题c语言,蓝桥杯算法训练:网络流裸题

题目描述:网络流裸题

问题描述

一个有向图,求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

样例输出

思路

代码

// 算法训练:网络流裸题

#include

#include

#include

#include

using namespace std;

typedef long long ll;

const int maxn = 1000 + 10;

const int INF = 0x7fffffff;

int n, m, g[maxn][maxn];

int maxflow = 0;

int increase = 0;

int pre[maxn], flow[maxn];

queue q;

int bfs(int s, int t)

{

while (!q.empty())

q.pop();

q.push(1);

for (int i = 1; i <= t; i++)

{

pre[i] = -1;

}

pre[s] = 0;

flow[s] = INF;

int ok = false;

while (!q.empty())

{

int a = q.front();

q.pop();

if (a == t)

break;

for (int i = 1; i <= t; i++)

{

if (g[a][i] > 0 && pre[i] == -1)

{

pre[i] = a;

flow[i] = min(flow[a], g[a][i]);

q.push(i);

if (i == t)

{

ok = true;

break;

}

}

}

if (ok)

break;

}

if (pre[t] == -1)

return -1;

else

return flow[t];

}

int main()

{

scanf("%d%d", &n, &m);

for (int i = 0; i < m; i++)

{

int s, t, c;

scanf("%d%d%d", &s, &t, &c);

g[s][t] += c;

}

while ((increase = bfs(1, n)) != -1)

{

maxflow += increase;

int b = n;

while (b != 1)

{

g[b][pre[b]] += increase;

g[pre[b]][b] -= increase;

b = pre[b];

}

}

printf("%d", maxflow);

return 0;

}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值