ACM刷题之路(五)最短路 Dijkstra POJ2387

简介: ACM刷题之路(五)最短路 Dijkstra POJ2387

题目网址http://poj.org/problem?id=2387

Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.


Farmer John's field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.


Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.

Input

* Line 1: Two integers: T and N


* Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.

Output

* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.

Sample Input

1. 5 5
2. 1 2 20
3. 2 3 30
4. 3 4 20
5. 4 5 20
6. 1 5 100

Sample Output

90

题意

就是第一行n,m

n代表有几条路,m代表1到m

求最短路径

先介绍一下Dijkstra算法

大概意思就是先找出1到各个结点的距离,保存在dis数组;然后在这些结点中找出与1距离最小的结点,判断它能不能作为中转站,使它作为中转站的情况下使得距离变得更小。

比如1到2的路是10,2到3的路是20,1到3的距离是50;先遍历从1开始的路,即dis[1]=0,dis[2]=10,dis[3]=50,然后到2的距离最小(因为1到1的距离是0已经确定),所以判断把2作为中转站的情况下能否使得1到别的结点的距离变得更小,最后更新1到3的距离是30,即dis[3]=30。这是我个人理解的Dijkstra算法。

ps:vis数组代表判断是否被访问过,有一股回溯法的感觉。


1. #include <iostream>
2. #include <cstdio>
3. #include <cstring>
4. #include <algorithm>
5. #define MAX 9999999
6. using namespace std;
7. 
8. int u, v, n, dis[1111], vis[1111], ma[1111][1111];
9. //有u条路,1到v个点 dis表示0到dis[i]的最短距离 
10. //vis判断是否访问 ma数组储蓄各点之间的路长
11. int max(int x, int y){
12.   if (x > y) return x;
13.   return y;
14. }
15. void dijk(){
16.   int i, j, k, mini;
17.   for (i = 1; i <= v; i++){
18.     dis[i] = ma[1][i];    //首先让1可以通的路长赋值给dis数组
19.   }
20.   for (i = 1; i <= v; i++){
21.     mini = MAX;
22.     for (j = 1; j <= v; j++) {//选出未确定最短路径中最短的结点
23.       if (!vis[j] && dis[j] < mini){
24.         mini = dis[j];
25.         k = j;//k就是最短的结点
26.       }
27.     }
28.     vis[k] = 1;   //设置k被访问过
29.     for (j = 1; j <= v; j++){
30.       if (dis[j] > dis[k] + ma[k][j]) {//如果k当中转站的距离比原来的小
31.         dis[j] = dis[k] + ma[k][j];
32.       }
33.     }
34.   }
35. }
36. int main(){
37.   int i, j;
38.   while (cin >> u >> v){
39.     n = 0;
40.     for (i = 0; i <= v; i++){
41.       for (j = 0; j <= v; j++){
42.         ma[i][j] = MAX;//所有数初始化为无穷大
43.       }
44.       ma[i][i] = 0;      //i到i的距离为0
45.       vis[i] = 0;     //表示i结点还没有被访问过
46.       dis[i] = MAX;     //初始化0到i结点的距离为无穷
47.     }
48.     for (i = 1; i <= u; i++){
49.       int a, b, len;
50.       cin >> a >> b >> len;
51.       n = max(max(n, a), b);    //n是要求最大的点,以便后面搜索
52.       if (ma[a][b] > len) {   //如果len小于a到b的距离,即为最优解
53.         ma[a][b] = ma[b][a] = len;
54.       }
55.     }
56.     dijk();
57.     printf("%d\n", dis[v]);
58.   }
59.   return 0;
60. }

 


相关文章
|
消息中间件 监控 Java
Elasticsearch 出现 “429 rejected” 报错,怎么办?
Elasticsearch 出现 “429 rejected” 报错,怎么办?
|
Java Linux Maven
使用nexus上传jar包图文教程
使用nexus上传jar包图文教程
2587 0
使用nexus上传jar包图文教程
|
2天前
|
云安全 人工智能 安全
AI被攻击怎么办?
阿里云提供 AI 全栈安全能力,其中对网络攻击的主动识别、智能阻断与快速响应构成其核心防线,依托原生安全防护为客户筑牢免疫屏障。
|
12天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
6天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
483 201
|
4天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
603 157
|
10天前
|
人工智能 自然语言处理 安全
国内主流Agent工具功能全维度对比:从技术内核到场景落地,一篇读懂所有选择
2024年全球AI Agent市场规模达52.9亿美元,预计2030年将增长至471亿美元,亚太地区增速领先。国内Agent工具呈现“百花齐放”格局,涵盖政务、金融、电商等多场景。本文深入解析实在智能实在Agent等主流产品,在技术架构、任务规划、多模态交互、工具集成等方面进行全维度对比,结合市场反馈与行业趋势,为企业及个人用户提供科学选型指南,助力高效落地AI智能体应用。