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. 二维vector基本使用

    变量声明 vector<vector<int> > 变量名: 添加行 vector<vector<int> > v2d; for(int i=0;i&l ...

  2. Pycharm安装package报错:AttributeError: module 'pip' has no attribute 'main'

    Pycharm安装package报错:AttributeError: module 'pip' has no attribute 'main' 确认pip已经升级到目前最新版本了. 在网上搜寻后,解决 ...

  3. java贪吃蛇小游戏详解

    https://blog.csdn.net/u011622021/article/details/81162083

  4. python 对位运算

  5. Python 运算符首尾匹配

  6. 【滴水石穿】rn

    这个项目还不错,还比较全 先放项目地址:https://github.com/ShionHXC/rn 项目算是一个完整的APP 有用到redux-thunk存储数据,算的上是一个普通的比较完整的APP ...

  7. JDBC的事务处理 JDBC事务处理 JDBC教程

    JDBC的事务基本知识 事务的定义:一个事务是由一条或多条对数据库操作的sql语句所组成的一个不可分割的工作单元,只有当事务中的所有操作都正常执行后,整个事务才会提交给数据库. 结束事务的操作:com ...

  8. jQuery打飞机游戏

    在线演示 本地下载

  9. CSS面试题总结2(转)

    1.你最喜欢的图片替换方法是什么,你如何选择使用. 图像替代,就是像我们在平时将文本添加到文本中,然后通过css隐藏文本并在它的位置上显示一个背景图片,这样,搜索引擎仍然可以搜到HTML文本,即使我们 ...

  10. 设备 VMnet0 上的网络桥接当前未在运行。

    早上,我打开我的虚拟机,却发现一个问题, 桥接网络怎么都连接不上. 报的是如下的错误 ------------------------------ 设备 VMnet0 上的网络桥接当前未在运行.该虚拟 ...