博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[kuangbin带你飞]专题四 最短路练习 A - Til the Cows Come Home(spfa算法)
阅读量:4557 次
发布时间:2019-06-08

本文共 2908 字,大约阅读时间需要 9 分钟。

A - Til the Cows Come Home

题目链接:

题目:

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 51 2 202 3 303 4 204 5 201 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.
 
题意:
 
1307/5000
Bessie在外地,想要回到谷仓,尽可能多地睡觉,然后Farmer John在早上挤奶时将她叫醒。贝茜需要她的美丽睡眠,所以她想尽快回来。
    Farmer John的场地中有N(2 <= N <= 1000)个地标,唯一编号为1..N。地标1是谷仓; Bessie整天站立的苹果树林是具有里程碑意义的N.奶牛在田间旅行时使用T(1 <= T <= 2000)双向牛尾迹,各种长度的地标之间。 Bessie对她的导航能力没有信心,因此她一旦开始就始终保持着从开始到结束的轨迹。
    鉴于地标之间的路径,确定贝西必须走的最小距离才能回到谷仓。保证存在一些这样的路线。
输入
    *第1行:两个整数:T和N.
    *行2..T + 1:每行描述一个以三个以空格分隔的整数的轨迹。前两个整数是小道行进的地标。第三个整数是路径的长度,范围是1..100。
产量
    *第1行:一个整数,是Bessie从地标N到地标1必须经过的最小距离。
样本输入
    5 5
    1 2 20
    2 3 30
    3 4 20
    4 5 20
    1 5 100
样本输出
    90
思路:最短路模板入门题,spfa算法如下:
 
//// Created by hanyu on 2019/7/14.//#include
#include
#include
#include
#include
#include
using namespace std;typedef long long ll;#define MAX 0x3f3f3f3fint T,n;const int maxn=1005;bool book[maxn];int lu[maxn][maxn];int d[maxn];void spfa(int a){ int now; memset(book,false,sizeof(book)); memset(d,MAX,sizeof(d)); queue
qu; d[a]=0; book[a]=true; qu.push(a); while(!qu.empty()) { now= qu.front(); qu.pop(); book[now]=false; for(int i=1;i<=n;i++) { if(d[i]>d[now]+lu[now][i]) { d[i]=d[now]+lu[now][i]; if(!book[i]) { qu.push(i); book[i]=true; } } } }}int main(){ while(~scanf("%d%d",&T,&n)) { for (int i = 1; i <= n; i++) { for (int j = 1; j <= i; j++) { if (i == j) lu[i][j] = 0; else lu[i][j] = lu[j][i] = MAX; } } int x, y, z; for (int i = 1; i <= T; i++) { scanf("%d%d%d", &x, &y, &z); if(z

 

转载于:https://www.cnblogs.com/Vampire6/p/11201337.html

你可能感兴趣的文章
Codeforces 1000C Covered Points Count 【前缀和优化】
查看>>
python高效读取文件、文件改写
查看>>
gulp
查看>>
pgsql查询优化之模糊查询
查看>>
不变模式
查看>>
matlab去云雾
查看>>
500lines项目简介
查看>>
Asp.net core logging 日志
查看>>
BOM浏览器对象模型
查看>>
Jq 遍历each()方法
查看>>
Android源码分析:Telephony部分–phone进程
查看>>
关于 redis.properties配置文件及rule
查看>>
WebService
查看>>
关于Java中重载的若干问题
查看>>
Java中start和run方法的区别
查看>>
23种设计模式中的命令模式
查看>>
[转载]年薪10w和年薪100w的人,差在哪里?
查看>>
shell 日期参数
查看>>
package的使用
查看>>
括号生成
查看>>