Til the Cows Come Home (最短路,Dijkstra算法)

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.
贝茜在外面的田里,她想在农夫约翰叫醒她去挤奶前回到谷仓,睡个好觉。贝茜需要美美的睡眠,所以她想尽快回来。
农夫约翰的田里有N个(2 <= N <= 1000)地标,唯一编号为1..N。地标1是谷仓;贝西整天站在的那片苹果园是地标N.奶牛在田野里走着,用T (1 <= T <= 2000)双向的牛道——不同长度的牛道。贝茜对自己的导航能力没有信心,所以一旦开始,她就会一直跟踪。
根据路标之间的路径,确定贝西要走的最小距离才能回到谷仓。可以保证存在这样的路线。


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.
* 第1行:两个整数:T和N
*线2 . .T+1:每一行描述一个轨迹为三个空格分隔的整数。前两个整数是路径旅行的地标。第三个整数是路径的长度,范围是1..100。


Output
* Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
*第1行:单个整数,贝西从地标N到地标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.


#include <stdio.h>
#include<algorithm>
using namespace std; 

const int N = (int) 1000 + 11; 
const int INF = (int) 0x3f3f3f3f;

int n, m; 
int mp[N][N]; 
void init(int n){
    for(int i = 0; i < n; i++){
        for(int j = 0; j < n; j++){
            if(i == j) mp[i][j] = 0;
            else mp[i][j]= INF;         //初始化 
        }
    }
}
void getmap(int m){ 
    int u, v, val;
    while(m--){
        scanf("%d%d%d", &u, &v, &val);
        u--; v--;
        mp[u][v] =  mp[v][u]= min(mp[u][v], val);   //无向图 
    }
}
bool vis[N]; int dis[N];    //vis[]是否走过,dis[]i点到st的最近距离 
void djk(int st, int ed){ 
    for(int i = 0; i < n; i++){
        vis[i] = false;
        dis[i] = mp[st][i];
    } 
    vis[st] = true;
    for(int i = 1; i < n; i++){
        int mn = INF, id = -1;  
        for(int j = 0; j < n; j++){
            if(!vis[j] && dis[j] < mn) {
                mn = dis[j]; id = j;
            }
        }
        if(id == -1) break;
        vis[id] = true; 
        for(int j = 0; j < n; j++){
            if(!vis[j] && mp[id][j] != INF) { 
                if(dis[j] > dis[id] + mp[id][j])  
                    dis[j] = dis[id] + mp[id][j];
            }
        }
    }
    printf("%d\n", (dis[ed] == INF) ? -1 : dis[ed]);
}
int main(){
    int t; 
    scanf("%d%d", &t, &n);
    init(n);
    getmap(t);
    djk(n-1, 0);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/shf1730797676/article/details/81835102