Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)
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)的更多相关文章
- Til the Cows Come Home(poj 2387 Dijkstra算法(单源最短路径))
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 32824 Accepted: 11098 Description Bes ...
- (最短路 弗洛伊德) Til the Cows Come Home -- POJ --2387
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> ...
- kuangbin专题专题四 Til the Cows Come Home POJ - 2387
题目链接:https://vjudge.net/problem/POJ-2387 题意:从编号为n的城市到编号为1的城市的最短路. 思路:dijkstra模板题,直接套板子,代码中我会带点注释给初学者 ...
- POJ 2449 第k短路 Dijkstra+A*
这道题我拖了半年,,,终于写出来了 思路: 先反向建边 从终点做一次最短路 ->这是估价函数h(x) 再正常建边,从起点搜一遍 (priority_queue(h(x)+g(x))) g(x)是 ...
- POJ 2387 Til the Cows Come Home
题目链接:http://poj.org/problem?id=2387 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K ...
- 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 ...
- POJ.2387 Til the Cows Come Home (SPFA)
POJ.2387 Til the Cows Come Home (SPFA) 题意分析 首先给出T和N,T代表边的数量,N代表图中点的数量 图中边是双向边,并不清楚是否有重边,我按有重边写的. 直接跑 ...
- POJ 2387 Til the Cows Come Home(最短路 Dijkstra/spfa)
传送门 Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 46727 Acce ...
- 怒学三算法 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 ...
随机推荐
- Codeforces 1249 E. By Elevator or Stairs?
传送门 首先显然下楼的操作一定是不优的,所以只要考虑上楼 设 $f[i]$ 表示到第 $i$ 层时需要的最少时间 那么首先考虑走楼梯,有转移,$f[i]=f[i-1]+a[i-1]$ 然后考虑坐电梯有 ...
- C#基础--Ref与Out区别
两者都是按地址传递的,使用后都将改变原来参数的数值. class Program { static void Main(string[] args) { int num = 1; Method(ref ...
- 【轻松一刻】Java制作字符动画
前言 今晚闲来无事,整理了一下电脑中尘封已久的旧代码,看着那些年自己写过的代码,踩过的坑,顿时老泪纵横.正当在感叹之际,突然发现在“马克思”文件夹下出现了一个好玩的项目,那就是N年前刚学Java时写的 ...
- impdp时报错ORA-39083&ORA-01917
转自:http://www.codes51.com/article/detail_146662.html impdp时报错ORA-39083&ORA-01917ORA-39083: 对象类型 ...
- stm32 待机模式
低功耗模式 降低系统时钟速度 不使用APBx和AHB外设时,将对应的外设时钟关闭 睡眠模式(Cortex™-M3内核停止,所有外设包括Cortex-M3核心的外设,如NVIC.系统时钟(SysTick ...
- JsonObject转换失败的bug
使用过程发现初始化失败,终于找到原因 转换的字符串必须都是字符串,不能没有双引号,否则转换失败
- Nginx从安装到简单使用
一.什么是Nginx: Nginx是一个高性能的HTTP和反向代理服务,也是一个IMAP/POP3/SMTP服务. 二.Nginx作用: 反向代理,集群,虚拟服务器,负载均衡,动静分离,解决跨域问题等 ...
- flex布局下img图片变形的解决方法
图片正常效果 图片变形效果 一.flex-shrink: 0 给 img 设置 flex-shrink: 0; flex-shrink 的默认值为1,如果没有显示定义该属性,将会自动按照默认值 ...
- ASE19团队项目 beta阶段 model组 scrum6 记录
本次会议于12月9日,19时30分在微软北京西二号楼sky garden召开,持续20分钟. 与会人员:Jiyan He, Lei Chai, Linfeng Qi, Xueqing Wu, Kun ...
- 根目录/缺少执行权限x产生的两种错误
Linux根目录缺少x权限,产生的两个错误: 以root用户执行systemctl命令报权限相关问题 [root@hps2 ~]# systemctl stop hps-manager * (pktt ...