• [kuangbin带你飞]专题四 最短路练习 A


    A - Til the Cows Come Home

    题目链接:https://vjudge.net/contest/66569#problem/A

    题目:

    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.
     
    题意:
     
    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<iostream>
    #include<algorithm>
    #include<queue>
    #include<map>
    #include<cstring>
    #include<cstdio>
    using namespace std;
    typedef long long ll;
    #define MAX 0x3f3f3f3f
    int 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<int>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<lu[x][y])
                     lu[x][y] = lu[y][x] = z;
            }
            spfa(1);
            printf("%d
    ", d[n]);
        }
        return 0;
    }
  • 相关阅读:
    Windows照片查看器全屏浏览查看
    Windows调出软键盘
    IE叉事件
    对gridview的小改动
    gridview小把戏
    安全认证(转)
    用数组的方式实现DataTable中的distinct(转)
    TreeView的简单应用
    禁止按钮重复提交
    配置Microsoft Visual SourceSafe 2005的Internet访问(转)
  • 原文地址:https://www.cnblogs.com/Vampire6/p/11201337.html
Copyright © 2020-2023  润新知