POJ 2387-Til the Cows Come Home(Dijkstra+堆优化)

你不知道的Postman的Mock接口测试,看这一篇就够了 创建Mock服务你可以从Postman已有的测试集(Collection)中创建Mock Server或者直接创建Mock Server(我们这里选择从已有的测试集中创建Mock Server)Mock server详细配置页面,在此页面中我们可以设置:命名Mock Server选择一个环境(可选的),我们可以选择之前设置过的环境,Mock Server将会使用对应环境中设置过的变量在对应的请求和响应中如果想要将Mock Server设置私有访问的,那么可以勾选此选项,不勾选则对所有人可见。 阅读详情

Til the Cows Come Home

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 40039 Accepted: 13620

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.

Source


题目意思:

有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离。


两种解题思路:

①Dijkstra;

②使用STL的priority_queue实现。

①

/*
* Copyright (c) 2016, 烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:dijkstra.cpp
* 作    者:单昕昕
* 完成日期:2016年3月30日
* 版 本 号:v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define MAXN 1010
#define INF 0xfffffff//0X代表16进制,后面是数字,十进制是4294967295
using namespace std;
int cost[MAXN][MAXN],dis[MAXN],n,t;
bool used[MAXN];//标识是否使用过
void dijkstra(int s)
{
    memset(used,0,sizeof(used));
    fill(dis,dis+n+1,INF);
    fill(used,used+n+1,false);
    dis[s]=0;
    while(true)
    {
        int v=-1;
        //从未使用过的顶点中选择一个距离最小的顶点
        for(int u=0; u<n; ++u)
            if(!used[u]&&(v==-1||dis[u]<dis[v]))
                v=u;
        if(v==-1) break;
        used[v]=true;
        for(int u=0; u<=n; ++u)
            dis[u]=min(dis[u],dis[v]+cost[v][u]);
    }
}
int main()
{
    int a,b,l;
    while(scanf("%d%d",&t,&n)!=EOF)
    {
        for(int i=0; i<=n; ++i)
            for(int j=0; j<=n; ++j)
                cost[i][j]=INF;//手动初始化,不能fill了……
        for(int i=0; i<t; i++)
        {
            scanf("%d%d%d",&a,&b,&l);
            if(l<cost[a][b])
                cost[a][b]=cost[b][a]=l;//无向图
        }
        dijkstra(1);
        printf("%d\n",dis[n]);
    }
    return 0;
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
*/


②使用STL的priority_queue实现。

内存占用少:

/*
* Copyright (c) 2016, 烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:queue.cpp
* 作    者:单昕昕
* 完成日期:2016年6月1日
* 版 本 号:v2.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define INF 0xfffffff//0X代表16进制,后面是数字,十进制是4294967295
#define MAXN 1010
using namespace std;
int dis[MAXN],n,t;
struct edge
{
    int to;
    int cost;
};
vector<edge> G[MAXN];//边表
typedef pair<int,int> P;
//first是最短距离,second是顶点的编号
void dijkstra(int s)
{
    //通过指定greater<P>参数,堆按照first从大到小的顺序取初值
    priority_queue<P,vector<P>,greater<P> > que;
    fill(dis,dis+n+1,INF);//初始化范围要到n
    dis[s]=0;
    que.push(P(0,s));
    while(!que.empty())
    {
        P p=que.top();
        que.pop();
        int v=p.second;
        if(dis[v]<p.first) continue;
        for(int i=0; i<G[v].size(); ++i)
        {
            edge e=G[v][i];
            if(dis[e.to]>dis[v]+e.cost)//Dijkstra
            {
                dis[e.to]=dis[v]+e.cost;
                que.push(P(dis[e.to],e.to));
            }
        }
    }
}
int main()
{
    int a,b,l;
    while(scanf("%d%d",&t,&n)!=EOF)
    {
        for(int i=0; i<t; i++)
        {
            scanf("%d%d%d",&a,&b,&l);//a->b的距离是l
            G[a].push_back({b,l});//这里的语法要注意
            G[b].push_back({a,l});
        }
        dijkstra(1);//起点是1
        printf("%d\n",dis[n]);
    }
    return 0;
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
*/


③

/*
* Copyright (c) 2016, 烟台大学计算机与控制工程学院
* All rights reserved.
* 文件名称:dijkstra.cpp
* 作    者:单昕昕
* 完成日期:2016年3月30日
* 版 本 号:v1.0
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <queue>
#include <algorithm>
#define inf 0xfffffff//0X代表16进制,后面是数字,十进制是4294967295
using namespace std;
int p[1003][1003],flag[1003],dis[1003],mmin,n,t;
void dijkstra()
{
    memset(flag,0,sizeof(flag));
    for(int i=2; i<=n; i++)
    {
        dis[i]=p[1][i];
    }
    flag[1]=1;
    int pre;
    for(int i=1; i<=n; i++)
    {
        mmin=inf;
        for(int j=1; j<=n; j++)
        {
            if(flag[j]==0&&dis[j]<mmin)
            {
                mmin=dis[j];
                pre=j;
            }
        }
        if(mmin==inf)
            break;
        flag[pre]=1;
        for(int j=1; j<=n; j++) //找最短路
        {
            if(flag[j]==0&&dis[pre]+p[pre][j]<dis[j])
            {
                dis[j]=dis[pre]+p[pre][j];
            }
        }
    }
}
int main()
{
    int a,b,l;
    while(scanf("%d%d",&t,&n)!=EOF)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                p[i][j]=inf;
            }
        }
        for(int i=0; i<t; i++)
        {
            scanf("%d%d%d",&a,&b,&l);
            if(l<p[a][b])
            {
                p[a][b]=p[b][a]=l;//无向图
            }
        }
        dijkstra();
        printf("%d\n",dis[n]);
    }
    return 0;
}



LinePhone3.2.7(android端最新版本) sip客户端 SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/ 立即下载

相关推荐

用STM32的SPI模式2搞定SSI绝对值编码器通信(附完整代码与避坑点)

本文详细解析了如何使用STM32的SPI模式2实现SSI绝对值编码器通信,包括硬件设计、SPI配置、数据处理算法及稳定性优化技巧。通过完整的代码示例和避坑指南,帮助工程师解决时序匹配、数据解析等常见问题,提升工业自动化系统中的编码器通信可靠性。

weixin_27252001的博客 275

POJ2387 ————最短路

链接:https://cn.vjudge.net/problem/POJ-2387 题意: 大约就是 一个people1 想多睡美容觉, 所以要尽快从顶点N 回到 顶点1,保证在people2叫醒他之前多睡点会。 然而people1 是个路痴, 但每个顶点之间有多条双向的牛的轨迹, 所以他可以通过这个回到顶点1。 所以该题 就是求N到1的最短路径。 只不过要注意的是 在输入中 两个顶点之间可能有条...

T.J的博客 528

别再乱改宏定义了!STM32F103程序移植保姆级教程(从C8T6到ZET6实战)

本文详细解析了STM32F103程序从C8T6移植到ZET6的实战步骤,重点介绍了硬件差异、开发环境配置、时钟陷阱及外设处理等关键点。通过修改宏定义(如STM32F10X_HD)、调整启动文件和时钟配置,确保移植顺利进行。适合嵌入式开发者参考,避免常见移植问题。

weixin_42567497的博客 220

POJ2387 Til the Cows Come Home

Descriptionn个点m条边,每条边有权值,问从点1到点n的最短距离 Input第一行两个整数m和n表示边数和点数(1<=m<=2000,1<=n<=1000),之后m行每行三个整数a,b和c表示点a和点b之间有一条权值为c的双向边 Output输出点1到点n的最短距离 Sample Input5 5 1 2 20 2 3 30 3 4 20 4 5 20 1 5 100

Demo的博客 506

聚类模型-层次聚类

聚类模型 1、层次聚类 2、原型聚类-K-means 3、模型聚类-GMM 4、EM算法-LDA主题模型 5、密度聚类-DBSCAN 6、图聚类-谱聚类 一、层次聚类 一、聚类理论 ​ 一般来说,聚类是在训练样本的标签信息不知的情况下,学习样本内在的性质和规律,将有限的集合划分成ccc类。根据“方以类聚,物以群分”的思想,类内对象尽可能的相似,...

myazi 6507

WinForm实战:如何用C#和PdfiumViewer打造一个功能齐全的PDF阅读器(含源码)

本文详细介绍了如何使用C#和PdfiumViewer库从零开始构建一个功能齐全的WinForm PDF阅读器。内容涵盖项目环境搭建、核心组件解析、基于命令模式的工具栏与UI架构设计,并深入讲解了智能缩放、页面导航、打印功能等核心交互的实现,以及内存管理和性能优化等高级话题,为开发者提供了一套完整的实战解决方案。

docker8compose的博客 509

POJ2387(最短路基础)

题目大意:n个点,m条边,求从第一个点到第n个点的最短路径,最基础的最短路问题,图论由于刚开始训练,还是把Dijkstra算法和SPFA算法对着书上的思路模拟了一边加深印象。

wenzhunpu的专栏 637

从 Open SQL JOIN 走向 ABAP CDS Association:把查询写成可复用的数据模型

摘要: 本文通过飞行模型示例,对比了ABAP中三种表关联实现方式:Open SQL JOIN、CDS显式JOIN和CDS Association。重点解析了Association的本质是将表关系声明为数据模型语言,使用时通过路径表达式按需展开为JOIN。文章指出路径表达式在不同位置会默认展开为不同JOIN类型(如SELECT列表默认为LEFT OUTER),并演示了如何用[inner]强制指定INNER JOIN。最后结合S/4HANA实际案例,说明Association通过中心化关系定义和路径复用,能有效

2007 年 ~ 2025 年,深耕 SAP 技术 18 年 390

POJ 2387(最短路dijkstra)

传送门:POJ2387 注意:需要注意的就是两个点之间可能有多条路,记录下最短的数据。 #include #include using namespace std; #define MAX 1005 #define INF 0x3f3f3f3 int Map[MAX][MAX]; int dis[MAX]; int grp[MAX]; void dijkstra(int n,int

一个写程序的 1270

C#上位机面试必问30题:机器视觉+工业通信+性能优化(含答案解析)

工业通信:围绕“稳定性”展开,重点答异常处理、断线重连、资源释放;机器视觉:围绕“精度+速度”展开,重点答预处理、ROI、参数优化;性能优化:围绕“UI不卡、内存不泄、效率不低”展开,重点答异步、限流、资源复用;面试加分项:结合实际项目案例(如“我在XX项目中用信号量解决了多设备并发卡顿问题”)。

这里只有干货:从 Modbus 通信到 MES 对接,从 YOLO 训练到工控机部署,带你搞定工业软件开发全流程。 1519

【Python进阶】VSCode Python开发完全指南:从环境配置到高效调试

本文通过15个实战案例,系统讲解VSCode进行Python开发的环境配置、编码技巧、调试方法及性能优化,帮助初学者快速构建专业开发环境。

赶紧的博客 3867

poj2387(SPFA算法)

1.建⽴⼀个队列,初始时队列⾥只有起始点,再建⽴⼀个表格记录起始点到所有点的最短路径(该表格的初始值要赋为极⼤值,该点到他本⾝的路径赋为 无穷大)。然后执⾏松弛操作,⽤队列⾥有的点作为起始点去刷新到所有点的最短路,如果刷新成功且被刷新点不在队列中则把该点加⼊到队列最后。重复执⾏直到队列为空。 方法一:SPFA算法 #include<iostream> #include<algorithm> #include<cmath> #include<cstring> #

Keep_Trying_Go的博客 616

SimpleITK使用——1. 进行Resample/Resize操作

文章目录1. 变换到一个新的spacing1.1 使用Resample()的方法1.1.1 完整代码1.1.2 转换前后图像1.2 使用ResampleImageFilter()的方法2. 已知原图和变换后目标图像的大小2.1 代码2.2 转换后图像3. 相关内容说明3.0 体素和spacing3.1 Resample() 函数说明3.2 itk::simple::ResampleImageFilter函数说明 1. 变换到一个新的spacing 1.1 使用Resample()的方法 1.1.1

啷个哩个啷 8088

【POJ 2387 --- Til the Cows Come Home】dijkstra || dijkstra堆优化 || SPFA

【POJ 2387 --- Til the Cows Come Home】dijkstra || dijkstra堆优化 || SPFA 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 ...

来日浅谈的博客 511

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版

Part-E1-SDIO-Specification-Ver4.00-Final-120220.pdf:最新的sdio 4.0 spec 协议标准 带书签 高清完整PDF版,包括SDIO 接口的外设,SDIO 接口官方协议等。

POJ2387-Til the Cows Come Home

POJ2387-Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 55360 Accepted: 18793 DescriptionBessie is out in the field and wants to get back to the barn to g

君安在AreYouWell 447

visio安装包

Microsoft Office Visio 是一款专业的制图软件。利用经过更新的形状、协作工具和数据链接图表,创建专业级图表以简化复杂的信息。

POJ 2387 Til the Cows Come Home【dijkstra模板题】

POJ 2387 Til the Cows Come Home 题目大意: 无向图中,从点n到点1的最短路径 具体思路: dijkstra,考虑重边 注意下标减1,建议下次还是不使用0号,便于理解 具体代码: #include<iostream> #include<cstdio> #include<stdlib.h> #include<string>...

指弹键盘哼民谣 893

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

POJ2387::Til the Cows Come Home dijkstra算法

Dijkstra算法 Dijkstra算法大致上是利用贪心思想实现的,首先把起点到所有点的距离存下来找个最短的,然后遍历一遍看通过刚刚找到的距离最短的点作为中转点会不会更近,如果更近了就更新距离,这样把所有的点遍历之后就存下了起点到其他所有点的最短距离。然后将最短点的距离输出。 下面咱们来看一下这个题: Til the Cows Come Home POJ - 2387 Bessie is out...

weixin_43620849的博客 894

【贪心】Dijkstra-POJ2387 Til the Cows Come Home

题目 http://poj.org/problem?id=2387 问题分析 这个题可以把地标作为顶点,把牛的踪迹作为边,顶点数为n,边数为t,那么所求就是第n个顶点到第一个顶点的最短路长度。 算法 算法核心 本题采用Dijkstra算法,Dijkstra算法是求最短路的贪心算法。有两个点集,设顶点总体为集合V,集合U初始为空,先确定起始顶点,将其加入集合U,然后在集合V中找到离起点最近的顶点,将...

vxiao_shen_longv的博客 208

POJ 2387 Til the Cows Come Home

题目链接&lt;http://poj.org/problem?id=2387&gt;题意&amp;题解:先一共有n个点,输入T条边,求从点1到点n的最短路。注意:1、数据量较大,不适宜用Floyd。          2、会有重边(链式前向星可忽略这点)。#include &lt;iostream&gt; #include &lt;cstdio&gt; #include &lt;algorithm...

Eliot的博客 206

Til the Cows Come Home POJ - 2387(Dijkstra模板 优先队列优化)

Til the Cows Come Home POJ - 2387 #include <cstdio> #include <vector> #include <queue> #include <cstring> using namespace std; typedef pair<int, int> pii; const int INF = 0x3f3f3f3f; const int N = 1000 + 5; struct node {

NaNa 196

Til the Cows Come Home//POJ - 2387//dijkstra

Til the Cows Come Home//POJ - 2387//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. Bes...

salty_fishman的博客 218

POJ - 2387 Til the Cows Come Home(最短路)(Dijkstra模板题)

POJ - 2387 Til the Cows Come Home(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 morni...

四季不败的博客 304

Dijkstra-POJ-2387-Til the Cows Come Home

Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 36731 Accepted: 12512 DescriptionBessie is out in the field and wants to get back to the barn to get as

懒鬼的私人泳池 689

Til the Cows Come Home-Poj2387(最短路)

Til the Cows Come Home-Poj2387 题目大意:有N个点,给出从a点到b点的距离,当然a和b是互相可以抵达的,问从1到n的最短距离 思路:bellman-ford求最短路模板题,此题是多组输入(卡我好久c++) //#include<bits/stdc++.h> #include <iostream> #include <vector> ...

sakuyu_10的博客 367

POJ2387 Til the Cows Come Home【Dijkstra】

Til the Cows Come Home 最短路落题,如果是邻接表存图,则要小心重边。换作是链式前向星存图就没事了。 #include<algorithm> #include<iostream> #include<cstring> #include<iomanip> #include<cstdio> #include<string> #include<vector> #include<cmath&g...

lllllan 191

Poj 2387 Til the Cows Come Home

Til the Cows Come Home Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 47474 Accepted: 16143 Description Bessie is out in the field and wants to get back t

香橙的博客 399
上一篇: 广度优先搜索BFS(迷宫问题)
下一篇: 静态方法输出参数统计大写字母、小写字母、数字、其他字符的个数
kewlgrl
博客等级 码龄12年 507粉丝 971原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值