题意
Sol
zz floyd。
很显然的一个dp方程(f[i][j][k][l])表示从(i)到(j)经过了(k)条边的最小权值
可以证明最优路径的长度一定(leqslant N)
然后一波(n^4) dp就完了
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF = 1e9 + 10;
inline int read() {
char c = getchar(); int x = 0, f = 1;
while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}
while(c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return x * f;
}
int N, M;
int f[51][51][1001];
int main() {
//memset(f, 0x3f, sizeof(f));
N = read(); M = read();
for(int k = 1; k <= N; k++)
for(int i = 1; i <= N; i++)
for(int j = 1; j <= N; j++)
f[i][j][k] = INF;
for(int i = 1; i <= M; i++) {
int x = read(), y = read(), w = read();
f[x][y][1] = min(f[x][y][1], w);
}
for(int l = 2; l <= N; l++)// num of edge
for(int k = 1; k <= N; k++) // mid point
for(int i= 1; i <= N; i++) // start point
for(int j = 1; j <= N; j++) // end point
f[i][j][l] = min(f[i][j][l], f[i][k][l - 1] + f[k][j][1]);
int Q = read();
while(Q--) {
int x = read(), y = read();
double ans = 1e18;
for(int i = 1; i <= N; i++) if(f[x][y][i] != INF) ans = min(ans, (double) f[x][y][i] / i);
if(ans == 1e18) puts("OMG!");
else printf("%.3lf
", ans);
}
return 0;
}
/*
*/