hdu 1596 find the safest road (dijkstra)
来源:互联网 发布:织梦5.7sp2最新漏洞 编辑:程序博客网 时间:2024/06/04 18:08
find the safest road
题目链接:Problem - 1596
题目大意:略
题目分析:水题,此题数据少,属于稠密图,可以用dijkstra做
思路分析:
1)double 数组存图
2)读取出发点和到达点,用dijkstra算法计算出最优路径
3)若路径不存在,输出“What a pity!"
贴AC代码:
#include <stdio.h> //定义输入/输出函数#include <limits.h> //定义各种数据类型最值常量#include <math.h> //定义数学函数#include <stdlib.h> //定义杂项函数及内存分配函数#include <string.h> //字符串处理#include <algorithm>//算法#include <queue>//队列#include <stack>//栈#include <vector>using namespace std;int n, Q, from, to, tmp;double map[1005][1005];double low[1005];bool vis[1005];void dijkstra(){ memset(low, 0, sizeof(low)); memset(vis, 0, sizeof(vis)); for (int i = 1; i <= n; i++){ if (map[from][i] == 0) continue; low[i] = map[from][i]; } vis[from] = 1; for (int i = 0; i < n - 1; i ++){ double maxs = 0; for (int j = 1; j <= n; j++){ if (low[j] > maxs && vis[j] == 0){ maxs = low[j]; tmp = j; } } vis[tmp] = 1; //printf("::%d || tmp = %d\n", i, tmp); //for (int k = 1; k <= n;k ++) // printf("%.3f ", low[k]); //printf("\n\n"); for (int j = 1; j <= n; j++){ if (map[tmp][j] == 0) continue; if (low[j] < low[tmp] * map[tmp][j]) low[j] = low[tmp] * map[tmp][j]; } }}int main(){ while (scanf("%d", &n) != EOF && n){ for (int i = 1; i <= n; i ++) for (int j = 1; j <= n; j++) scanf("%lf", &map[i][j]); scanf("%d", &Q); while (Q--){ scanf("%d%d", &from, &to); dijkstra(); if (low[to] != 0.0) printf("%.3f\n", low[to]); else printf("What a pity!\n"); } }}
0 0
- hdu 1596find the safest road--dijkstra
- HDU 1596 find the safest road(Dijkstra)
- hdu 1596 find the safest road (dijkstra)
- hdu 1596 find the safest road(dijkstra||floyd)
- HDU 1596 find the safest road【dijkstra & floyed & SPFA 】
- hdu 1596 find the safest road dijkstra || floyd
- HDU-1596 find the safest road(最短路[Dijkstra])
- HDU 1596:find the safest road【Dijkstra & SPFA】
- HDU 1596 find the safest road dijkstra&&SPFA
- hdu-1596-find the safest road(dijkstra)
- find the safest road HDU杭电1596【Dijkstra || SPFA】
- HDU Problem 1596 find the safest road【最短路dijkstra】
- hdu 1596 find the safest road (dijkstra)
- hdoj 1596 find the safest road 【dijkstra】
- hdoj 1596 find the safest road【dijkstra】
- 【HDU】-find the safest road(最短路)(dijkstra)
- hdu 1596 find the safest road(priority_queue )
- HDU 1596 find the safest road
- NGUI研究院之Sprite精灵与精灵动画的使用(二)
- static和extern
- Python 学习(3)---Python标准异常总结
- 嵌入式开发 tftp使用方法
- pthread_cancel的带来的一个死锁问题(验证)
- hdu 1596 find the safest road (dijkstra)
- 利用mysql里的show global status和show variables来优化mysql的配置参数
- 《ArcGIS Runtime SDK for Android开发笔记》——(7)、示例代码arcgis-runtime-samples-android的使用
- 运算符重载 - 判断平行
- Android各种访问权限Permission详解
- onSaveInstanceState和onRestoreInstanceState
- weibo feed
- maven的使用
- 读书笔记4——《用户故事与敏捷方法》之Scrum