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 ...
随机推荐
- java异常那些事
异常的基本定义: 异常情形是指阻止当前方法或者作用域继续执行的问题.在这里一定要明确一点:异常代码某种程度的错误,尽管Java有异常处理机制,但是我们不能以“正常”的眼光来看待异常,异常处理机制的原因 ...
- hdu 6377 度度熊看球赛 (dp)
大意: $n$对情侣, $2n$个座位, 对于一个方案, 若$k$对情侣相邻, 则喧闹值增加$D^k$, 求喧闹值期望. 跟CF 840C一样, 设$dp[i][j]$为$i$个人, 有$j$对情侣相 ...
- MH-P虚拟机DSR中安装SQL2008
双击下载好的安装文件setup.exe.(注意:安装之前请确认是否有安装SQL Server 2008 R2需要的.NET Framework 3.5 SP1,我的环境由于之前有配置安装过,在这里不具 ...
- .net core 依赖注入在特性中的应用
.net core 依赖注入在特性中的应用,不知道怎么用属性注入,那么在特性中的构造函数里,怎么用接口的方法呢? 来一个简单的例子: 主要思路是把ServiceProvider 静态全局化: publ ...
- vmware 虚拟机扩展 liunx系统硬盘空间
参考一下以下博客 https://www.cnblogs.com/yongdaimi/p/9050155.html https://blog.csdn.net/daemon_2017/article/ ...
- Linux命令——netstat
参考:20 Netstat Commands for Linux Network Management Foreword Print network connections, routing tabl ...
- 网络设备驱动程序-netdevice结构体关键部分注释
仅仅做个记录,内核4.19 struct net_device { char name[IFNAMSIZ]; //网络设备的名称 struct hlist_node name_hlist; char ...
- Exams(二分
题意:给你每天要考的科目,和每门科目需要复习多长时间,问最少需要几天才能完成所有的考试. 思路:二分答案,然后判断答案是否可行,这边需要进行贪心,即倒着往前推, 比如第i天,那么前面有i-1天是,可供 ...
- 2018/7/31 -zznu-oj -问题 C: 磨刀- 【扩展欧几里得算法的基本应用】
问题 C: 磨刀 时间限制: 1 Sec 内存限制: 128 MB提交: 190 解决: 39[提交] [状态] [讨论版] [命题人:admin] 题目描述 磨刀是一个讲究的工作,只能在n℃下进 ...
- linux tcp server bind
如果不是系统管理员: bind()函数 返回失败