hdoj 1596 find the safest road【最短路变形,求最大安全系数】
find the safest road
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 9122 Accepted Submission(s):
3213
0 和 1 间的实数(包括0,1),一条从u 到 v 的通道P 的安全度为Safe(P) = s(e1)*s(e2)…*s(ek) e1,e2,ek是P 上的边
,现在8600 想出去旅游,面对这这么多的路,他想找一条最安全的路。但是8600 的数学不好,想请你帮忙 ^_^
第一行:n。n表示城市的个数n<=1000;
接着是一个n*n的矩阵表示两个城市之间的安全系数,(0可以理解为那两个城市之间没有直接的通道)
接着是Q个8600要旅游的路线,每行有两个数字,表示8600所在的城市和要去的城市
pity!",
其他的输出这两个城市之间的最安全道路的安全系数,保留三位小数。
#include<stdio.h>
#include<string.h>
#define MAX 1010
#define INF 0x3f3f3f
#define DD double
int n,m;
DD map[MAX][MAX],low[MAX];
int vis[MAX];
void getmap()
{
int i,j;
DD a;
memset(map,0,sizeof(map));
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
scanf("%lf",&a);
map[i][j]=a;
}
}
}
void dijkstra(int a,int b)
{
int i,j,next;
DD max;
for(i=1;i<=n;i++)
low[i]=map[a][i];
memset(vis,0,sizeof(vis));
vis[a]=1;
for(i=1;i<n;i++)
{
max=0;
next=a;
for(j=1;j<=n;j++)
{
if(!vis[j]&&max<low[j])
{
next=j;
max=low[j];
}
}
vis[next]=1;
for(j=1;j<=n;j++)
{
if(!vis[j]&&low[j]<low[next]*map[next][j]) //此处的求法有所不同
low[j]=low[next]*map[next][j];
}
}
if(low[b]==0)
printf("What a pity!\n");
else
printf("%.3lf\n",low[b]);
}
void solve()
{
scanf("%d",&m);
for(int i=1;i<=m;i++)
{
int a,b;
scanf("%d%d",&a,&b);
if(a==b)
printf("1.000\n");//注意自己到自己的情况
else
dijkstra(a,b);
}
}
int main()
{
int i,j;
while(scanf("%d",&n)!=EOF)
{
getmap();
solve();
}
return 0;
}
hdoj 1596 find the safest road【最短路变形,求最大安全系数】的更多相关文章
- HDU 1596 find the safest road (最短路)
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdoj 1596 find the safest road
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析:Dijkstra变体,最短路径判断计算方式:Safe(P) = s(e1)*s(e2)…* ...
- hdu 1596 find the safest road(最短路,模版题)
题目 这是用Dijsktra做的,稍加改动就好,1000ms..好水.. #define _CRT_SECURE_NO_WARNINGS #include<string.h> #inclu ...
- HDU.1596 find the safest road (Floyd)
HDU.1596 find the safest road (Floyd) 题意分析 与普通的最短路不太相同,本题有些许的变化. 1. 要找到由i到j最安全的路,故在求解的时候要保证mp[i][j]尽 ...
- 杭电 1596 find the safest road (最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=1596 这道题目与杭电2544最短路的思想是一样的.仅仅只是是把+改成了*,输入输出有些不一样而已. find t ...
- 杭电1596 find the safest road
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 1596 find the safest road (最短路径)
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu 1596 find the safest road
http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <cstdio> #include <cstring> #inc ...
- hdu 1596 find the safest road (变形SP && dij+heap)
Problem - 1596 变形最短路问题,给出邻接矩阵,要求求出给定点对间安全率最大值. 这题可以用dijkstra+heap来做.对于每一个查询,做一次dij即可. 代码如下: #include ...
随机推荐
- QuickSort 递归 分治
QuickSort 参考<算法导论>,<C程序设计语言> #include<stdio.h> void swap(int v[], int i, int j); v ...
- matlab结构体形式保存数据生成.mat文件< 转>
2015年 参加天池大数据竞赛 为了建立模型,打算基于matlab使用Random Forest Algorithm的工具包 该工具包我在此分享给大家,http://yunpan.cn/cVX ...
- boost::bind实践
第一部分源码为基础实践: /*Beyond the C++ Standard Library ( An Introduction to Boost )[CN].chm*/ /*bind的用法*/ #i ...
- cell的循环使用
cell的循环利用:(对cell的简单优化) 1.创建一个标示(Identifier),用于区分缓存池里的不同cell. 2.去缓存池里拿自己对应的cell,用到dequeueReusableCell ...
- GoJS研究,简单图表制作。
话不多说,先上图 我在这个中加入了缩略图.鼠标放大缩小等功能. <!doctype html> <html> <head> <title>Flowcha ...
- 【随记】数据库还原失败System.Data.SqlClient.SqlError: 无法执行 BACKUP LOG,因为当前没有数据库备份
解决方法:去掉下图中箭头所指的项.
- 关于js效果不提示就执行了刷新(解决 在h-ui框架中)
parent.layer.msg('保存成功!<script>setTimeout("window.location.reload();",1100);<\/sc ...
- onethink 换空间报错 解决方案
onethink 换空间的时候有两个配置文件 Application\Common\Conf Application\User\Conf 如果报错先测试数据库 <?php $con = mysq ...
- c++清除输入缓冲区之 sync() vs ignore()
最近在写程序的时候总是不注意输入缓冲区内是否还有东西,导致出现了一些异常,调试了半天.所以来上一贴,学习注意,引以为戒! http://blog.chinaunix.net/uid-21254310- ...
- PHP之路——Redis安装
windows: redis下载链接:https://github.com/ServiceStack/redis-windows 然后编辑redis.windows.conf文件,我看网上有的教程说编 ...