POJ2387 Til the Cows Come Home(最短路)

POJ2387 Til the Cows Come Home(最短路)

目录

  • POJ2387 Til the Cows Come Home(最短路)
      • 目录
      • 原题
      • 大概题意
      • 代码块

传送门

原题

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.

大概题意

就是单源最短路径啦
要求的是从点1到点n所需的最短路径

写这篇博客的主要目的是放一个SPFA模板啦啦啦
用Dijkstra要去重边耶

代码块

//SPFA
#include 
#include 
#include 
#include 
#include 
const int inf = 19971010;
using namespace std;

int n, m, u, v, w, s, dis[2010], vis[2010]; 
int cnt, head[2010], val[4010], to[4010], nxt[4010];

inline int read(){
    int ans = 0, f = 1;
    char ch = getchar();
    for(; ch < '0' || ch > '9'; ch = getchar())
        if (ch == '-')
            f = 0;
    for(; ch >= '0' && ch <= '9'; ch = getchar())
        ans = (ans << 3) + (ans << 1) + ch - 48;
    return f? ans: -ans;
}

void add(int u, int v, int w){
    nxt[++cnt] = head[u];
    to[cnt] = v; 
    val[cnt] = w;
    head[u] = cnt; 
}    

void spfa(){
    queue  q; 
    for (int i = 1; i <= n; i++)
        dis[i] = inf, vis[i] = 0; 
    q.push(s); 
    dis[s] = 0; 
    vis[s] = 1;
    while (!q.empty()){
        int uu = q.front();
        q.pop(); 
        vis[uu] = 0; 
        for (int i = head[uu]; i; i = nxt[i]){
            int vv = to[i]; 
            if (dis[vv] > dis[uu] + val[i]){
                dis[vv] = dis[uu] + val[i];
                if (vis[vv] == 0){
                    vis[vv] = 1; 
                    q.push(vv);
                }
            }
        }
    }
}

int main(){
    scanf("%d%d", &m, &n);
    memset(head, 0, sizeof(head)); 
    cnt = 0;
    for (int i = 1; i <= m; i++){
        scanf("%d%d%d", &u, &v, &w);
        add(u, v, w);
        add(v, u, w);   
    } 
    s = 1;
    spfa(); 
    printf("%d\n", dis[n]);

    return 0;
} 

你可能感兴趣的:(题解,模板)