QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#154795 | #5506. Hyperloop | berarchegas# | TL | 799ms | 33064kb | C++17 | 3.9kb | 2023-08-31 23:02:15 | 2023-08-31 23:02:16 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
mt19937 rng((int) chrono::steady_clock::now().time_since_epoch().count());
const int MOD = 1e9 + 7;
const int MAXN = 2e5 + 5;
const ll INF = 2e18;
const int maxn = 300010;
const ll inf = 1000000000000000000LL;
int n, m;
int ord[maxn];
int dp[maxn][2];
int topoSort[maxn];
int u[maxn], v[maxn], w[maxn];
bool actived[maxn];
ll dist[maxn][2];
vector<pii> adj[maxn];
vector<pii> dag[maxn], inv[maxn];
void dijkstra(int source, int idDij)
{
set<pair<ll,int>> s;
for(int i = 1 ; i <= n ; i++)
dist[i][idDij] = inf;
dist[source][idDij] = 0;
s.insert( { 0 , source } );
while( !s.empty() )
{
int node = s.begin()->second;
s.erase( s.begin() );
for(auto [neighbor, id]: adj[node])
{
if( dist[neighbor][idDij] > dist[node][idDij] + w[id] )
{
s.erase( { dist[neighbor][idDij] , neighbor } );
dist[neighbor][idDij] = dist[node][idDij] + w[id];
s.insert( { dist[neighbor][idDij] , neighbor } );
}
}
}
}
void rebuildDAG()
{
for(int i = 1 ; i <= n ; i++)
dag[i].clear(), inv[i].clear();
for(int i = 1 ; i <= m ; i++)
{
if( actived[i] )
{
dag[ u[i] ].push_back( { v[i] , i } );
inv[ v[i] ].push_back( { u[i] , i } );
}
}
}
void round(int weight)
{
for(int i = 1 ; i <= n ; i++)
{
int node = topoSort[i];
dp[node][0] = 0;
for(auto [neighbor, id]: inv[node])
dp[node][0] = max( dp[node][0] , dp[neighbor][0] + (weight == w[id]));
}
for(int i = n ; i > 0 ; i--)
{
int node = topoSort[i];
dp[node][1] = 0;
for(auto [neighbor, id]: dag[node])
dp[node][1] = max( dp[node][1] , dp[neighbor][1] + (weight == w[id]));
}
for(int i = 1 ; i <= m ; i++)
{
if( !actived[i] )
continue;
if( dp[ u[i] ][0] + (weight == w[i]) + dp[ v[i] ][1] != dp[n][0] )
actived[i] = false;
}
rebuildDAG();
}
vector<int> findPath()
{
int node = 1;
vector<int> path = { 1 };
while( node != n )
{
node = dag[node][0].first;
path.push_back( node );
}
return path;
}
void solve()
{
cin >> n >> m;
for(int i = 1 ; i <= n ; i++)
adj[i].clear();
for(int i = 1 ; i <= m ; i++)
{
cin >> u[i] >> v[i] >> w[i];
adj[ u[i] ].push_back( { v[i] , i } );
adj[ v[i] ].push_back( { u[i] , i } );
}
dijkstra( 1 , 0 );
dijkstra( n , 1 );
for(int i = 1 ; i <= m ; i++)
{
actived[i] = false;
if( dist[ u[i] ][0] + w[i] + dist[ v[i] ][1] == dist[n][0] )
{
actived[i] = true;
continue;
}
swap( u[i] , v[i] );
if( dist[ u[i] ][0] + w[i] + dist[ v[i] ][1] == dist[n][0] )
actived[i] = true;
}
iota( ord + 1 , ord + m + 1 , 1 );
sort( ord + 1 , ord + m + 1 , [&](int i, int j){
return w[i] > w[j];
});
iota( topoSort + 1 , topoSort + n + 1 , 1 );
sort( topoSort + 1 , topoSort + n + 1 , [&](int i, int j){
return dist[i][0] < dist[j][0];
});
rebuildDAG();
for(int i = 1 ; i <= m ; i++)
{
int curEdge = ord[i];
if( !actived[curEdge] )
continue;
round( w[curEdge] );
}
vector<int> ans = findPath();
cout << (int)ans.size() << endl;
for(int x: ans)
cout << x << " ";
cout << endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin >> t;
while( t-- )
solve();
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 9ms
memory: 32920kb
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: 799ms
memory: 33064kb
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...