QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#216262 | #2879. Kaleidoscopic Route | karuna# | TL | 1ms | 8436kb | C++17 | 2.7kb | 2023-10-15 17:02:03 | 2023-10-15 17:02:03 |
Judging History
answer
#include <bits/stdc++.h>
#define ff first
#define ss second
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 101010;
const int INF = 1e9 + 10;
int n, m, d[2][MAXN];
vector<pair<int, int>> g[MAXN];
int back_mn[2][MAXN];
int back_mx[2][MAXN];
int mn[2][MAXN];
int mx[2][MAXN];
int main() {
cin.tie(0); ios_base::sync_with_stdio(0);
cin >> n >> m;
for (int i = 0; i < m; i++) {
int u, v, c; cin >> u >> v >> c;
g[u].push_back({c, v});
g[v].push_back({c, u});
}
for (int t = 0; t < 2; t++) {
queue<int> q;
for (int i = 1; i <= n; i++) d[t][i] = -1;
if (t == 0) q.push(1), d[t][1] = 0;
else q.push(n), d[t][n] = 0;
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto [w, x] : g[v]) {
if (d[t][x] == -1) {
d[t][x] = d[t][v] + 1;
q.push(x);
}
}
}
}
int D = d[0][n];
// cout << d[0][1] << ' ' << d[1][3] << "?\n";
// cout << "distance " << D << "?\n";
for (int t = 0; t < 2; t++) for (int i = 1; i <= n; i++) mn[t][i] = INF;
for (int t = 0; t < 2; t++) {
queue<int> q;
if (t == 0) q.push(1);
else q.push(n);
while (!q.empty()) {
int v = q.front();
q.pop();
for (auto [w, x] : g[v]) {
if (d[t][v] + d[t ^ 1][x] + 1 == D) { // need to check?
// if (t == 0) cout << v << ' ' << x << "?!!\n";
if (mn[t][x] > min(mn[t][v], w)) {
mn[t][x] = min(mn[t][v], w);
back_mn[t][x] = v;
}
if (mx[t][x] < max(mx[t][v], w)) {
mx[t][x] = max(mx[t][v], w);
back_mx[t][x] = v;
}
q.push(x);
}
}
}
}
// for (int i = 1; i <= n; i++) {
// cout << mn[0][i] << ' ' << mx[1][i] << "?\n";
// }
int cs = -1;
int vn = -1;
int val = -1;
for (int i = 1; i <= n; i++) {
if (d[0][i] + d[1][i] != D) continue;
// case 1 : min first
if (val < mx[1][i] - mn[0][i]) {
val = mx[1][i] - mn[0][i];
vn = i;
cs = 1;
}
// case 2 : max first
if (val < mx[0][i] - mn[1][i]) {
val = mx[0][i] - mn[1][i];
vn = i;
cs = 2;
}
}
vector<int> pre, suf;
if (cs == 1) {
for (int v = vn; v != 1; v = back_mn[0][v]) {
pre.push_back(v);
}
pre.push_back(1);
for (int v = vn; v != n; v = back_mx[1][v]) {
if (v != vn) suf.push_back(v);
}
suf.push_back(n);
}
else {
for (int v = vn; v != 1; v = back_mx[0][v]) {
pre.push_back(v);
}
pre.push_back(1);
for (int v = vn; v != n; v = back_mn[1][v]) {
if (v != vn) suf.push_back(v);
}
suf.push_back(n);
}
reverse(pre.begin(), pre.end());
for (int x : suf) pre.push_back(x);
cout << (int)pre.size() - 1 << '\n';
for (int x : pre) cout << x << ' ';
cout << '\n';
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 8436kb
input:
6 8 1 5 2 5 2 5 3 5 4 1 3 10 3 4 6 4 5 7 4 6 8 2 6 1
output:
3 1 5 4 6
result:
ok 3 6
Test #2:
score: -100
Time Limit Exceeded
input:
10 20 1 9 34 6 3 80 7 3 15 5 4 73 4 1 42 8 6 92 2 10 25 4 3 8 9 3 79 3 1 11 9 2 74 10 1 83 3 2 6 10 3 38 10 4 48 1 5 38 6 2 43 4 2 55 8 7 90 3 5 4