题目连接:

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

Description

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.

题意描述:
最短路水题。
解题思路:
处理数据,使用迪杰斯特拉算法。
AC代码:
 #include<stdio.h>
#include<string.h>
int e[][],dis[],bk[];
int main()
{
int i,j,min,t,t1,t2,t3,n,u,v;
int inf=;
while(scanf("%d%d",&t,&n)!=EOF)
{
for(i=;i<=n;i++)
{
for(j=;j<=n;j++)
{
if(i==j)
e[i][j]=;
else
e[i][j]=inf;
}
}
for(i=;i<=t;i++)
{
scanf("%d%d%d",&t1,&t2,&t3);
if(e[t1][t2]>t3)
{
e[t1][t2]=t3;
e[t2][t1]=t3;
}
}
for(i=;i<=n;i++)
dis[i]=e[][i];
memset(bk,,sizeof(bk));
bk[]=;
for(i=;i<=n-;i++)
{
min=inf;
for(j=;j<=n;j++)
{
if(bk[j]==&&dis[j]<min)
{
min=dis[j];
u=j;
}
}
bk[u]=;
for(v=;v<=n;v++)
{
if(e[u][v]<inf && dis[v]>dis[u]+e[u][v])
dis[v]=dis[u]+e[u][v];
}
}
printf("%d\n",dis[n]);
}
return ;
}
 

POJ 2387 Til the Cows Come Home(模板——Dijkstra算法)的更多相关文章

  1. 怒学三算法 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 ...

  2. 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 ...

  3. (简单) 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 ...

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

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

  5. 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 ...

  6. POJ.2387 Til the Cows Come Home (SPFA)

    POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...

  7. POJ 2387 Til the Cows Come Home

    题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K ...

  8. POJ 2387 Til the Cows Come Home --最短路模板题

    Dijkstra模板题,也可以用Floyd算法. 关于Dijkstra算法有两种写法,只有一点细节不同,思想是一样的. 写法1: #include <iostream> #include ...

  9. POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)

    传送门 Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 46727   Acce ...

  10. POJ 2387 Til the Cows Come Home (最短路 dijkstra)

    Til the Cows Come Home 题目链接: http://acm.hust.edu.cn/vjudge/contest/66569#problem/A Description Bessi ...

随机推荐

  1. 4.Nginx的URL重写应用

    Nginx的URL重写应用 nginx的URL重写模块是用得比较多的模块之一,所以我们需要好好地掌握运用.常用的URL重写模块命令有if,rewrite,set,break等. if命令 if用于判断 ...

  2. Dubbo(一) 开始认识Dubbo,分布式服务框架

    引言: 以前的车马很慢,一生只够爱一个人以前的网站人很少,一个单应用服务着一个人--------------------现在,动不动就谈什么高并发,千万级访问.单应用?BOOM!分分钟爆炸.于是,技术 ...

  3. 安装MySQL时提示3306端口已被占用的解决方案

    之前安装过mysql,用的好好的,但是今天开启服务时报异常,无法启动.为省事,于是想到卸载重装,安装过程中发现3306已经被占用,这也是一开始服务无法启动的原因. 看到有人说用fport查看端口号,于 ...

  4. 小程序语音红包中遇到的 语音识别silk转wav格式 如何在线转 或者mp3转wav格式

    公司在开发一个小程序语音红包,现在遇到的问题就是通过微信的小程序文档接口拿到的录音文件要么是silk格式的,要么是mp3格式的 但是呢,如果要调用百度的语音接口,又必须是wav格式的.也就是说通过微信 ...

  5. 这些年常用的WEB开发工具和技术, 学会一半你找工作没问题

    前言: 技术选型并不是一成不变的,需要根据技术的发展.项目实际情况和人员技能构成实际考虑,在此列出的只是这些年常用的. 开发环境 1. 主要开发语言:Java7, HTML, Javascript等 ...

  6. NPOI 2.0 教程

    NPOI2.0帮助官方地址 目录 1. 前言 1.1 NPOI 2.0与NPOI 1.x的区别 1.2 NPOI 2.0模块简介 1.3 自动识别并打开Excel 2003和Excel 2007文件 ...

  7. 安装好的虚拟机,外部通过ssh工具连接,报connection failed

    今天,新装了一台ubuntu虚拟机,安装成功以后,准备利用Xshell从外部访问linux,以减少切换,但是,在连接时,总是会报:connection failed. 于是,写下这篇随笔,以增加记忆且 ...

  8. Maven代理设置

    公司需要设置代理才能上网,而运行Maven时需要下载依赖的库. 怎么办呢? 原来Maven也像IE一样,可以设置HTTP代理的. 步骤如下: ·编辑 ~/.m2/setting.xml 文件.如果该目 ...

  9. mimtproxy和arpspoof实现局域网MITM

    本地环境 环境:kali系统 目标机器:192.168.0.101 局域网网关:192.168.0.1 当前网络网卡端口:wlan0 arp欺骗流程 命令行开启本地数据转发: echo > /p ...

  10. MongoDB批量导入及简单的性能优化

    今天简单分享一下MongoDB使用过程中的一些性能优化,其实并不只适用MongoDB,其他数据库多少也可适用. 首先先随机导入一千万条数据.这里我分段导入的,因为mongo的BsonDocument一 ...