QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#420884#8038. Hammer to FallVeryAmazedML 0ms3652kbC++201.6kb2024-05-25 02:52:072024-05-25 02:52:07

Judging History

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

  • [2024-05-25 02:52:07]
  • 评测
  • 测评结果:ML
  • 用时:0ms
  • 内存:3652kb
  • [2024-05-25 02:52:07]
  • 提交

answer

// Right side, 7th row, inner, Johns Hopkins University

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for (int i = a; i < (b); +i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;

int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin.exceptions(cin.failbit);

	int n, m, q; cin >> n >> m >> q;
	vector<int> residents(n); for (int &x : residents) cin >> x;

	vector adj(n, vector<pair<int,int>>());
	while (m--) {
		int u, v, w; cin >> u >> v >> w; u--, v--;
		adj[u].emplace_back(v, w);
		adj[v].emplace_back(u, w);
	}

	vector<pair<int,int>> parent(n);
	vector children(n, vector<pair<int,int>>());
	vector<multiset<long long>> cost(n);
	auto build = [&](auto self, int u, int p, int w) -> void {
		parent[u] = {p, w};
		for (auto [v,w1] : adj[u]) if (v != p) self(self, v, u, w1), cost[u].emplace(w1);
	};
	build(build, 0, 0, 0);


	vector<long long> cur_cost(n);
	vector<int> hammers(q); for (int &x : hammers) cin >> x, x--; reverse(hammers.begin(),hammers.end());
	for (int h : hammers) {
		if (h) {
			auto it = cost[parent[h].first].find(cur_cost[h] + parent[h].second);
			cost[parent[h].first].erase(it);
		}

		long long mn = LLONG_MAX;
		if (h) mn = cur_cost[parent[h].first] + parent[h].second;
		if (cost[h].size()) mn = min(mn, *cost[h].begin());
		cur_cost[h] = mn;

		if (h) {
			cost[parent[h].first].emplace(cur_cost[h] + parent[h].second);
		}
	}

	const int M = 998244353;
	int ans = 0;
	for (int i = 0; i < n; i++) {
		ans = (ans + 1ll * residents[i] * (cur_cost[i] % M)) % M;
	}
	cout << ans << '\n';
}

詳細信息

Test #1:

score: 100
Accepted
time: 0ms
memory: 3652kb

input:

3 2 2
1 1 1
2 3 10
1 2 1
3 2

output:

12

result:

ok single line: '12'

Test #2:

score: 0
Accepted
time: 0ms
memory: 3652kb

input:

2 1 10
5000 5000
1 2 10000
1 2 2 1 2 2 1 1 1 2

output:

550000000

result:

ok single line: '550000000'

Test #3:

score: -100
Memory Limit Exceeded

input:

10 10 10
5 14 99 14 18 4 58 39 48 60
2 4 4
6 9 56
10 8 34
7 5 96
1 3 26
3 7 92
6 8 4
5 1 72
7 6 39
7 2 93
8 8 9 10 2 2 5 9 2 3

output:


result: