find the safest road
find the safest road
Time Limit : 10000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 23 Accepted Submission(s) : 12
#include<stdio.h>
#include<string.h>
#define MAX(x,y)(x>y?x:y)
const int MAXN=;
double map[MAXN][MAXN],d[MAXN];
int vis[MAXN];
int n;
void initial(){
memset(d,,sizeof(d));
memset(vis,,sizeof(vis));
}
void dijskra(int s){
initial();//写错位置了,应该每次都初始化
d[s]=;
while(true){
int k=-;
for(int i=;i<=n;i++)
if(!vis[i]&&(k==-||d[i]>d[k]))k=i;
if(k==-)break;
vis[k]=;
for(int i=;i<=n;i++)
d[i]=MAX(d[i],d[k]*map[k][i]);
}
}
int main(){int Q,a,b;
while(~scanf("%d",&n)){
memset(map,,sizeof(map));
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
scanf("%lf",&map[i][j]);
scanf("%d",&Q);
while(Q--){
scanf("%d%d",&a,&b);
dijskra(a);
if(d[b]==0.0)puts("What a pity!");
else printf("%.3lf\n",d[b]);
}
}
return ;
}
SPFA:
#include<stdio.h>
#include<string.h>
#include<queue>
using namespace std;
const int MAXN=;
const int MAXM=;
int head[MAXM],vis[MAXN];
double dis[MAXN];
struct Edge{
int from,to,next;
double value;
};
Edge edg[MAXM];
int n,edgnum;
queue<int>dl;
void initial(){
memset(dis,,sizeof(dis));
memset(vis,,sizeof(vis));
while(!dl.empty())dl.pop();
}
void add(int u,int v,double value){
Edge E;
E.from=u;E.to=v;E.value=value;E.next=head[u];
edg[edgnum]=E;
head[u]=edgnum++;
}
void SPFA(int sx){
initial();
dis[sx]=;
vis[sx]=;
dl.push(sx);
while(!dl.empty()){
int k=dl.front();
dl.pop();
vis[k]=;
for(int i=head[k];i!=-;i=edg[i].next){
int v=edg[i].to;
if(dis[k]*edg[i].value>dis[v]){
dis[v]=dis[k]*edg[i].value;
if(!vis[v])
dl.push(v),vis[v]=;
}
}
}
}
int main(){
int a,b,Q;
double v;
while(~scanf("%d",&n)){
memset(head,-,sizeof(head));
edgnum=;
for(int i=;i<=n;i++)
for(int j=;j<=n;j++){
scanf("%lf",&v);
add(i,j,v);
}
scanf("%d",&Q);
while(Q--){
scanf("%d%d",&a,&b);
SPFA(a);
if(dis[b]==0.0)puts("What a pity!");
else printf("%.3lf\n",dis[b]);
}
}
return ;
}
find the safest road的更多相关文章
- 杭电 1595 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【最短路变形,求最大安全系数】
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
- hdu1569find the safest road(floyd变形求最大安全值)
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 ...
- HDU1596 find the safest road
find the safest road XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一 ...
- hdu1596find the safest road(floyd)
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 (Floyd)
HDU.1596 find the safest road (Floyd) 题意分析 与普通的最短路不太相同,本题有些许的变化. 1. 要找到由i到j最安全的路,故在求解的时候要保证mp[i][j]尽 ...
- HDU——1596find the safest road(邻接矩阵+优先队列SPFA)
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- 从setTimeout到浏览器线程机制
看高性能javascipt 这本书时,看到这么一句话: Putting scripts at the top of the page in this way typically leads to a ...
- 键盘有没有NKRO ?微软帮你测
玩家甚至媒体的解读是错的,所以小编在此重点说明一些概念.并分享如何测试.在许多游戏与软体中都会使用组合键功能,也就是同时按下特定几个按键之后就能触发特别的功能,简单的说就是一些动作的快捷键.不过,有时 ...
- 自制证书搭建https服务
第一步,自制CA证书和Server证书,私钥 自制CA私钥 openssl genrsa -des3 -out ca.key 4096 自制CA证书 openssl req -new -x509 -d ...
- Redis 实践笔记
本文来自:http://www.cnblogs.com/me-sa/archive/2012/03/13/redis-in-action.html 最近在项目中实践了一下Redis,过程中遇到并解决了 ...
- how to translate the text of push button
Background:In a project, the need to translate the buttons on the screen, as shown below,the followi ...
- _itemFailedToPlayToEnd: { kind = 1; new = 2; old = 0; }
watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvenVveW91MTMxNA==/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...
- Ubuntu12.04下jamvm1.5.4+classpath-0.98成功执行 helloworld.class
经过两天的努力,总于在ubuntu以下编译好classpath-0.98与jamvm1.5.4,并能成功的运行类文件:jamvm hellowold,当屏幕上打印出"hello world! ...
- Unity 利用NGUI2.6.3做技能冷却的CD效果
NGUI非常强大我们今天来学习一下,如何利用NGUI做技能冷却的CD效果.先导入NGUI的插件.没有的话这里有啊NGUI2.6.3下载地址: http://vdisk.weibo.com/s/KLqn ...
- jQuery滑动选取数值范围插件
HTML 首先载入jQuery库文件以及jRange相关的css文件:jquery.range.css和插件:jquery.range.js <script src="jquery.j ...
- 使用fat jar和exe4j把java程序打包成exe执行文件---转载的
java应用编写测试好之后都是jar包或class文件,客户拿到这个东西后一般是不会java开发者那样在命令窗口下面输入运行的.客户要求的就是直接点击应用名称运行.java在方面做得很不友好,开发人员 ...