QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#491940 | #5506. Hyperloop | piratZnachor | TL | 436ms | 4056kb | C++14 | 6.1kb | 2024-07-26 01:41:47 | 2024-07-26 01:41:47 |
Judging History
answer
#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <array>
#include <atomic>
#include <chrono>
#include <condition_variable>
#include <forward_list>
#include <future>
#include <initializer_list>
#include <mutex>
#include <random>
#include <ratio>
#include <regex>
#include <scoped_allocator>
#include <system_error>
#include <thread>
#include <tuple>
#include <typeindex>
#include <type_traits>
#include <unordered_map>
#include <unordered_set>
#include <cassert>
using namespace std;
void __print(int x) {cerr << x;}
void __print(long x) {cerr << x;}
void __print(long long x) {cerr << x;}
void __print(unsigned x) {cerr << x;}
void __print(unsigned long x) {cerr << x;}
void __print(unsigned long long x) {cerr << x;}
void __print(float x) {cerr << x;}
void __print(double x) {cerr << x;}
void __print(long double x) {cerr << x;}
void __print(char x) {cerr << '\'' << x << '\'';}
void __print(const char *x) {cerr << '\"' << x << '\"';}
void __print(const string &x) {cerr << '\"' << x << '\"';}
void __print(bool x) {cerr << (x ? "true" : "false");}
template<typename T, typename V>
void __print(const pair<T, V> &x) {cerr << '{'; __print(x.first); cerr << ','; __print(x.second); cerr << '}';}
template<typename T>
void __print(const T &x) {int f = 0; cerr << '{'; for (auto &i: x) cerr << (f++ ? "," : ""), __print(i); cerr << "}";}
void _print() {cerr << "]\n";}
template <typename T, typename... V>
void _print(T t, V... v) {__print(t); if (sizeof...(v)) cerr << ", "; _print(v...);}
#ifndef ONLINE_JUDGE
#define debug(x...) cerr << "[" << #x << "] = ["; _print(x)
#else
#define debug(x...)
#endif
// #define int long long
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define pb push_back
#define BOOST ios_base::sync_with_stdio(false),cin.tie(NULL),cout.tie(NULL);
typedef long long ll;
typedef long double ld;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ii> vii;
typedef vector<ll> vll;
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
const ll INF = 1e18;
vll dijkstra(vector<vii> &adj, int start) {
int n = adj.size() - 1;
vll dist(n + 1, INF);
priority_queue<pair<int,ll>, vector<pair<int,ll>>, function<bool(pair<int,ll>&, pair<int,ll>&)>>
pq([&](pair<int,ll> &p1, pair<int,ll> &p2) {
return p1.second > p2.second;
});
dist[start] = 0;
pq.push({start, dist[start]});
while(pq.size() > 0) {
auto p = pq.top();
pq.pop();
if(dist[p.first] != p.second) {
continue;
}
for(auto p2 : adj[p.first]) {
if(dist[p.first] + p2.second < dist[p2.first]) {
dist[p2.first] = dist[p.first] + p2.second;
pq.push({p2.first, dist[p2.first]});
}
}
}
return dist;
}
vector<int> toposort(vector<vii> &adj) {
int n = adj.size() - 1;
vector<int> res;
vector<int> deg(n + 1, 0);
vector<int> cur;
for(int u = 1; u <= n; u++) {
for(auto p : adj[u]) {
deg[p.first]++;
}
}
for(int u = 1; u <= n; u++) {
if(deg[u] == 0) {
cur.pb(u), res.pb(u);
}
}
while(cur.size() > 0) {
int u = cur.back();
cur.pop_back();
for(auto p : adj[u]) {
deg[p.first]--;
if(deg[p.first] == 0) {
cur.pb(p.first), res.pb(p.first);
}
}
}
if((int)res.size() != n) {
assert(false);
}
return res;
}
bool bigger_lex(vi &v1, vi &v2) {
for(int i = 0; i < (int)min(v1.size(), v2.size()); i++) {
if(v1[i] > v2[i]) return true;
else if(v1[i] < v2[i]) return false;
}
return v1.size() > v2.size();
}
void test_case() {
int n, m;
cin >> n >> m;
vector<vii> adj(n + 1, vii());
for(int i = 0; i < m; i++) {
int u, v, c;
cin >> u >> v >> c;
adj[u].pb({v, c});
adj[v].pb({u, c});
}
vll dist1 = dijkstra(adj, 1), dist2 = dijkstra(adj, n);
vector<vii> adj2(n + 1, vii());
for(int u = 1; u <= n; u++) {
for(auto p : adj[u]) {
int v = p.first, c = p.second;
if(dist1[u] + c + dist2[v] == dist1[n]) {
adj2[u].pb({v, c});
}
}
}
adj.clear();
adj = adj2;
adj2.clear();
vector<int> topo = toposort(adj);
reverse(all(topo));
vector<pair<int,int>> nxt(n + 1, {-1, -1});
for(int rep = 0; rep < n; rep++) {
int u = topo[rep];
pair<int,int> best_arg = {-1, -1};
vi best{};
for(auto p : adj[u]) {
vi path{p.second};
int cur = p.first;
while(nxt[cur].first != -1) {
path.pb(nxt[cur].second);
cur = nxt[cur].first;
}
sort(all(path), [&](int &a, int &b) { return a > b; });
if(bigger_lex(path, best)) {
best = path, best_arg = p;
}
}
nxt[u] = best_arg;
}
vi ans{1};
int cur = 1;
while(nxt[cur].first != -1) {
ans.pb(nxt[cur].first);
cur = nxt[cur].first;
}
cout << ans.size() << "\n";
for(auto x : ans) {
cout << x << " ";
}
cout << "\n";
}
int32_t main() {
BOOST;
int tc = 1;
cin >> tc;
while(tc--) {
test_case();
}
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 3604kb
input:
2 4 6 1 2 1 1 3 2 2 3 1 2 4 2 3 4 1 1 4 4 6 11 1 2 9 2 3 12 3 4 3 4 5 5 5 6 10 6 1 22 2 4 9 3 6 1 4 6 5 2 5 2 3 5 8
output:
3 1 2 4 5 1 2 5 3 6
result:
ok correct (2 test cases)
Test #2:
score: 0
Accepted
time: 436ms
memory: 4056kb
input:
600 320 1547 204 81 13768 232 97 9939 97 249 3719 201 109 14322 183 132 40881 142 143 1 275 186 24548 18 236 7907 30 317 11845 131 130 1 311 300 11704 141 92 41925 174 191 32128 119 120 1 184 183 1 310 309 1 283 270 25477 233 141 36076 212 92 13770 307 110 40656 218 137 14033 180 85 41892 200 199 44...
output:
184 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 10...
result:
ok correct (600 test cases)
Test #3:
score: -100
Time Limit Exceeded
input:
4 100000 220000 48940 43355 42347 77914 77913 1 45236 82683 42904 22563 16038 34866 81537 81538 43088 49803 51485 25497 63071 25523 14336 44102 39850 43782 13607 92386 16724 98711 73651 46840 17775 16801 28765 5757 98829 13508 85095 48444 1 9198 43003 32678 14461 14462 1 20245 48742 18138 89120 8911...
output:
35000 1 24721 24648 24647 24700 24305 24272 99392 25020 25051 25070 24443 24448 25109 25096 43056 43055 43054 43053 43052 43051 43050 43049 43048 43047 43046 43045 43044 43043 43042 43041 43040 43039 43038 43037 43036 43035 43034 43033 43032 43031 43030 43029 43028 43027 43026 43025 43024 43023 4302...