MLE是什么鬼
查看原帖
MLE是什么鬼
895312
Archy_楼主2024/7/13 11:04

为什么 MLE 啊,想不明白。是 queue 炸了?

#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
#define INF 1e9
#define N 2000
#define M 30000

using namespace std;
int n, m, s, t, ans, mincost;
int head[N], depth[N], cur[N], a[N];


inline int rd() {
	int w = 1, x = 0;
	char c = getchar();
	while (c < 48 || c > 57) {
		if (c == 45) w *= -1;
		c = getchar();
	}
	while (c >= 48 && c <= 57) {
		x = (x << 1) + (x << 3) + (c ^ 48);
		c = getchar();
	}
	return x * w;
}

struct node {
	int to, next, w, c;
} e[M];

int cnt = 1;
inline void addi(int u, int v, int f, int cost) {
	e[++cnt].to = v;
	e[cnt].w = f;
	e[cnt].c = cost;
	e[cnt].next = head[u];
	head[u] = cnt;
}
inline void add(int u, int v, int f, int cost) {
	addi(u,v,f,cost);
	addi(v,u,0,-cost);
}

inline bool spfa() {
	queue <int> q;
	for(int i = 1; i <= t; i++) depth[i] = INF;
	memcpy(cur, head, sizeof(head));
	depth[s] = 0;
	q.push(s);
	while (!q.empty()) {
		int u = q.front();
		q.pop();
		for (int i = head[u]; i; i = e[i].next) {
			int v = e[i].to;
			if (e[i].w > 0 && (depth[v] > depth[u] + e[i].c)) {
				depth[v] = depth[u] + e[i].c;
				q.push(v);
			}
		}
	}
	return depth[t] != INF;
}

inline int dfs1(int u, int nowflow) {
	if (!nowflow || u == t) return nowflow;
	for (int i = cur[u]; i; i = e[i].next) {
		int v = e[i].to;
		cur[u] = i;
		if ((depth[v] == depth[u] + e[i].c) && e[i].w > 0) {
			int flow = dfs1(v, min(nowflow, e[i].w));
			if (flow > 0) {
				mincost += flow * e[i].c;
				e[i].w -= flow;
				e[i ^ 1].w += flow;
				return flow;
			}
		}
	}
	return 0;
}

signed main() {
	n = rd(), m = rd();
	s = 0, t = n + n + 1;
	for (int i = 1; i <= n; i++) {
		a[i] = rd();
		add(s, i, 1, 0);
		add(s, i + n, 1, a[i]);
		add(i + n, t, 1, 0);
	}
	for (int i = 1; i <= m; i++) {
		int u = rd(), v = rd(), w = rd();
		if(u > v) swap(u, v);
		if(w < a[v]) add(u, v + n, 1, w);
	}
	while (spfa()) {
		while (int maxflow = dfs1(s, INF)) {
			ans += maxflow;
		}
	}
	cout << mincost;
	return 0;
}
2024/7/13 11:04
加载中...