problem

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 <cstdio>
#include <cstdlib>
#include <algorithm>
#include <cstring>
using namespace std;
int m,n;
const int inf = 0x3f3f3f3f;
int dis[1005];
int gra[1005][1005];
int vis[1005];
void dj()
{
memset(vis,0,sizeof(vis));
int minn,v;
for(int i = 1; i <= n; i ++) dis[i] = gra[1][i];
for(int i = 1; i <= n; i ++)
{
minn = inf;
for(int j = 1; j <= n; j ++)
{
if(!vis[j] && dis[j] < minn)
{
minn = dis[j];
v = j;
}
}
vis[v] = 1;
for(int j = 1; j <= n; j ++)
{
if(gra[v][j] + dis[v] < dis[j] && !vis[j])
{
dis[j] = gra[v][j] + dis[v];
}
}
}
printf("%d\n",dis[n]);
}
int main()
{
int i,j,a,b,c;
while(~scanf("%d%d",&m,&n))
{
for(i = 1; i <= n; i ++)
{
for(j = 1; j <= n; j ++)
{
if(i == j) gra[i][j] = 0;
else gra[i][j] = gra[j][i] = inf;
}
}
for(i = 1; i <= m; i ++)
{
scanf("%d%d%d",&a,&b,&c);
if(gra[a][b] > c ) gra[a][b] = gra[b][a] = c;
}
dj();
}
return 0;
}

Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)的更多相关文章

  1. Til the Cows Come Home(poj 2387 Dijkstra算法(单源最短路径))

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 32824   Accepted: 11098 Description Bes ...

  2. (最短路 弗洛伊德) Til the Cows Come Home -- POJ --2387

      #include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> ...

  3. kuangbin专题专题四 Til the Cows Come Home POJ - 2387

    题目链接:https://vjudge.net/problem/POJ-2387 题意:从编号为n的城市到编号为1的城市的最短路. 思路:dijkstra模板题,直接套板子,代码中我会带点注释给初学者 ...

  4. POJ 2449 第k短路 Dijkstra+A*

    这道题我拖了半年,,,终于写出来了 思路: 先反向建边 从终点做一次最短路 ->这是估价函数h(x) 再正常建边,从起点搜一遍 (priority_queue(h(x)+g(x))) g(x)是 ...

  5. POJ 2387 Til the Cows Come Home

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

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

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

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

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

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

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

随机推荐

  1. java中的自动装箱和拆箱

    一.什么是自动装箱和拆箱: 我们知道java为8种基本类型分别提供了对应的包装类型,在Java SE5之前,如果要生成一个数值为10的Integer对象,必须这样进行: Integer i=new I ...

  2. 解决使用绝对定位absolute后,margin:0 auto居中方法失效(转)

    https://blog.csdn.net/qq_40678503/article/details/82780680

  3. (十二) web服务与javaweb结合(3)

    一.需求 上一章节虽然将webservice和web项目绑定在了一起,但是还是不能共同一个端口,本章讲解webservice和web项目绑定且共同端口. 二.案例 2.1 创建web工程,并引入依赖 ...

  4. wstngfw中配置squid

    wstngfw中配置squid Squid是一个缓存 Internet 数据的软件,其接收用户的下载申请,并自动处理所下载的数据.当一个用户想要下载一个主页时,可以向 Squid 发出一个申请,要 S ...

  5. 什么是DDOS

    什么是DDOS?分布式拒绝服务攻击(Distributed Denial of Service).百度的解释有一个形象的例子我认为比较好理解,照搬如下:   一群恶霸试图让对面那家有着竞争关系的商铺无 ...

  6. Linux下源码包安装Swoole及基本使用 转

    版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:https://blog.csdn.net/msllws/article/details ...

  7. css Cascading Style Sheet 层叠样式表

    作用 :实现网页布局,美化页面元素 CSS 在文档中的三种方式 1 行内样式/内联样式 特点:在具体的html标签中引入css 代码 语法: 所有的html标签都具有一个style属性,属性值就可以使 ...

  8. 操作系统 (OS)

    1. 操作系统(Operation System,OS) 操作系统作为接口的示意图 没有安装操作系统的计算机,通常被称为 裸机 如果想在 裸机 上运行自己所编写的程序,就必须用机器语言书写程序 如果计 ...

  9. 【转】Linux编译链接问题----静态库和动态库

    Linux静态库和动态库的命名规则 静态函数库 静态库的名字一般是libxxx.a,利用静态库编译生成的文件比较大,因为整个静态库所有的数据都会被整合进目标代码中. a) 优点: 编译后,可执行文件不 ...

  10. GNS3 介绍

    什么是GNS3? GNS3是一款模拟CISCO网络设备的模拟器,和CPT(Cisco Packet Tracer)相比.GNS3运行的是真实设备的IOS,命令集更全,在如有部分有非常好的表现,交换部分 ...