이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
vector <ll> adj[501][501];
const ll inf = 1e16;
int main () {
int n, m;
cin >> n >> m;
bool flag = 1;
for (int i = 1; i <= m; i++) {
ll a, b, c;
cin >> a >> b >> c;
adj[a][b].push_back(c);
adj[b][a].push_back(c);
flag &= b == a + 1;
}
for (int i = 1; i < n; i++) {
sort(adj[i][i + 1].begin(), adj[i][i + 1].end());
}
int q;
cin >> q;
while (q--) {
ll x;
cin >> x;
if (flag) {
ll sum = 0;
for (int i = 2; i <= n; i++) {
ll mn = inf;
auto z = lower_bound(adj[i][i - 1].begin(), adj[i][i - 1].end(), x) - adj[i][i - 1].begin();
if (z != (int)adj[i][i - 1].size()) {
mn = min(mn, abs(x - adj[i][i - 1][z]));
}
z--;
if (z >= 0) {
mn = min(mn, abs(x - adj[i][i - 1][z]));
}
sum += mn;
}
cout << sum << '\n';
} else {
ll dist[n + 1], vis[n + 1];
for (int i = 0; i <= n; i++) {
dist[i] = inf;
vis[i] = 0;
}
ll sum = 0;
dist[1] = 0; vis[1] = 1;
for (int j = 2; j <= n; j++) {
for (auto k : adj[1][j]) {
dist[j] = min(dist[j], abs(k - x));
}
}
for (int i = 2; i <= n; i++) {
ll mn = 1 + inf, pos = -1;
for (int j = 1; j <= n; j++) {
if (!vis[j] && dist[j] < mn) {
pos = j;
mn = dist[j];
}
}
// for (int j = 1; j <= n; j++) cout << dist[j] << " ";
//cout << '\n';
vis[pos] = 1;
sum += dist[pos];
for (int j = 1; j <= n; j++) {
if (vis[j]) continue;
for (auto k : adj[pos][j]) {
dist[j] = min(dist[j], abs(k - x));
}
}
}
//cout << '\n';
cout << sum << '\n';
}
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |