I have been thinking about this question for a long time ~~ I only came up with dp [60] [60] [60] [1000], which is completely inefficient. (During update, I also need to scan 60 ^ 3 space... so .. the efficiency is 1000*60 ^ 6 .. ...)... I suddenly realized it after reading other people's code... perform Floyd for 1000 times ~~ Efficiency 1000*60 ^ 3 = 216000000 barely acceptable...
Program:
[Cpp]
# Include <iostream>
# Include <algorithm>
# Include <stdio. h>
# Include <string. h>
# Include <cmath>
# Include <queue>
# Define OOS 2000000000
# Define ll long
Using namespace std;
Int n, m, r, car [63] [63] [63], ans [63] [63] [1003];
Void getanswer ()
{
Int I, j, t, k;
For (I = 1; I <= n; I ++)
For (j = 1; j <= n; j ++)
{
Ans [I] [j] [0] = 1000000;
For (k = 1; k <= m; k ++)
If (ans [I] [j] [0]> car [k] [I] [j]) ans [I] [j] [0] = car [k] [I] [j];
}
For (t = 1; t <= 1000; t ++)
{
For (I = 1; I <= n; I ++)
For (j = 1; j <= n; j ++)
Ans [I] [j] [t] = ans [I] [j] [T-1];
For (k = 1; k <= n; k ++)
For (j = 1; j <= n; j ++)
For (I = 1; I <= n; I ++)
If (ans [I] [j] [t]> ans [I] [k] [T-1] + ans [k] [j] [0])
Ans [I] [j] [t] = ans [I] [k] [T-1] + ans [k] [j] [0];
}
}
Int main ()
{
Scanf ("% d", & n, & m, & r );
Int I, j, x, y, k, s, e, t;
For (t = 1; t <= m; t ++)
{
For (I = 1; I <= n; I ++)
For (j = 1; j <= n; j ++)
Scanf ("% d", & car [t] [I] [j]);
For (k = 1; k <= n; k ++)
For (I = 1; I <= n; I ++)
For (j = 1; j <= n; j ++)
If (car [t] [I] [j]> car [t] [I] [k] + car [t] [k] [j])
Car [t] [I] [j] = car [t] [I] [k] + car [t] [k] [j];
}
Getanswer ();
While (r --)
{
Scanf ("% d", & s, & e, & t );
Printf ("% d \ n", ans [s] [e] [t]);
}
Return 0;
}