hdoj 1596 find the safest rode
第一行:n。n表示城市的个数n<=1000;
接着是一个n*n的矩阵表示两个城市之间的安全系数,(0可以理解为那两个城市之间没有直接的通道)
接着是Q个8600要旅游的路线,每行有两个数字,表示8600所在的城市和要去的城市
其他的输出这两个城市之间的最安全道路的安全系数,保留三位小数。
#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的更多相关文章
- hdoj 1596 find the safest road
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=1596 分析:Dijkstra变体,最短路径判断计算方式:Safe(P) = s(e1)*s(e2)…* ...
- hdoj 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]尽 ...
- 杭电 1596 find the safest road (最短路)
http://acm.hdu.edu.cn/showproblem.php?pid=1596 这道题目与杭电2544最短路的思想是一样的.仅仅只是是把+改成了*,输入输出有些不一样而已. find t ...
- HDOJ 1596
9899828 2013-12-27 16:42:37 Accepted 1596 3312MS 6668K 711 B C++ 泽泽 floyed暴力 #include<cstdio> ...
- 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 (最短路)
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 ...
- 杭电1596 find the safest road
find the safest road Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Ot ...
随机推荐
- JS中this的指向问题&使用call或apply模拟new
this的指向由调用时决定而不是定义时决定,定义的方式: //直接定义在函数里 var a="window中的a"; var name="window"; fu ...
- XML序列化与反序列化
public static class XmlHelper { private static void XmlSerializeInternal(Stream stream, object o, En ...
- 搭建angular2环境(1)
1.安装node(windows环境) 进入node官网https://nodejs.org/en/下载好后直接安装就可以了.安装完成之后可以在命令窗口查看安装的版本 2.安装npm express ...
- frame busting
[frame busting] 参考:http://book.51cto.com/art/201204/330076.htm
- 获取项目中文件,存放到Debug中。
说起这个,还真是费了一般功夫. 说个最简单的方法: 第一步:把需要生成到Debug中的文件放到项目中(注意:当前文件夹目录是什么样的,存放到Debug中也是什么样) 第二部:设置文件属性中 复制到输出 ...
- Android using Accelerometer
http://code.tutsplus.com/tutorials/using-the-accelerometer-on-android--mobile-22125 public class Mai ...
- iOS Core Animation之CALayer心得
使用CALayer的mask实现注水动画效果 Core Animation一直是iOS比较有意思的一个主题,使用Core Animation可以实现非常平滑的炫酷动画.Core animtion的AP ...
- python 识别图片验证码报IOError
说一下困扰了我一周的问题:识别图片验证码 本来我按照安装步骤(http://www.cnblogs.com/yeayee/p/4955506.html?utm_source=tuicool&u ...
- VoLTE 注册流程
1.开关按钮位置: 设置--> 更多--> 移动网络--> 增强型4G LTE模式 2.该设置开关使用了SwitchPreference控件,addEnhanced4GLteSw ...
- 字节b换算kb/mb/gb/tb/pb
public static string HumanReadableFilesize(double size) { string[] units = new string[] { "B&qu ...