zoukankan      html  css  js  c++  java
  • [题解] hdu 1596 find the safest road (dijkstra最短路)

    - 传送门 -

     http://acm.hdu.edu.cn/showproblem.php?pid=1596
     

    # find the safest road

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 14211    Accepted Submission(s): 5004

    Problem Description

    XX星球有很多城市,每个城市之间有一条或多条飞行通道,但是并不是所有的路都是很安全的,每一条路有一个安全系数s,s是在 0 和 1 间的实数(包括0,1),一条从u 到 v 的通道P 的安全度为Safe(P) = s(e1)s(e2)…s(ek) e1,e2,ek是P 上的边 ,现在8600 想出去旅游,面对这这么多的路,他想找一条最安全的路。但是8600 的数学不好,想请你帮忙 _

    Input

    输入包括多个测试实例,每个实例包括:
    第一行:n。n表示城市的个数n<=1000;
    接着是一个n*n的矩阵表示两个城市之间的安全系数,(0可以理解为那两个城市之间没有直接的通道)
    接着是Q个8600要旅游的路线,每行有两个数字,表示8600所在的城市和要去的城市

    Output

    如果8600无法达到他的目的地,输出"What a pity!",
    其他的输出这两个城市之间的最安全道路的安全系数,保留三位小数。

    Sample Input

    3
    1 0.5 0.5
    0.5 1 0.4
    0.5 0.4 1
    3
    1 2
    2 3
    1 3

    Sample Output

    0.500
    0.400
    0.500

    [Statistic](http://acm.hdu.edu.cn/statistic.php?pid=1596) | [Submit](http://acm.hdu.edu.cn/submit.php?pid=1596) | [Discuss](http://acm.hdu.edu.cn/discuss/problem/list.php?problemid=1596) | [Note](http://acm.hdu.edu.cn/note/note.php?pid=1596)
      ### - 题意 -  求两点间安全度最大的路.  安全度的定义: 经过路径的安全度之积.  (所有路径的安全度属于[0,1], 0 表示不连通)   ### - 思路 -  注意一下精度问题, 其它的就和最短路没什么区别了.    细节见代码.

     PS:
     n = 1000 竟然可以用Floyd卡过去好气哦.
     

    - 代码 -

    #include<cstdio>
    #include<queue>
    #define ft first
    #define sd second
    using namespace std;
    
    typedef double db;
    typedef pair<double, int> pdi;
    const int N = 1e3 + 5;
    const int inf = 0x3f3f3f3f;
    
    db G[N][N], DIS[N];
    int VIS[N];
    int n, q;
    
    
    void init(int x) {
    	for (int i = 1; i <= n; ++i)
    		DIS[i] = VIS[i] = 0;
    	DIS[x] = 1;
    }
    
    db dijkstra(int bg, int ed) {
    	priority_queue<pdi>Q;
    	init(bg);
    	pdi st;
    	st.ft = DIS[bg];
    	st.sd = bg;
    	Q.push(st);
    	while (!Q.empty()) {
    		pdi x = Q.top();
    		Q.pop();
    		if (x.sd == ed) break;
    		if (VIS[x.sd]) continue;
    		VIS[x.sd] = 1;
    		for (int i = 1; i <= n; ++i) {
    			if (!VIS[i] && DIS[i] < DIS[x.sd] * G[i][x.sd]) {
    				DIS[i] = DIS[x.sd] * G[i][x.sd];
    				pdi y;
    				y.ft = DIS[i];
    				y.sd = i;
    				Q.push(y);
    			}
    		}
    	}
    	return DIS[ed];
    }
    
    int main() {
    	while (scanf("%d", &n) != EOF) {
    		for (int i = 1; i <= n; ++i)
    			for (int j = 1; j <= n; ++j)
    				scanf("%lf", &G[i][j]);
    		scanf("%d", &q);
    		for (int i = 1, x, y; i <= q; ++i) {
    			scanf("%d%d", &x, &y);
    			db ans = dijkstra(x, y);
    			if(ans >= 1e-7) printf("%.3lf
    ", ans);      //ans<1e-7时视作0, 所以不能写ans == 0
    			else puts("What a pity!");
    		}
    	}
    	return 0;
    }
    
  • 相关阅读:
    ubuntu14.04通过将语音,耳机没有声音
    网络编程easy错误点-手知道
    如何实现网页在线咨询框架
    C++定义自己的命名空间和头文件
    使用光标
    工作日两个日期之间的数
    Swift
    Swift
    Swift
    Swift
  • 原文地址:https://www.cnblogs.com/Anding-16/p/7387926.html
Copyright © 2011-2022 走看看