【POJ - 2387】 Til the Cows Come Home(单源最短路Dijkstra算法)

题干:

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<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>using namespace std;
const int MAX = 100000 + 5;
const int INF = 0x3f3f3f3f ;
int vis[MAX];
int maze[1005][1005];
int n,m;
int dis[MAX];//表示从出发点开始到该点的最短距离。 void Dijkstra(int u,int v) {dis[u] = 0;
//	vis[u] = 1;		//这步还真不能加上。。。。。 int minw = INF,minv;for(int k = 1;k<= n;k++) {minw = INF;for(int i = 1; i<=n; i++) {if(vis[i] ) continue;if(dis[i] < minw) {minv = i; minw = dis[i];}}vis[minv] = 1;if(minv == v) break;for(int i = 1; i<=n; i++) {if(vis[i]) continue;if(maze[minv][i] == 0) continue;dis[i] = min(dis[i], dis[minv] + maze[minv][i]);}}printf("%d\n",dis[v]);} int main()
{int a,b,w;while(~scanf("%d %d",&m,&n) ) {memset(dis,0x3f3f3f3f,sizeof(dis) ) ;memset(maze,0x3f3f3f3f,sizeof(maze) );memset(vis,0,sizeof(vis) ) ;for(int i = 1; i<=m; i++) {scanf("%d%d%d",&a,&b,&w);if(w<maze[a][b])maze[a][b] = maze[b][a] = w;}Dijkstra(1,n); }return 0 ;} 

AC代码2:(Bellman-Ford算法)

//*bellman算法:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define N 2010
#define MAX 99999999
using namespace std ;
struct node {int a , b , w ;
} edge[N];
int n , m ;
void bell() {int i , j ;int  d[N];for(int i =1 ; i<=n; i++) { //*距离初始化为无穷;d[i]=MAX;}d[1]=0;//*初始地点为0;for(i=1; i<=n; i++) {for(j=1; j<=m; j++) { //*按点-边搜,顺便解决了重边问题;if(d[edge[j].a]>d[edge[j].b]+edge[j].w) d[edge[j].a]= d[edge[j].b]+edge[j].w;if(d[edge[j].b]>d[edge[j].a]+edge[j].w) d[edge[j].b]= d[edge[j].a]+edge[j].w;}}printf("%d\n",d[n]);
}
int main() {int i , a   , b ,c;while(cin>>m>>n) {for(int i =1 ; i<=m; i++) { //*结构体存边和权cin>>a>>b>>c;edge[i].a=a;edge[i].b=b;edge[i].w=c;}bell();}return 0 ;
}

 

 总结:

    别忘处理一下重边!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/441975.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

递归Java_递归的Java实现

递归是一种应用非常广泛的算法(或者编程技巧)。递归求解问题的分解过程&#xff0c;去的过程叫“递”&#xff0c;回来的过程叫“归”。递归需要满足的三个条件&#xff1a;1. 一个问题的解可以分解为几个子问题的解&#xff1b;2. 这个问题与分解之后的子问题&#xff0c;除了…

【HDU - 2112】 HDU Today(dijkstra单源最短路 + map转换)

题干&#xff1a; HDU Today Time Limit : 15000/5000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submission(s) : 11 Accepted Submission(s) : 5 Problem Description 经过锦囊相助&#xff0c;海东集团终于度过了危机&#xff0c;从此&#…

JAVA线程并发数量控制_线程同步工具(二)控制并发访问多个资源

声明&#xff1a;本文是《 Java 7 Concurrency Cookbook》的第三章&#xff0c; 作者&#xff1a; Javier Fernndez Gonzlez 译者&#xff1a;郑玉婷控制并发访问多个资源在并发访问资源的控制中&#xff0c;你学习了信号量(semaphores)的基本知识。在上个指南&#xff0c;你实…

*【51nod - 1459】迷宫游戏(记录双向权值的Dijkstra单源最短路)

题干&#xff1a; 你来到一个迷宫前。该迷宫由若干个房间组成&#xff0c;每个房间都有一个得分&#xff0c;第一次进入这个房间&#xff0c;你就可以得到这个分数。还有若干双向道路连结这些房间&#xff0c;你沿着这些道路从一个房间走到另外一个房间需要一些时间。游戏规定…

算法--背包九讲(详细讲解+代码)

背包九讲 目录 第一讲 01背包问题 第二讲 完全背包问题 第三讲 多重背包问题 第四讲 混合三种背包问题 第五讲 二维费用的背包问题 第六讲 分组的背包问题 第七讲 有依赖的背包问题 第八讲 泛化物品 第九讲 背包问题问法的变化 附&#xff1a;USACO中的背包问题 前…

java中白盒测试用例_基于JAVA开发的中国象棋游戏的开发与研究白盒测试用例.doc...

中国象棋白盒测试用例文件状态当前版本V1.0草稿作 者梁世聪完成日期2012/6/17文档模板SSP-VER-T13-V1.0密 级变更历史版本完成日期变更记录作者批准签字V1.02012/6/17无梁世聪梁世聪目 录目录1 目的12 范围13 被测模块列表14 模块逻辑结构14.1 模块逻辑结构图14.2 模块功能定义…

【POJ - 1502】MPI Maelstrom(Dijkstra单源最短路--求一点到其余个点的最小值的最大值)

题干&#xff1a; BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKees research advisor, Jack Swigert, has asked her to ben…

java 强制清除缓存_IDEA强制清除Maven缓存的方法示例

重新导入依赖的常见方式下面图中的刷新按钮&#xff0c;在我的机器上&#xff0c;并不能每次都正确导入pom.xml中写的依赖项&#xff0c;而是导入之前pom.xml的依赖(读了缓存中的pom.xml)。当然除了这些&#xff0c;还可以下面这样&#xff1a;存在的问题上面虽然是重新导入Mav…

ACM算法--spfa算法--最短路算法

求单源最短路的SPFA算法的全称是&#xff1a;Shortest Path Faster Algorithm。 SPFA算法是西南交通大学段凡丁于1994年发表的。 从名字我们就可以看出&#xff0c;这种算法在效率上一定有过人之处。 很多时候&#xff0c;给定的图存在负权边&#xff0c;这时类似…

knn算法python理解与预测_理解KNN算法

KNN主要包括训练过程和分类过程。在训练过程上&#xff0c;需要将训练集存储起来。在分类过程中&#xff0c;将测试集和训练集中的每一张图片去比较&#xff0c;选取差别最小的那张图片。如果数据集多&#xff0c;就把训练集分成两部分&#xff0c;一小部分作为验证集(假的测试…

【POJ-3259】 Wormholes(判负环,spfa算法)

题干&#xff1a; While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Eac…

【HihoCoder - 1550】顺序三元组(思维)

题干&#xff1a; 给定一个长度为N的数组A[A1, A2, ... AN]&#xff0c;已知其中每个元素Ai的值都只可能是1, 2或者3。 请求出有多少下标三元组(i, j, k)满足1 ≤ i < j < k ≤ N且Ai < Aj < Ak。 Input 第一行包含一个整数N 第二行包含N个整数A1, A2, ... …

joptionpane java_Java JOptionPane

Java JOptionPane1 Java JOptionPane的介绍JOptionPane类用于提供标准对话框&#xff0c;例如消息对话框&#xff0c;确认对话框和输入对话框。这些对话框用于显示信息或从用户那里获取输入。JOptionPane类继承了JComponent类。2 Java JOptionPane的声明public class JOptionPa…

【POJ - 3268 】Silver Cow Party(Dijkstra最短路+思维)

题干&#xff1a; One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; roa…

【HDU - 3342】Legal or Not(拓扑排序)

题干&#xff1a; ACM-DIY is a large QQ group where many excellent acmers get together. It is so harmonious that just like a big family. Every day,many "holy cows" like HH, hh, AC, ZT, lcc, BF, Qinz and so on chat on-line to exchange their ideas.…

java 股票 代码_Java中利用散列表实现股票行情的查询_java

---- 在java中&#xff0c;提供了一个散列表类Hashtable&#xff0c;利用该类&#xff0c;我们可以按照特定的方式来存储数据&#xff0c;从而达到快速检索的目的。本文以查询股票的收盘数据为例&#xff0c;详细地说明java中散列表的使用方法。一、散列表的原理---- 散列表&am…

deepin部署python开发环境_deepin系统下部署Python3.5的开发及运行环境

deepin系统下部署Python3.5的开发及运行环境1 概述本人小白一枚&#xff0c;由于最近要学习python接口自动化测试&#xff0c;所以记录一下相关学习经过及经验&#xff0c;希望对跟我一样小白的朋友可以有所帮助。2 下载在python官网下载指定平台下的python3.5的环境wget https…

【HDU - 2066】:一个人的旅行(Dijkstra算法)

题干&#xff1a; 虽然草儿是个路痴&#xff08;就是在杭电待了一年多&#xff0c;居然还会在校园里迷路的人&#xff0c;汗~),但是草儿仍然很喜欢旅行&#xff0c;因为在旅途中 会遇见很多人&#xff08;白马王子&#xff0c;^0^&#xff09;&#xff0c;很多事&#xff0c;还…

for相关 java_Java学习之for循环相关知识梳理

for循环是编程语言中一种循环语句&#xff0c;是Java程序员日常工作中的重要组成部分。循环语句由循环体及循环的判定条件两部分组成&#xff0c;其表达式为&#xff1a;for(单次表达式;条件表达式;末尾循环体){中间循环体&#xff1b;}。拉勾IT课小编为大家分析如何使用这一属…

【HDU - 3790】最短路径问题(DIjkstra算法 双权值)

题干&#xff1a; 给你n个点&#xff0c;m条无向边&#xff0c;每条边都有长度d和花费p&#xff0c;给你起点s终点t&#xff0c;要求输出起点到终点的最短距离及其花费&#xff0c;如果最短距离有多条路线&#xff0c;则输出花费最少的。 Input 输入n,m&#xff0c;点的编号…