Til the Cows Come Home (dijkstra算法)
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
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90
Hint
INPUT DETAILS:
There are five landmarks.
OUTPUT DETAILS:
Bessie can get home by following trails 4, 3, 2, and 1.
题解:这个基本是可以套用dijkstra算法,并且需要注意双向赋值,别的基本没什么坑点
代码:
#include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
using namespace std;
const int Inf = 0x3f3f3f3f ;
const int MAXN = 2005;
int dis[MAXN];
int map[MAXN][MAXN];//用来存储图
bool vis[MAXN];//用来标记,避免重复搜
int n,m ;//n个点,m条边
// u 为单源点
void dijkstra(int u)//dijkstra的算法
{
int t = u;
dis[t] = 0 ;
vis[t] = true ;
for ( int i = 1 ; i <= n ; i ++ )
{
for ( int j = 1 ; j <= n ; j ++ )
{
if ( !vis[j] && map[t][j] + dis[t] < dis[j] )//判断直接近,还是间接近
{
dis[j] = map[t][j] + dis[t] ;
}
}
int mini = Inf ;
for ( int j = 1 ; j <= n ; j ++ )
{
if ( !vis[j] && dis[j] < mini )
{
mini = dis[j] ;
t=j;
}
}
vis[t] = true ;
}
}
void init()
{
memset(vis,false,sizeof(vis)) ; //初始化标记数组
for ( int i = 1 ; i <= n ; i ++ )
{
dis[i] = Inf ;
for ( int j = 1 ; j <= n ; j ++ )
{
map[i][j] = Inf ;
}
}
return ;
}
int main()
{
while (scanf("%d%d",&m,&n)!=EOF)
{
init();
memset(map,Inf,sizeof(map));//初始化图
for ( int i = 0 ; i < m ; i ++ )
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);//表示 u 到 v的距离为 w
if ( map[u][v] > w )
{
map[v][u] = map[u][v] = w ;
}
}
dijkstra(1);
cout<<dis[n]<< endl ;
}
return 0 ;
}
Til the Cows Come Home (dijkstra算法)的更多相关文章
- poj 2387 Til the Cows Come Home(dijkstra算法)
题目链接:http://poj.org/problem?id=2387 题目大意:起点一定是1,终点给出,然后求出1到所给点的最短路径. 注意的是先输入边,在输入的顶点数,不要弄反哦~~~ #incl ...
- POJ 2387 Til the Cows Come Home Dijkstra求最短路径
Til the Cows Come Home Bessie is out in the field and wants to get back to the barn to get as much s ...
- POJ - Til the Cows Come Home(Dijkstra)
题意: 有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 分析: 典型的模板题,但是一定要注意有重边,因此需要对输入数据加以判断,保存较短的边,这样才能正确使用模板. ...
- Poj 2387 Til the Cows Come Home(Dijkstra 最短路径)
题目:从节点N到节点1的求最短路径. 分析:这道题陷阱比较多,首先是输入的数据,第一个是表示路径条数,第二个是表示节点数量,在 这里WA了四次.再有就是多重边,要取最小值.最后就是路径的长度的最大值不 ...
- POJ 2387 Til the Cows Come Home (Dijkstra)
传送门:http://poj.org/problem?id=2387 题目大意: 给定无向图,要求输出从点n到点1的最短路径. 注意有重边,要取最小的. 水题..对于无向图,从1到n和n到1是一样的. ...
- 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 ...
- Til the Cows Come Home(poj 2387 Dijkstra算法(单源最短路径))
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32824 Accepted: 11098 Description Bes ...
- 怒学三算法 POJ 2387 Til the Cows Come Home (Bellman_Ford || Dijkstra || SPFA)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33015 Accepted ...
- (原创)最短路径-Dijkstra算法,以Til the Cows Come Home为例
(1)首先先解释一下单源最短路径: 1)容易的解释:指定一个点(源点)到其余各个顶点的最短路径,也叫做“单源最短路径” 2)官方解释:给定一个带权有向图G=(V,E),其中每条边的权是一个实数.另外, ...
随机推荐
- IntelliJ IDEA 导入Project
一.方式一 File---->Close Project 这样的户每次需要import,都要close一次,非常不方便,如果能在File下面岂不是更好? 二.方式二 File---->Se ...
- oracle 之sys 、system区别
角色 1)最重要的区别,存储的数据的重要性不同sys--所有oracle的数据字典的基表和视图都存放在sys用户中,这些基表和视图对于oracle的运行是至关重要的,由数据库自己维护,任何用户都不能手 ...
- 4-5 父节点watcher事件
三种方式设置watcher:ls.stat.get
- 解决swfupload改变display属性后flash重新加载的问题(chome,safari内核的所有浏览器)
最近在做的项目中有要用到上传控件,所有就用到了swfupload flash上传控件 因为在项目中要使用到Tab控件,tab控件通过改变display属性来控制tab页的显 示与隐藏.当swfuplo ...
- c语言学习笔记-变量、变量的命名、变量的赋值和变量的初始化
在学习了简单的输入输出功能和了解了一些基本的运算符号之后我们可以试着做一个非常简单的计算器. 比如说想计算23+65 输入以下代码就可以了. printf("23+65=%d",2 ...
- c++正确处理 is-a has-a关系
比如.我们想实现一个Set类,而已经有一个List类可提供给你使用,我们到底用is-a(public继承)关系还是用has-a(组合)关系呢? 1:如果使用is-a关系,则 class Set:pub ...
- 无法认识patch请求
Content-Type: application/vnd.api+jsonbase64:账号密码的设置总结:该及时消化的知识,就应该当时消化.不能拖.注意细节,一个小细节的疏忽,导致自己几乎一天的时 ...
- ObjectARX杂碎--(学习指南及书籍)
---------------------------------------------------------------------------------------------------- ...
- DataGridView增加全选列
最近的一个winform的项目中,碰到datagridview控件的第一列添加全选的功能,通常这个功能,有两种实现方式:1. 为控件添加DataGridViewCheckBoxColumn来实现,但是 ...
- C++: I/O流详解(三)——串流
一.串流 串流类是 ios 中的派生类 C++的串流对象可以连接string对象或字符串 串流提取数据时对字符串按变量类型解释:插入数据时把类型 数据转换成字符串 串流I/O具有格式化功能 例: #i ...