poj2387 Til the Cows Come Home 最短路径dijkstra算法
Description
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
* 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
Sample Input
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
Sample Output
90 dijkstra算法模板题;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define inf 0xfffffff
using namespace std;
int p[][],flag[],dis[],mmin,n,t;
int dijkstra()
{
memset(flag,,sizeof(flag));
for(int i=;i<=n;i++)
{
dis[i]=p[][i];
}
flag[]=;
int pre;
for(int i=;i<=n;i++)
{
mmin=inf;
for(int j=;j<=n;j++)
{
if(flag[j]==&&dis[j]<mmin)
{
mmin=dis[j];
pre=j;
}
}
if(mmin==inf)break;
flag[pre]=;
for(int j=;j<=n;j++)
{
if(flag[j]==&&dis[pre]+p[pre][j]<dis[j])
{
dis[j]=dis[pre]+p[pre][j];
}
}
}
}
int main()
{
int a,b,l;
while(scanf("%d%d",&t,&n)!=EOF)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++)
{
p[i][j]=inf;
}
}
for(int i=;i<t;i++)
{
scanf("%d%d%d",&a,&b,&l);
if(l<p[a][b])
{
p[a][b]=p[b][a]=l;
}
}
dijkstra();
printf("%d\n",dis[n]);
}
return ;
}
poj2387 Til the Cows Come Home 最短路径dijkstra算法的更多相关文章
- POJ2387 Til the Cows Come Home(SPFA + dijkstra + BallemFord 模板)
Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 37662 Accepted ...
- poj2387 Til the Cows Come Home(Dijkstra)
题目链接 http://poj.org/problem?id=2387 题意 有n个路标,编号1~n,输入路标编号及路标之间相隔的距离,求从路标n到路标1的最短路径(由于是无向图,所以也就是求从路标1 ...
- 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 ...
- 网络最短路径Dijkstra算法
最近在学习算法,看到有人写过的这样一个算法,我决定摘抄过来作为我的学习笔记: <span style="font-size:18px;">/* * File: shor ...
- 单源最短路径Dijkstra算法,多源最短路径Floyd算法
1.单源最短路径 (1)无权图的单源最短路径 /*无权单源最短路径*/ void UnWeighted(LGraph Graph, Vertex S) { std::queue<Vertex&g ...
- 最短路径-Dijkstra算法与Floyd算法
一.最短路径 ①在非网图中,最短路径是指两顶点之间经历的边数最少的路径. AE:1 ADE:2 ADCE:3 ABCE:3 ②在网图中,最短路径是指两顶点之间经历的边上权值之和最短的路径 ...
- 数据结构实验之图论七:驴友计划 ( 最短路径 Dijkstra 算法 )
数据结构实验之图论七:驴友计划 Time Limit: 1000 ms Memory Limit: 65536 KiB Submit Statistic Discuss Probl ...
- 最短路径——Dijkstra算法以及二叉堆优化(含证明)
一般最短路径算法习惯性的分为两种:单源最短路径算法和全顶点之间最短路径.前者是计算出从一个点出发,到达所有其余可到达顶点的距离.后者是计算出图中所有点之间的路径距离. 单源最短路径 Dijkstra算 ...
随机推荐
- jquery fadeOut 异步
1. 概述 jquery实现动画效果的函数使用起来很方便,不过动画执行是异步的, 所以要把自定义的操作放在回调函数里. 2. example <html> <body> < ...
- 利用jquery实现网页禁止鼠标右键、禁止复制
很多时候,网站的内容辛苦写法被轻松复制,为了不让自己的劳动成果外流,可以利用禁止鼠标右键等方式保护自己的原创内容! 方式1:禁止鼠标右键操作 <script src="http://l ...
- jquery.ajax error调试
$(document).ready(function() { jQuery("#clearCac").click(function() { jQuery.ajax({ url: u ...
- 【GOF23设计模式】解释器模式 & 访问者模式
来源:http://www.bjsxt.com/ 一.[GOF23设计模式]_解释器模式.访问者模式.数学表达式动态解析库式 1.解释器模式Interpreter 2.访问者模式Visitor
- jquery 监控文本框键盘事件(回车事件),附常用keycode值。
$(function(){ $(".search").keydown(function(event) { ) { //执行操作 } }) ); 完整的 key press 过程分为 ...
- 通过FTP连接Azure上的网站
下载发布文件 使用记事本(或其他文本工具)打开 找到ftp连接地址以及用户名.密码 使用ftp工具进行连接 输入相应参数,连接即可
- 转:jQuery 常见操作实现方式
http://www.cnblogs.com/guomingfeng/articles/2038707.html 一个优秀的 JavaScript 框架,一篇 jQuery 常用方法及函数的文章留存备 ...
- android内存泄露调试,Heap,MAT
三.内存监测工具 DDMS --> Heap 无论怎么小心,想完全避免bad code是不可能的,此时就需要一些工具来帮助我们检查代码中是否存在会造成内存泄漏的地方.Android tools中 ...
- .NET下单文件的上传处理
ASP.NET的单文件上传使用控件 <asp:FileUpload ID="upmess" runat="server" Width="248p ...
- SqlServer int型转varchar型 出现*号
今天调一个bug,错误提示执行语句 * 附近有语法错误,看了存储过程半天没啥反应,我就更本没有* .打印了一下语句发现 where Mor_Id=* 仔细一看set @sqlupdate+=' whe ...