记录编号 |
21881 |
评测结果 |
AAAAAAAA |
题目名称 |
最小密度路径 |
最终得分 |
100 |
用户昵称 |
lc |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
0.078 s |
提交时间 |
2010-11-15 18:09:36 |
内存使用 |
1.15 MiB |
显示代码纯文本
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<algorithm>
using namespace std;
const int maxn = 60,INF = 1000000000;
int N,M,Q;
int Map[maxn][maxn];
bool Reach[maxn][maxn];
int F[maxn][maxn][maxn];
void prep()
{
scanf("%d%d",&N,&M);
for (int i=1; i<=N; i++)
for (int j=1; j<=N; j++)
for (int L=1; L<=N; L++)
F[i][j][L] = INF;
for (int x=1; x<=N; x++) F[x][x][0] = 0;
for (int i=1; i<=M; i++)
{
int x,y,u;
scanf("%d%d%d",&x,&y,&u);
F[x][y][1] = u < F[x][y][1] ? u : F[x][y][1];
Reach[x][y] = true;
}
}
void work()
{
for (int L=2; L<=N; L++)
for (int x=1; x<=N; x++)
for (int y=1; y<=N; y++)
for (int P=1; P<=N; P++)
{
F[x][y][L] = min(F[x][y][L], F[x][P][L-1] + F[P][y][1] );
Reach[x][y] = Reach[x][y] || (F[x][y][L]!=INF);
}
}
void query()
{
scanf("%d",&Q);
for (int i=1; i<=Q; i++)
{
int x,y;
scanf("%d%d",&x,&y);
if (!Reach[x][y])
{
printf("OMG!\n"); continue;
}
double ave = INF;
for (int L=1; L<=N; L++)
if ((double)F[x][y][L]/L < ave)
ave = (double)F[x][y][L] / L;
printf("%.3f\n",ave);
}
}
int main()
{
freopen("path.in","r",stdin);
freopen("path.out","w",stdout);
prep();
work();
query();
return 0;
}