MLE求助

P3376 【模板】网络最大流

JiaoKan @ 2019-01-30 11:07:45

RT

#include "pch.h"
#include <iostream>
#include <queue>
using namespace std;
const int inf = 1e9;
int n, m, s, t, g[10001][10001], pre[10001], max_flow = 0;//g[201][201]就re了三个点(#2,#9,#10),开成这样全部MLE,初始内存占用460MB(VS调的)......
bool bfs()
{
    queue<int> q;
    q.push(s);
    int _min;
    memset(pre, 0, sizeof(pre));
    while (!q.empty())
    {
        int tmp = q.front();
        q.pop();
        for (int i = 1; i <= n; i++)
        {
            if (g[tmp][i] != 0 && pre[i] == 0)
            {
                q.push(i);
                pre[i] = tmp;
                if (i == t) return true;
            }
        }
    }
    return false;
}
void work()
{
    while (bfs())
    {
        int _min = inf;
        int k = t;
        while (k != s)
        {
            _min = min(_min, g[pre[k]][k]);
            k = pre[k];
        }
        k = t;
        max_flow += _min;
        while (k != s)
        {
            g[pre[k]][k] -= _min;
            g[k][pre[k]] += _min;
            k = pre[k];
        }
    }
}
int main()
{
    cin >> n >> m >> s >> t;//N、M、S、T,分别表示点的个数、有向边的个数、源点序号、汇点序号。
    cout << n << m << s << t << endl;
    memset(g, 0, sizeof(g));
    for (int i = 1; i <= m; i++)
    {
        int x, y, z;
        cin >> x >> y;
        cin >> g[x][y];
    }
    work();
    cout << max_flow;
    return 0;
}

by 萌田薰子 @ 2019-01-30 11:09:22

@Judgelight 您的g不爆才怪


by NaCly_Fish @ 2019-01-30 11:09:48


by 萌田薰子 @ 2019-01-30 11:09:57

@Judgelight 打这种东西不用邻接表或victor是真的不行


by JiaoKan @ 2019-01-30 11:16:38

@一之濑琴美 谢谢dalao,回来改改吧


by L_M_ @ 2019-01-30 11:27:40

肯定不能用邻接矩阵鸭


|