您的位置:首页 > Web前端

HDU-1596-find the safest road

2016-12-05 19:30 106 查看
ACM模版

描述



题解

很水的一道题,Floyd扫描一遍,把加法改为乘法就行了。

代码

#include <iostream>
#include <cstring>
#include <cstdio>

using namespace std;

const int MAXN = 1010;

double cost[MAXN][MAXN];
double lowcost[MAXN][MAXN];

void Floyd(int n)
{
memcpy(lowcost, cost, sizeof(cost));

for (int k = 0; k < n; k++)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
if (lowcost[i][j] < (lowcost[i][k] * lowcost[k][j]))
{
lowcost[i][j] = lowcost[i][k] * lowcost[k][j];
}
}
}
}
return ;
}

int main(int argc, const char * argv[])
{
int n, q;

while (cin >> n)
{
memset(cost, 0, sizeof(cost));

for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)
{
scanf("%lf", cost[i] + j);
}
}

Floyd(n);

cin >> q;
int st, ed;

while (q--)
{
scanf("%d %d", &st, &ed);
st--;
ed--;
if (lowcost[st][ed] == 0)
{
printf("What a pity!\n");
}
else
{
printf("%.3lf\n", lowcost[st][ed]);
}
}
}

return 0;
}


参考

《最短路》
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  最短路