HDU-2544 最短路 Dijkstra模板题
题目链接:https://vjudge.net/problem/HDU-2544
题意:
题目要求找到节点1到节点n之间的一条最短路
分析:
Dijkstra模板题
单源最短路径,可以用dijkstra(当然Floyd或者其他也可以),首先初始化节点间距离数组map和访问记录数组vis,然后录入并存储当前已知点间距离,再使用dijsktra算法以起始点为中心向外层层扩展(广度优先搜索思想),不断更新最短距离,直到扩展到终点为止。最后得到的dis[n]即为起点1至终点n的最短距离。
代码如下:
#include <cstdio>
#include<iostream>
#include <cstring>
#include <cmath>
#include <algorithm>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int maxn = ;
int map[maxn][maxn], dis[maxn];
bool vis[maxn];
int n, m; void init() {
for(int i = ; i <= n; i++) {
for(int j = ; j <= n; j++) {
if(i == j)
map[i][j] = ;
else
map[i][j] = INF;
}
}
memset(vis, , sizeof(vis));
} void dijkstra() {
for(int i = ; i <= n; i++) {
dis[i] = map[][i]; //dis表示起点1至点i的当前最短路径,初始为map[1][i]
}
for(int i = ; i <= n-; i++) {
int min = INF, p;
for(int j = ; j <= n; j++) { //在此循环中找到与1距离最短的节点
if(vis[j] == && dis[j] < min) { //如果点j没有访问过,且1->j距离最短
min = dis[j]; //更新当前最短距离
p = j; //更新最短距离节点
}
}
vis[p] = ; //标记p已被访问
for(int j = ; j <= n; j++) { //在此循环中根据之前找到的最短距离点
if(vis[j] == && dis[p]+map[p][j] < dis[j]) {//把其他点更新一遍
dis[j] = dis[p]+map[p][j]; //更新最短路径
}
}
}
} int main(void) {
while(scanf("%d%d", &n, &m) == && n+m) {
init();
int u, v, w;
for(int i = ; i <= m; i++) {
scanf("%d%d%d", &u, &v, &w);
if(w < map[u][v])
map[u][v] = map[v][u] = w;
}
dijkstra();
printf("%d\n", dis[n]);
}
}
HDU-2544 最短路 Dijkstra模板题的更多相关文章
- HDU 2544最短路dijkstra模板题
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU 2544 最短路(模板题——Floyd算法)
题目: 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以现在他们想要寻找最短的从商店到赛场的路线,你 ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题
hdu 2544 求点1到点n的最短路 无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...
- hdu 2544 最短路 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间, ...
- HDOJ/HDU 2544 最短路---dijkstra算法
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 这题的思路可以见这里(同一类型):http://blog.csdn.net/xiaozhuaix ...
- HDU 2544 最短路(dijkstra+邻接矩阵)
( ̄▽ ̄)" #include<iostream> #include<cstdio> using namespace std; const int INF=10e7; ...
- HDU 2544 最短路 【Dijkstra模板题】
传送门:http://acm.hdu.edu.cn/showproblem.php?pid=2544 思路:最短路的模板题 Dijkstra 算法是一种类似于贪心的算法,步骤如下: 1.当到一个点时, ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
随机推荐
- redis改配置
命令行: 暂时生效,适合于做测试,或者线上马上服务修改,重启失效 CONFIG set stop-writes-on-bgsave-error no OK CONFIG get stop-writes ...
- 剑指offer自学系列(五)
题目描述:请实现一个函数用来找出字符流中第一个只出现一次的字符.例如,当从字符流中只读出前两个字符"go"时,第一个只出现一次的字符是"g".当从该字符流中读出 ...
- pixi的图片处理
pixi的图片处理 var texture = PIXI.Texture.fromImage('sprite.png');var sprite = new PIXI.Sprite(texture) ...
- 19 03 02 HTTP和https
HTTP协议(HyperText Transfer Protocol,超文本传输协议):是一种发布和接收 HTML页面的方法. HTTPS(Hypertext Transfer Protocol ov ...
- 洛谷 三月月赛 B
搞出每一位与前一位的差,然后区间修改只是会影响区间的端点,所以只修改一下端点的值就好. %%%高一神犇线段树 #include<bits/stdc++.h> #define N 10000 ...
- 131-PHP子类可以访问父类public修饰的类成员
<?php class father{ //定义father类 public function cook(){ return '烹饪'; } } class son extends father ...
- sql编程利器,Sql Prompt下载及安装方法
Sql Prompt只能提示及其格式化用起来非常方便: 推荐网址:www.4-yecao.com 免费下载地址:http://download.csdn.net/detail/caizz520/455 ...
- 数论 CF27E Number With The Given Amount Of Divisors
求因子数一定的最小数(反素数) #include<iostream> #include<string> #include<cmath> #include<cs ...
- 报错SQL盲注之BIGINT 溢出
首先感谢原创博主,在此致敬.本文转自:http://www.cnblogs.com/lcamry/articles/5509112.html MySQL版本在 5.5.5 及其以上 0x01 概述 我 ...
- 课堂测试_WEB界面链接数据库
课堂测试_WEB界面链接数据库 一,题目: 一. 考试要求: 1登录账号:要求由6到12位字母.数字.下划线组成,只有字母可以开头:(1分) 2登录密码:要求显示“• ”或“*”表示输入位数,密码要求 ...