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
 
思路:算法还是利用dijkstra算法,只要改成求的是最大距离就好了,要注意数的类型
 
 #include <stdio.h>
#include <algorithm>
int n, q;
double safe[], sa[][];
int vis[];
double max(double x, double y)
{
return x > y ? x : y;
}
void dijkstra(int s, int t)
{
int u, v;
for(u = ; u <= n; u++)
{
vis[u] = ;
safe[u] = 0.0;
}
safe[s] = 1.0;
while(true)
{
v= -;
for(u = ; u <= n; u++)
if(!vis[u] && (safe[u]>safe[v] || v==-))
v = u;
if(v == -)
break;
vis[v] = ;
for(u = ; u <= n; u++)
{
safe[u] = max(safe[u], safe[v]*sa[v][u]);
}
}
if(safe[t] == 0.0)
printf("What a pity!\n");
else
printf("%.3f\n", safe[t]);
}
int main()
{
double s;
while(~scanf("%d", &n))
{
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
{
scanf("%lf", &s);
if(i <= j)
sa[i][j] = sa[j][i] = s;
}
scanf("%d", &q);
while(q--)
{
int a, b;
scanf("%d%d", &a, &b);
dijkstra(a, b);
}
}
return ;
}

spfa算法代码:

 #include <stdio.h>
#include <string.h>
#include <queue>
#define INF 0x3f3f3f3f
#define N 1010
#define M 10000010
using namespace std;
int n, cnt;
int vis[N], head[N];
double dis[N];
struct node
{
int from, to, next;
double val;
}edge[M];
void add(int x, int y, double z)
{
node e = {x, y, head[x], z};
edge[cnt] = e;
head[x] = cnt++;
}
void spfa(int s, int e)
{
queue<int>q;
for(int i = ; i <= n; i++)
{
dis[i] = 0.0;
}
memset(vis, , sizeof(vis));
q.push(s);
dis[s] = 1.0;
vis[s] = ;
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = ;
for(int i = head[u]; i != -; i = edge[i].next)
{
int v = edge[i].to;
if(dis[v] < dis[u]*(edge[i].val))
{
dis[v] = dis[u]*(edge[i].val);
if(!vis[v])
{
vis[v] = ;
q.push(v);
}
}
}
}
if(dis[e] == 0.0)
printf("What a pity!\n");
else
printf("%.3f\n", dis[e]);
}
int main()
{
while(~scanf("%d", &n))
{
double a;
cnt = ;
memset(head, -, sizeof(head));
for(int i = ; i <= n; i++)
for(int j = ; j <= n; j++)
{
scanf("%lf", &a);
add(i, j, a);
} int b;
scanf("%d", &b);
while(b--)
{
int start, end;
scanf("%d%d", &start, &end);
spfa(start, end);
}
}
return ;
}

hdoj 1596 find the safest rode的更多相关文章

  1. hdoj 1596 find the safest road

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析:Dijkstra变体,最短路径判断计算方式:Safe(P) = s(e1)*s(e2)…* ...

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

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

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

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

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

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

  5. HDOJ 1596

    9899828 2013-12-27 16:42:37 Accepted 1596 3312MS 6668K 711 B C++ 泽泽 floyed暴力 #include<cstdio> ...

  6. hdu 1596 find the safest road

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

  7. HDU 1596 find the safest road (最短路)

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

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

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

  9. 杭电1596 find the safest road

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

随机推荐

  1. JS中this的指向问题&使用call或apply模拟new

    this的指向由调用时决定而不是定义时决定,定义的方式: //直接定义在函数里 var a="window中的a"; var name="window"; fu ...

  2. XML序列化与反序列化

    public static class XmlHelper { private static void XmlSerializeInternal(Stream stream, object o, En ...

  3. 搭建angular2环境(1)

    1.安装node(windows环境) 进入node官网https://nodejs.org/en/下载好后直接安装就可以了.安装完成之后可以在命令窗口查看安装的版本 2.安装npm express ...

  4. frame busting

    [frame busting] 参考:http://book.51cto.com/art/201204/330076.htm

  5. 获取项目中文件,存放到Debug中。

    说起这个,还真是费了一般功夫. 说个最简单的方法: 第一步:把需要生成到Debug中的文件放到项目中(注意:当前文件夹目录是什么样的,存放到Debug中也是什么样) 第二部:设置文件属性中 复制到输出 ...

  6. Android using Accelerometer

    http://code.tutsplus.com/tutorials/using-the-accelerometer-on-android--mobile-22125 public class Mai ...

  7. iOS Core Animation之CALayer心得

    使用CALayer的mask实现注水动画效果 Core Animation一直是iOS比较有意思的一个主题,使用Core Animation可以实现非常平滑的炫酷动画.Core animtion的AP ...

  8. python 识别图片验证码报IOError

    说一下困扰了我一周的问题:识别图片验证码 本来我按照安装步骤(http://www.cnblogs.com/yeayee/p/4955506.html?utm_source=tuicool&u ...

  9. VoLTE 注册流程

    1.开关按钮位置:   设置--> 更多--> 移动网络--> 增强型4G LTE模式 2.该设置开关使用了SwitchPreference控件,addEnhanced4GLteSw ...

  10. 字节b换算kb/mb/gb/tb/pb

    public static string HumanReadableFilesize(double size) { string[] units = new string[] { "B&qu ...