QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#21380 | #1274. Walking Plan | FudanU1# | AC ✓ | 1236ms | 3840kb | C++17 | 2.7kb | 2022-03-04 17:37:36 | 2022-05-08 02:57:47 |
Judging History
answer
#pragma GCC optimize(3)
#include <stdio.h>
#include <algorithm>
using namespace std;
struct matrix {
int n , m;
int a[51][51];
} ini[120] , ini100[120] , now;
int n , m , q;
int dis[120][120];
int ans;
void clear () {
}
matrix operator * ( matrix &x1 , matrix &x2 ) {
int i , j , k;
matrix y;
y.n = x1.n; y.m = x2.m;
for ( i = 1 ; i <= y.n ; i++ ) {
for ( j = 1 ; j <= y.m ; j++ ) {
y.a[i][j] = -1;
}
}
for ( i = 1 ; i <= x1.n ; i++ ) {
for ( j = 1 ; j <= x1.m ; j++ ) {
if ( x1.a[i][j] == -1 ) continue;
for ( k = 1 ; k <= x2.m ; k++ ) {
if ( x2.a[j][k] != -1 ) {
if ( y.a[i][k] == -1 ) y.a[i][k] = x1.a[i][j] + x2.a[j][k];
else y.a[i][k] = min ( y.a[i][k] , x1.a[i][j] + x2.a[j][k] );
}
}
}
}
return y;
}
void work () {
int i , j , k , u , v , w;
int s , t;
//n = 50; m = 2500;
scanf ( "%d%d" , &n , &m );
for ( i = 1 ; i <= n ; i++ ) {
for ( j = 1 ; j <= n ; j++ ) {
dis[i][j] = -1;
}
}
for ( i = 1 ; i <= m ; i++ ) {
/*u = (i-1) / n + 1;
v = (i-1) % n + 1;
w = rand () % 10000;*/
scanf ( "%d%d%d" , &u , &v , &w );
if ( dis[u][v] == -1 ) dis[u][v] = w;
else dis[u][v] = min ( dis[u][v] , w );
}
ini[0].n = ini[0].m = ini100[0].n = ini100[0].m = n;
for ( i = 1 ; i <=n ; i++ ) {
for ( j = 1 ; j <= n ; j++ ) {
ini[0].a[i][j] = ini100[0].a[i][j] = -1;
}
ini[0].a[i][i] = ini100[0].a[i][i] = 0;
}
for ( i = 1 ; i <= n ; i++ ) {
for ( j = 1 ; j <= n ; j++ ) {
ini[1].a[i][j] = dis[i][j];
}
}
ini[1].n = ini[1].m = n;
for ( i = 2 ; i <= 100 ; i++ ) {
ini[i] = ini[1] * ini[i-1];
}
ini100[1] = ini[100];
for ( i = 2 ; i <= 100 ; i++ ) {
ini100[i] = ini100[1] * ini100[i-1];
}
for ( i = 1 ; i <= n ; i++ ) {
dis[i][i] = 0;
}
for ( k = 1 ; k <= n ; k++ ) {
for ( i = 1 ; i <= n ; i++ ) {
for ( j = 1 ; j <= n ; j++ ) {
if ( dis[i][k] != -1 && dis[k][j] != -1 ) {
if ( dis[i][j] == -1 ) dis[i][j] = dis[i][k] + dis[k][j];
else dis[i][j] = min ( dis[i][j] , dis[i][k] + dis[k][j] );
}
}
}
}
//q = 100000;
scanf ( "%d" , &q );
for ( i = 1 ; i <= q ; i++ ) {
/*s = rand () % n + 1;
t = rand () % n + 1;
k = rand () % 10000 + 1;*/
scanf ( "%d%d%d" , &s , &t , &k );
now.n = 1;
now.m = n;
for ( j = 1; j <= n ; j++ ) now.a[1][j] = ini[k%100].a[s][j];
now = now * ini100[k/100];
ans = -1;
for ( j = 1 ; j <= n ; j++ ) {
if ( now.a[1][j] != -1 && dis[j][t] != -1 ) {
if ( ans == -1 ) ans = now.a[1][j] + dis[j][t];
else ans = min ( ans , now.a[1][j] + dis[j][t] );
}
}
printf ( "%d\n" , ans );
}
}
int main () {
int t;
scanf ( "%d" , &t );
while ( t-- ) {
work ();
clear ();
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3724kb
input:
2 3 3 1 2 1 2 3 10 3 1 100 3 1 1 1 1 2 1 1 3 1 2 1 1 2 1 1 2 1 1
output:
111 1 11 -1
result:
ok 4 lines
Test #2:
score: 0
Accepted
time: 661ms
memory: 3836kb
input:
10 5 10 4 2 55 2 1 26 3 4 83 5 2 16 4 5 54 3 4 38 2 1 68 2 5 1 4 5 85 4 2 60 20 2 1 13 1 4 17 3 2 20 2 4 16 4 2 17 4 2 2 3 1 2 1 5 10 2 1 8 4 5 15 4 2 16 3 1 18 5 2 7 4 2 9 4 3 16 1 4 18 3 2 5 1 5 9 5 1 19 1 2 16 20 50 4 16 25 3 16 990 9 18 863 19 12 236 4 10 360 13 4 585 14 17 164 8 18 198 2 17 804...
output:
128 -1 246 -1 191 70 119 -1 94 173 189 253 67 123 -1 -1 125 -1 195 -1 -1 23449 3476 18735 17412 23124 29499 26452 9757 21128 9524 -1 4486 8797 8041 33717 32669 5108 32534 13020 22800 4411 3529 37460 4191 15863 5342 22005 -1 14496 16535 13644 -1 33956 28717 24721 13816 26289 8840 28137 9991 14430 -1 ...
result:
ok 406120 lines
Test #3:
score: 0
Accepted
time: 1236ms
memory: 3840kb
input:
10 5 10 4 1 62 3 5 93 4 5 99 2 5 63 2 4 26 5 3 31 2 5 14 5 3 54 1 2 47 1 5 18 200 2 5 8 3 1 17 1 5 11 2 1 17 4 2 1 4 2 13 5 1 13 5 2 2 1 4 8 2 3 4 1 1 1 1 5 7 3 5 3 5 3 12 1 4 18 3 1 11 5 4 2 1 3 4 5 5 13 2 3 12 1 4 19 3 5 17 3 5 20 3 3 1 5 5 9 1 5 7 2 4 9 4 5 16 1 3 19 4 1 18 2 1 16 3 5 17 4 5 7 3 ...
output:
365 -1 466 763 109 649 -1 -1 343 137 135 288 217 775 883 -1 -1 173 868 531 883 1085 1333 124 620 288 431 744 848 872 763 1085 339 -1 343 -1 341 -1 161 784 -1 109 244 49 -1 424 871 -1 -1 872 587 393 270 763 61 701 620 -1 -1 270 558 -1 559 341 -1 540 135 -1 857 -1 -1 810 31 713 467 -1 -1 -1 277 784 69...
result:
ok 900200 lines