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. JQuery实现的模块交换动画效果

    <!doctype html> <html> <head> <meta http-equiv="content-type" content ...

  2. 寻找 nani (1)

    test.pl文件room(kitchen). room(office). room(hall). room('dining room'). room(cellar). location(desk,o ...

  3. 关于odbc的彻底删除问题

    最近在装一个软件,由于第一次安装产生了一个错误,于是我尝试在卸载之后,重新进行安装~但是,在安装过程当中出现了一个问题. NEWLRE ODBC data source already exists. ...

  4. 基于AQS的锁

    锁分为独占锁和共享锁,它们的主要实现都是依靠AbstractQueuedSynchronizer,这个类只提供一系列公共的方法,让子类来调用.基于我了解不深,从这个类的属性,方法,和独占锁的获取方式去 ...

  5. LESS初探

    1. 安装less $ npm install -g less 2. less文件编译成css文件 $ lessc styles.less styles.css <!DOCTYPE html&g ...

  6. 如何在属性面板中增加一个属性-UI界面编辑器(XproerUI)教程

    版权所有 2009-2015 荆门泽优软件有限公司 保留所有权利 产品首页:http://www.ncmem.com/apps/xproerui/index.asp 开发文档(SkinStudio): ...

  7. CentOS平台部署vsftp(基于虚拟用户)

    1. 安装FTP 1 2 [root@task ~]# yum install vsftpd –y [root@task ~]# chkconfig vsftpd on          # 配置开机 ...

  8. 9png图片制作

    制作步骤不多说了,这儿有链接:http://blog.csdn.net/pugongying1988/article/details/6938972 链接中去边框一个像素可以不用做,直接用androi ...

  9. 什么是SCADA Viewer

    SCADA Viewer 什么是SCADA Viewer SCADA Viewer是一个基于Web的软件框架(基于Web的HMI/SCADA/M2M工业和楼宇自动化,支持Modbus,BACnet,O ...

  10. 日常维护sql

    修复mysqlcheck -u -p --repair  pmdb prefix="/export/data/mysql/bin/mysql -u -p -e" domain=机房 ...