题目链接

http://poj.org/problem?id=2387

题意

有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1到路标n的最短路径)。

思路

最短路径问题,由于结点数目最多可以有1000个,用Floyd算法应该会超时,而且做了之后发现输入会有相同的边,使用SPFA算法会麻烦一些,所以这里使用Dijkstra算法求解。

代码

 #include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int INF = 0x3f3f3f;
const int N = + ;
int map[N][N];
int dist[N];
int visit[N];
int n, t; void dijkstra()
{
for (int i = ; i <= n; i++)
dist[i] = map[][i];
dist[] = ;
int min_dist, now = ;
for (int i = ; i <= n; i++)
{
visit[now] = ;
min_dist = INF;
for (int j = ; j <= n; j++)
{
if (!visit[j] && dist[j] < min_dist)
{
min_dist = dist[j];
now = j;
}
}
visit[now] = ;
for (int j = ; j <= n; j++)
dist[j] = min(dist[j], dist[now] + map[now][j]);
}
printf("%d\n", dist[n]);
} int main()
{
//freopen("poj2387.txt", "r", stdin);
scanf("%d%d", &t, &n);
memset(map, INF, sizeof(map));
int a, b, d;
for (int i = ; i < t; i++)
{
scanf("%d%d%d", &a, &b, &d);
if(map[a][b] > d) //对于相同的边,取权值最小的那条
map[a][b] = map[b][a] = d;
}
dijkstra();
return ;
}

poj2387 Til the Cows Come Home(Dijkstra)的更多相关文章

  1. Til the Cows Come Home(Dijkstra)

    Dijkstra (迪杰斯特拉)最短路算法,算是模板 POJ - 2387 #include<iostream> #include<algorithm> #include< ...

  2. POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)

    Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 37662   Accepted ...

  3. POJ 2387 Til the Cows Come Home(dijkstra裸题)

    题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...

  4. POJ 2387 Til the Cows Come Home (dijkstra模板题)

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  5. poj2387 Til the Cows Come Home 最短路径dijkstra算法

    Description Bessie is out in the field and wants to get back to the barn to get as much sleep as pos ...

  6. POJ2387 Til the Cows Come Home 【Dijkstra】

    题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...

  7. POJ2387 Til the Cows Come Home (最短路 dijkstra)

    AC代码 POJ2387 Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to ...

  8. POJ 2387 Til the Cows Come Home (图论,最短路径)

    POJ 2387 Til the Cows Come Home (图论,最短路径) Description Bessie is out in the field and wants to get ba ...

  9. 迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)

    迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合 ...

随机推荐

  1. python 日期时间处理

    # 获取日期: import datetime #调用事件模块 today =datetime.date.today() #获取今天日期 deltadays =datetime.timedelta(d ...

  2. td自动换行

    自动换行方法: 1.在<td>中设置样式style为word-wrap:break-word;word-break:break-all; (一般情况只需要设置word-break:brea ...

  3. 【译】第一篇 Integration Services:SSIS是什么

    本篇文章是Integration Services系列的第一篇,详细内容请参考原文. Integration Services是一种在SQL Server中最受欢迎的子系统.允许你在各种数据源之间提取 ...

  4. 关于Python编码问题小记

    Python编码问题小记: 引子: 最近在复习redis,当我在获取redis的key的时候,redis 存储英文和汉字下面这个样子的,我知道汉字是用16进制的UTF-8编码了,然后突然很想搞清楚字符 ...

  5. windows 下安装mysqlclient 包

    正常情况下是可以直接用 pip install mysqlclient 进行安装的.如果你的机器上安装的既有python3 又有python2.7 的话,建议使用python -m pip insta ...

  6. Shell-判断条件总结

    -b file 若文件存在且是一个块特殊文件,则为真 -c file 若文件存在且是一个字符特殊文件,则为真 -d file 若文件存在且是一个目录,则为真 -e file 若文件存在,则为真 -f ...

  7. NuGet套件还原步骤(以vs2012为例)

    下载别人的范例,出现由于Nuget套件不存在而无法启动时: 效果如下图: 步骤如下: 1.点击 项目->启用NuGet程序包还原 2.点击下图中的是 3.点击下图中的确定 4.效果如图: . 5 ...

  8. STM8CubeMx来了

    几年前出来的STM32CubeMx是众多stm32开发者的福音,大大缩短了开发者的开发周期.就在前几天,st官网宣布针对stm8的图形配置工具stm8cube横空出世. 如果你还不知道STM32Cub ...

  9. SSD的SLC MLC 和TLC哪个好?

    <1>SLC = Single-Level Cell ,即1bit/cell,速度快寿命长,价格超贵(约MLC 3倍以上的价格),约10万次擦写寿命MLC = Multi-Level Ce ...

  10. 使用MongoDB命令工具导出、导入数据

    Windows 10家庭中文版,MongoDB 3.6.3, 前言 在前面的测试中,已经往MongoDB的数据库中写入了一些数据.现在要重新测试程序,数据库中的旧数据需要被清理掉,可是,又想保存之前写 ...