poj2387 Til the Cows Come Home(Dijkstra)
题目链接
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)的更多相关文章
- Til the Cows Come Home(Dijkstra)
Dijkstra (迪杰斯特拉)最短路算法,算是模板 POJ - 2387 #include<iostream> #include<algorithm> #include< ...
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- POJ 2387 Til the Cows Come Home(dijkstra裸题)
题目链接:http://poj.org/problem?id=2387 题目大意:给你t条边(无向图),n个顶点,让你求点1到点n的最短距离. 解题思路:裸的dijsktra,注意判重边. 代码: # ...
- 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 ...
- 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 ...
- POJ2387 Til the Cows Come Home 【Dijkstra】
题目链接:http://poj.org/problem?id=2387 题目大意; 题意:给出两个整数T,N,然后输入一些点直接的距离,求N和1之间的最短距离.. 思路:dijkstra求单源最短路, ...
- 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 ...
- 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 ...
- 迪杰斯特拉(dijkstra)算法的简要理解和c语言实现(源码)
迪杰斯特拉(dijkstra)算法:求最短路径的算法,数据结构课程中学习的内容. 1 . 理解 算法思想::设G=(V,E)是一个带权有向图,把图中顶点集合V分成两组,第一组为已求出最短路径的顶点集合 ...
随机推荐
- webpack的基础入门
webpack的基础入门 这里对于 webpack 的基础入门进行一些总结,可以参考 github 上的 webpack-demo ,链接是 https://github.com/RealAndMe/ ...
- LibreOJ#6030. 「雅礼集训 2017 Day1」矩阵
https://loj.ac/problem/6030 如果矩阵第i列有一个黑色, 那可以用他把第i行全都染黑,也可以使任意一列具有黑色 然后就可以用第i行把矩阵染黑 染黑一列的代价最少是1 染黑一行 ...
- Spring MVC处理响应的 header
我们经常需要在HttpResponse中设置一些headers,我们使用Spring MVC框架的时候我们如何给Response设置Header呢? So easy, 看下面的代码: @Request ...
- 20155306 2016-2017-2 《Java程序设计》第6周学习总结
20155306 2016-2017-2 <Java程序设计>第6周学习总结 教材学习内容总结 第十章 输入/输出 10.1 InputStream与OutputStream 如果要将数据 ...
- c++刷题(18/100)树
题目1:二叉搜索树的第k个节点 给定一颗二叉搜索树,请找出其中的第k小的结点.例如, 5 / \ 3 7 /\ /\ 2 4 6 8 中,按结点数值大小顺序第三个结点的值为4. 思路:因为是二叉搜索树 ...
- Informatica学习:2、配置存储库服务和集成服务
继续上一篇的1.安装介质的获取与安装,本文介绍服务端的存储库服务和集成服务的配置. 安装好Informatica的客户端和服务端后,需要登陆Administration Console,配置存储库服务 ...
- 20165227朱越 预备作业3 Linux安装及学习
预备作业3 Linux安装及学习 Linux的安装 虚拟机的安装远没有想象中的那样容易,下载还没有出现什么问题,当我安装的时候,第一个问题出现在创建虚拟机时选择安装的虚拟机版本和类型的时候的错误 当时 ...
- CSS ... 文本溢出用省略号代替
{ overflow:hidden; text-overflow:ellipsis; white-space:nowrap } text-overflow 属性规定当文本溢出包含元素时发生的事情. c ...
- C# 执行固定个数任务自行控制进入线程池的线程数量,多任务同时但是并发数据限定
思路来源:http://bbs.csdn.NET/topics/390819824,引用该页面某网友提供的方法. 题目:我现在有100个任务,需要多线程去完成,但是要限定同时并发数量不能超过5个. 原 ...
- 转载: Android开源库V - Layout:淘宝、天猫都在用的UI框架,赶紧用起来吧!
阿里的UI库... 分析的很精辟... http://blog.csdn.net/carson_ho/article/details/71077193