QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#21313 | #1274. Walking Plan | FudanU1# | TL | 1ms | 1772kb | C++17 | 2.2kb | 2022-03-04 15:07:52 | 2022-05-08 02:52:12 |
Judging History
answer
#include <stdio.h>
#include <algorithm>
using namespace std;
struct matrix {
int a[60][60];
} ini , pw;
int n , m , q;
int dis[120][120];
int ans;
void clear () {
}
matrix operator * ( matrix x1 , matrix x2 ) {
int i , j , k;
matrix y;
for ( i = 1 ; i <= n ; i++ ) {
for ( j = 1 ; j <= n ; j++ ) {
y.a[i][j] = -1;
}
}
for ( i = 1 ; i <= n ; i++ ) {
for ( j = 1 ; j <= n ; j++ ) {
for ( k = 1 ; k <= n ; k++ ) {
if ( x1.a[i][j] != -1 && 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;
}
matrix ksm ( matrix f , int x ) {
matrix s;
int i , j;
for ( i = 1 ; i <= n ; i++ ) {
for ( j = 1 ; j <= n ; j++ ) {
s.a[i][j] = -1;
}
s.a[i][i] = 0;
}
while ( x ) {
if ( x % 2 == 1 ) s = s * f;
f = f * f; x = x / 2;
}
return s;
}
void work () {
int i , j , k , u , v , w;
int s , t;
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++ ) {
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 );
}
for ( i = 1 ; i <= n ; i++ ) {
for ( j = 1 ; j <= n ; j++ ) {
ini.a[i][j] = dis[i][j];
}
}
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] );
}
}
}
}
scanf ( "%d" , &q );
for ( i = 1 ; i <= q ; i++ ) {
scanf ( "%d%d%d" , &s , &t , &k );
pw = ksm ( ini , k );
ans = -1;
for ( j = 1 ; j <= n ; j++ ) {
if ( pw.a[s][j] != -1 && dis[j][t] != -1 ) {
if ( ans == -1 ) ans = pw.a[s][j] + dis[j][t];
else ans = min ( ans , pw.a[s][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: 1ms
memory: 1772kb
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: -100
Time Limit Exceeded
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 ...