洛谷P1730 最小密度路径(floyd)

题意

题目链接

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\n", ans);
	}
	return 0;
}
/*
*/
posted @ 2018-10-16 21:13  自为风月马前卒  阅读(299)  评论(0编辑  收藏  举报

Contact with me