find the safest road

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 6973    Accepted Submission(s): 2469

Problem Description
XX星球有非常多城市,每一个城市之间有一条或多条飞行通道,可是并非全部的路都是非常安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包含0,1),一条从u 到 v 的通道P 的安全度为Safe(P) = s(e1)*s(e2)…*s(ek) e1,e2,ek是P 上的边 ,如今8600 想出去旅游,面对这这么多的路,他想找一条最安全的路。可是8600 的数学不好,想请你帮忙 ^_^
 
Input
输入包含多个測试实例,每一个实例包含:

第一行:n。n表示城市的个数n<=1000;

接着是一个n*n的矩阵表示两个城市之间的安全系数,(0能够理解为那两个城市之间没有直接的通道)

接着是Q个8600要旅游的路线,每行有两个数字,表示8600所在的城市和要去的城市
 
Output
假设86无法达到他的目的地,输出"What a pity!",

其它的输出这两个城市之间的最安全道路的安全系数,保留三位小数。
 
Sample Input
3
1 0.5 0.5
0.5 1 0.4
0.5 0.4 1
3
1 2
2 3
1 3
 
Sample Output
0.500
0.400
0.500
 
Author
ailyanlu
 
Source
 
Recommend
8600   |   We have carefully selected several similar problems for you:  1217 1598 1142 1690 1385




题意非常明确了,还是一些小细节的问题,模板打多了毁带来一些思维僵化,

所以找一些模板题但又有些变通的题最好了。



代码:1671MS

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
using namespace std;
#define M 1050
int n,m;
double
map[M][M],dis[M];
void
Dijkstra(int x,int y)
{ bool
v[M]={0};
int
i,j;
for(
i=1;i<=n;i++) dis[i]=(i==x?1:0);
for(
i=1;i<=n;i++)
{
int
k;
double
Min=0; //在这里WA了一发,模板打多了就仅仅会int了。
for(
j=1;j<=n;j++) if(!v[j] && dis[j]>Min) Min=dis[k=j];
v[k]=1;
for(
j=1;j<=n;j++) dis[j]=max(dis[j],dis[k]*map[k][j]);
}
if(
dis[y]) printf("%.3lf\n",dis[y]);
else
printf("What a pity!\n");
} int main()
{
int
i,j;
int
a,b,c;
while(
scanf("%d",&n)!=EOF && n)
{

memset(map,0,sizeof(map));
for(
i=1;i<=n;i++)
for(
j=1;j<=n;j++)
{

scanf("%lf",&map[i][j]);
}

//题目给的是邻接矩阵,所以不要初始化了。
scanf("%d",&m);
for(
i=1;i<=m;i++)
{

scanf("%d%d",&a,&b);
Dijkstra(a,b);
}
}
return
0;
}

我个人觉得用Floyd算法应该也能够,就是时限的问题了。

这题应该要用SPFA算法去做,只是时限放的非常宽,所以就变成模板题了。

事实上精品题和模板题差距就在时限和思想双方面。

HDU 1596 find the safest road (最短路)的更多相关文章

  1. hdu 1596 find the safest road(最短路,模版题)

    题目 这是用Dijsktra做的,稍加改动就好,1000ms..好水.. #define _CRT_SECURE_NO_WARNINGS #include<string.h> #inclu ...

  2. HDU.1596 find the safest road (Floyd)

    HDU.1596 find the safest road (Floyd) 题意分析 与普通的最短路不太相同,本题有些许的变化. 1. 要找到由i到j最安全的路,故在求解的时候要保证mp[i][j]尽 ...

  3. hdu 1596 find the safest road (最短路径)

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. hdu 1596 find the safest road

    http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <cstdio> #include <cstring> #inc ...

  5. hdu 1596 find the safest road (变形SP && dij+heap)

    Problem - 1596 变形最短路问题,给出邻接矩阵,要求求出给定点对间安全率最大值. 这题可以用dijkstra+heap来做.对于每一个查询,做一次dij即可. 代码如下: #include ...

  6. HDU 1596 find the safest road(SPFA)

    Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条 ...

  7. hdu 1596 find the safest road (dijkstra)

    Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条 ...

  8. 杭电 1596 find the safest road (最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=1596 这道题目与杭电2544最短路的思想是一样的.仅仅只是是把+改成了*,输入输出有些不一样而已. find t ...

  9. hdoj 1596 find the safest road【最短路变形,求最大安全系数】

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

随机推荐

  1. maven项目建立pom.xml报无法解析org.apache.maven.plugins:maven-resources-plugin:2.4.3

    一.发现问题 建立maven项目后,pom.xml在显示红叉.鼠标放上去,显示Executiondefault-testResources of goalorg.apache.maven.plugin ...

  2. android2.2应用开发之IccCard(sim卡或USIM卡)

    tyle="margin:20px 0px 0px; font-size:14px; line-height:26px; font-family:Arial; color:rgb(51,51 ...

  3. poj 3311 状压DP

    经典TSP变形 学到:1.floyd  O(n^3)处理随意两点的最短路 2.集合的位表示,我会在最后的总结出写出.注意写代码之前一定设计好位的状态.本题中,第0位到第n位分别代表第i个城市,1是已经 ...

  4. codeforces55D数位dp

    codeforces55D 查询给定区间内的beautiful number.  一个数字是beautiful number当且仅当能被自己的各个数字不为0的位整除. 这个dp的状态还是挺难想的.一个 ...

  5. 使用cocos2d 2.1制作一条河游戏(4): 主要的游戏逻辑BaseLayer设计

    前段时间一直忙着.没有时间更新博客.今天,仍然需要一段时间才能实现对游戏的一小部分,最后打动他. BaseLayer.h: #import <GameKit/GameKit.h> #imp ...

  6. Windows Phone开发(27):隔离存储A

    原文:Windows Phone开发(27):隔离存储A 在很多资料或书籍上都翻译为"独立存储",不过,我想了一下,决定将IsolatedStorage翻译为"隔离存储& ...

  7. OGG-01008 Extract displays Discarding bad record (discard recs=1) when using filter or where clause

    因为在extract參数文件里使用了where语句,而where后面的的条件列又不是主键,没有为update.delete操作记录日志,因此会报1008错误. Applies to: Oracle G ...

  8. 正确Linux新手很实用20命令

     //正确Linux新手很实用20命令 //slwang  2014.4.19 1, ls list directory contents 内容 ls -l     //以详情模式(long li ...

  9. 72_leetcode_Construct Binary Tree from Preorder and Inorder Traversal

    Given preorder and inorder traversal of a tree, construct the binary tree. Note: You may assume that ...

  10. ADO.NET连接方式

    使用Command.DataReader和DataSet两种方法实现数据绑定 方法1:使用Command和DataReader SqlConnection con = new SqlConnectio ...