[Kuangbin take you to fly] four thematic shortest exercises A - Til the Cows Come Home (spfa algorithm)

A - Til the Cows Come Home

Topic links: https://vjudge.net/contest/66569#problem/A

topic:

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.
 
Meaning of the questions:
 
1307/5000
Bessie in the field, we want to return to the barn, sleep as much as possible, then Farmer John when she would wake up in the morning milking. Bessie needs her beauty sleep, so she wants to come back as soon as possible.

    Farmer John site has N (2 <= N <= 1000) landmarks, unique number 1..N. Landmark 1 barn; Bessie day apple trees standing cow is N. landmark used when traveling field T (1 <= T <= 2000) Shelter bidirectional traces, of various lengths between landmarks. Bessie no confidence in her ability to navigate, so she once started has maintained from the beginning to the end of the track.

    In view of the path between the landmarks, to determine the minimum distance Bessie must go back to the barn. Guarantee the existence of some of these routes.
Input
    * Line 1: two integers: T and N.

    * row 2..T + 1: In each row describes a three integers separated by spaces of the tracks. The first two are integers trail travel landmarks. The third is the integer length of the path, the range of 1..100.
Yield
    * Line 1: an integer, Bessie minimum distance from a landmark to the landmark N 1 must pass.
Sample input

    . 5. 5
    . 1 2 20 is
    2. 3 30
    . 3 20 is. 4
    . 4. 5 20 is
    . 1. 5 100

samples of the output

    90
Ideas: Shortest template entry title, spfa algorithm is as follows:
 
//
// 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\n", d[n]);
    }
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/Vampire6/p/11201337.html