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. apache配置多域名多站点记录

    <VirtualHost *:80>  DocumentRoot "/mnt/web/www.*.cn"  ServerName www.*.cn  ErrorLog ...

  2. GIT GUI的使用

    http://blog.csdn.net/fym0512/article/details/7713006

  3. 完整的 mime type 列表

    原文地址:http://blog.csdn.net/zhaoyw2008/article/details/46647723 Suffixes applicable Media type and sub ...

  4. 分支合并git checkout adview git merge adview3

    分支合并 git checkout adview git merge adview3

  5. 网络编程——URL编程

    URL:是统一资源定位器的简称,它表示Internet某一资源的地址.通过URL我们可以访问Internet上的各种网络资源,比如最常见的www,ftp站点.浏览器通过解析给定的URL可以在网络上查找 ...

  6. java file类的常用方法和属性

    1 常用方法       a.createNewFile方法 public boolean createNewFile() throws IOException 该方法的作用是创建指定的文件.该方法只 ...

  7. js算出生日是当年第多少天

    var year, month, day, monthSum = 0; var arr = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, ...

  8. android添加第三方字体并设置的简单使用

    1.java文件 package lpc.com.project006; import android.app.Activity; import android.content.res.AssetMa ...

  9. VLC嵌入网页,终于要成功了!

    <OBJECT classid="clsid:9BE31822-FDAD-461B-AD51-BE1D1C159921" width="640" heig ...

  10. NGUI 屏幕自适应大屏与小屏(初始设定宽高为1280x720,能适应比其小或者更大的屏)

    具体细节可以参考另外一篇随笔! 以下提供的算法完成的事: 1.自适应1280x720分辨率以下的屏幕 2.自适应1280x720分辨率以上的屏幕 在我设定的要求内包括的分辨率大部分都测过了,背景图.全 ...