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 ...
随机推荐
- List 泛型 集合中 Find 的用法
以前在开发中为了对List,String[].Array进行元素的查找一般都是这样做:List lists = new List();list.add("111");.....fo ...
- sniffer底层网络配置记录
公司需要一款可以检查网络的软件,之后任务分配到我们组 经过讨论最终方案是以wireshark为版本,进行二次开发,主要目的在于简化上层操作复杂性(软件使用人群为非专业性的) 软件分为三部分,其一底层网 ...
- php中Content-type说明
'hqx' -> 'application/mac-binhex40','cpt' -> 'application/mac-compactpro','doc' -> 'applica ...
- tablediff使用方法
tablediff -sourceserver "db0093\sql2008" -sourcedatabase "testly" -sourcetable & ...
- oracle sql查询转义下划线
1,看以下结果 select * from test where login like '%CF_%'; LOGIN------------------------------------------ ...
- MIME Type
一.首先,我们要了解浏览器是如何处理内容的.在浏览器中显示的内容有 HTML.有 XML.有 GIF.还有 Flash --那么,浏览器是如何区分它们,决定什么内容用什么形式来显示呢?答案是 MIME ...
- sql查询上一条记录和下一条记录
上一条记录的SQL语句: * from news where newsid<id order by newsid DESC 下一条记录的SQL语句: * from news where news ...
- iOS开发笔记之Runtime实用总结
前言 runtime的资料网上有很多了,部分有些晦涩难懂,我通过自己的学习方法总结一遍,主要讲一些常用的方法功能,以实用为主,我觉得用到印象才是最深刻的.另外runtime的知识还有很多,想要了解更多 ...
- 为什么重写equals时必须重写hashCode方法?
原文地址:http://www.cnblogs.com/shenliang123/archive/2012/04/16/2452206.html 首先我们先来看下String类的源码:可以发现Stri ...
- MM常用表
Table 表描述 MKPF 物料凭证抬头 MSEG 物料凭证段