Problem - 1596

  变形最短路问题,给出邻接矩阵,要求求出给定点对间安全率最大值。

  这题可以用dijkstra+heap来做。对于每一个查询,做一次dij即可。

代码如下:

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue> using namespace std; const int N = ;
double wt[N][N];
bool vis[N];
#define _clr(x) memset(x, 0, sizeof(x))
typedef pair<double, int> PDBI;
priority_queue<PDBI> priq; double dij(int s, int t, int n) {
while (!priq.empty()) priq.pop();
_clr(vis);
for (int i = ; i < n; i++) priq.push(PDBI(wt[s][i], i));
while (!priq.empty()) {
double w = priq.top().first;
int id = priq.top().second;
priq.pop();
if (vis[id]) continue;
// cout << "id!!! " << id << endl;
vis[id] = true;
if (id == t) return w;
for (int i = ; i < n; i++) {
if (vis[i]) continue;
if (wt[s][i] < wt[s][id] * wt[id][i]) {
wt[s][i] = wt[s][id] * wt[id][i];
priq.push(PDBI(wt[s][i], i));
}
}
}
return wt[s][t];
} int main() {
// freopen("in", "r", stdin);
int n, m;
while (~scanf("%d", &n)) {
for (int i = ; i < n; i++) {
for (int j = ; j < n; j++) {
scanf("%lf", &wt[i][j]);
}
}
scanf("%d", &m);
for (int i = , x, y; i < m; i++) {
scanf("%d%d", &x, &y);
double ans = dij(x - , y - , n);
if (ans < 1e-) puts("What a pity!");
else printf("%.3f\n", ans);
}
}
return ;
}

——written by Lyon

hdu 1596 find the safest road (变形SP && dij+heap)的更多相关文章

  1. HDU.1596 find the safest road (Floyd)

    HDU.1596 find the safest road (Floyd) 题意分析 与普通的最短路不太相同,本题有些许的变化. 1. 要找到由i到j最安全的路,故在求解的时候要保证mp[i][j]尽 ...

  2. HDU 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 (最短路径)

    find the safest road Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Ot ...

  4. hdu 1596 find the safest road

    http://acm.hdu.edu.cn/showproblem.php?pid=1596 #include <cstdio> #include <cstring> #inc ...

  5. hdu 1596 find the safest road(最短路,模版题)

    题目 这是用Dijsktra做的,稍加改动就好,1000ms..好水.. #define _CRT_SECURE_NO_WARNINGS #include<string.h> #inclu ...

  6. HDU 1596 find the safest road(SPFA)

    Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条 ...

  7. hdu 1596 find the safest road (dijkstra)

    Problem Description XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条 ...

  8. hdoj 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 (最短路)

    http://acm.hdu.edu.cn/showproblem.php?pid=1596 这道题目与杭电2544最短路的思想是一样的.仅仅只是是把+改成了*,输入输出有些不一样而已. find t ...

随机推荐

  1. update当根据条件不同时 更新同一个字段的方法 或多表插入

    1.通过存储过程 循环 传值 create or replace procedure p_u isbegin for rs in (select distinct (rks) from rkbz)lo ...

  2. 2018-8-10-如何移动-nuget-缓存文件夹

    title author date CreateTime categories 如何移动 nuget 缓存文件夹 lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...

  3. 如何高效地在github上找开源项目学习?

    1.高级条件组合(精确搜索) in:readme 微服务 stars:>1000 in:readme spring security stars:>3000 in:name python  ...

  4. JS函数式编程 - 概念

    最近在看Typescript,顺便看了一些函数式编程,然后半个国庆假期就没有了.做个笔记,分几个部分写吧. 最开始接触函数式编程的时候,第一个接触的概念就是高阶函数,和柯里化.咋一看,这不就是长期用来 ...

  5. JS黑科技

    1.论如何优雅的取随机字符串 Math.random().toString(16).substring(2) // 13位 Math.random().toString(36).substring(2 ...

  6. 客户端connect返回错误显示No route to host

    务器程序运行起来后,客户端connect返回错误显示No route to host,但是两台机子能ping通 是firewall的问题, services iptables stop应该就ok了

  7. python流程控制和循环

    变量的命名:可以由数字字母下换线组成 ,不能以数字开头,可以使用中文但是不推荐使用中文,不推荐前面使用_ __,不能使用系统的关键字,变量名严格区分大小写 逻辑运算优先级 or<and<n ...

  8. 使用Data Lake Analytics从OSS清洗数据到AnalyticDB

    前提 必须是同一阿里云region的Data Lake Analytics(DLA)到AnalyticDB的才能进行清洗操作: 开通并初始化了该region的DLA服务: 开通并购买了Analytic ...

  9. Hdu 4493

    题目链接 注意四舍五入,保留到小数点后两位(如果存在的话). 附上代码: /************************************************************** ...

  10. hdu1536 sg打表

    标记数组用bool型防止超时.输入的f[ ]要排序. #include<stdio.h> #include<string.h> #include<algorithm> ...