QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#275325#7883. Takeout DeliveringCSUST_GXLWA 142ms41408kbC++202.5kb2023-12-04 16:44:502023-12-04 16:44:51

Judging History

你现在查看的是最新测评结果

  • [2023-12-04 16:44:51]
  • 评测
  • 测评结果:WA
  • 用时:142ms
  • 内存:41408kb
  • [2023-12-04 16:44:50]
  • 提交

answer

#include<unordered_map>
#include<functional>
#include<algorithm>
#include<iostream>
#include<cassert>
#include<cstring>
#include<cstdlib>
#include<numeric>
#include<iomanip>
#include<bitset>
#include<random>
#include<cstdio>
#include<string>
#include<vector>
#include<array>
#include<ctime>
#include<stack>
#include<queue>
#include<cmath>
#include<set>
#include<map>

#define fi first
#define se second
#define pii pair<int,int>
#define NO cout << "NO\n"
#define YES cout << "YES\n"
#define stop system("pause");
#define debug printf("++ ++\n");
#define cout(a) cout << (#a) << " = " << a << endl;
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int N = 3e5;
random_device gen;
mt19937 rnd(gen());
int n, m;
int dis1[N + 5], disn[N + 5];
vector<array<int, 3> > e;
vector<pii > G[N + 5];

/* 终点是一切概率的结束,也是一切期望的开始 */
/* *
 * 可能做法: 基础算法(思维, 暴力, 贪心) / 图论
 * */

struct DSU {
	std::vector<int> pre, sz;

	DSU(int n) : pre(n + 1), sz(n + 1, 1) {
		std::iota(pre.begin(), pre.end(), 0ll);
	}

	int find(int x) {
		return pre[x] == x ? x : pre[x] = find(pre[x]);
	}

	int size(int x) {
		return sz[find(x)];
	}

	bool same(int x, int y) {
		return find(x) == find(y);
	}

	bool merge(int x, int y) {
		int fx = find(x), fy = find(y);
		if (fx == fy) return false;
		if (sz[fx] > sz[fy]) swap(fx, fy);
		sz[fy] += sz[fx];
		pre[fx] = fy;
		return true;//合并成功
	}

};

void dfs(int father, int u, int tmp, int dis[]) {
	dis[u] = tmp;
	for (auto [v, w] : G[u]) {
		if (v == father) continue;
		dfs(u, v, max(tmp, w), dis);
	}
}


void solve() {
	cin >> n >> m;
	DSU dsu(n);
	for (int i = 1, u, v, w; i <= m; i++) {
		cin >> u >> v >> w;
		e.push_back({u, v, w});
	}
	std::sort(e.begin(), e.end(), [&](const array<int, 3> a, const array<int, 3> b) {
		return a.at(2) < b.at(2);
	});
	for (auto [u, v, w] : e) {
		if (dsu.same(u, v)) continue;
		dsu.merge(u, v);
		G[u].emplace_back(v, w);
		G[v].emplace_back(u, w);
	}
	dfs(0, 1, 0, dis1);
	dfs(0, n, 0, disn);
	int ans = INF;
	for (auto [u, v, w] : e) { //枚举最大边, 消除同侧影响
		int uw = dis1[u], vw = disn[v];
		if (w >= max(uw, vw)) ans = min(ans, max(uw, vw) + w);
		uw = disn[u], vw = dis1[v];
		if (w >= max(uw, vw)) ans = min(ans, max(uw, vw) + w);
	}
	cout << ans << '\n';
}

signed main() {
	std::ios::sync_with_stdio(false), cin.tie(nullptr), cout.tie(nullptr);
	int t = 1;
//	cin >> t;
	while (t--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 11876kb

input:

4 6
1 2 2
1 3 4
1 4 7
2 3 1
2 4 3
3 4 9

output:

5

result:

ok 1 number(s): "5"

Test #2:

score: -100
Wrong Answer
time: 142ms
memory: 41408kb

input:

300000 299999
80516 80517 597830404
110190 110191 82173886
218008 218009 954561262
250110 250111 942489774
66540 66541 156425292
34947 34948 239499776
273789 273790 453201232
84428 84429 439418398
98599 98600 326095035
55636 55637 355015760
158611 158612 684292473
43331 43332 43265001
171621 171622 ...

output:

1061109567

result:

wrong answer 1st numbers differ - expected: '1999991697', found: '1061109567'