spfa算法解决POJ 2387

本文深入探讨了SPFA算法的实现细节与应用场景,通过一个具体的农场牛群导航问题,阐述了如何使用SPFA算法找到从任意节点到目标节点的最短路径。文章提供了完整的代码示例,并解释了输入格式的重要性,有助于读者理解和掌握SPFA算法。

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


spfa算法的输入是很讲究的,之所以这样
我想是方便后面用queue来存储和取出。
明白了输入是怎么一回事,搞清spfa的过程
就不是问题了。我在下面的代码给出了我
研究过程的代码,肯定会对大家有帮助的。
View Code
#include "iostream"
#include "queue"
using namespace std;
#define N 10000
#define Max 0x7FFFFFFF
typedef struct Node{
    int u, value, next;
}node;
node e[N];
int n, m;
int dis[N], p[N];
bool vis[N];
void init(){
    memset(p, -1, sizeof(p));
    memset(vis, false, sizeof(vis));
    fill(dis, dis+N, Max);
    int temp = 0; 
    int a, b, c;
    for(int i=0; i<n; i++){
        cin>>a>>b>>c;
        e[temp].u = b;
        e[temp].value = c;
        e[temp].next = p[a];
        p[a] = temp;
        temp ++;
        e[temp].u = a;
        e[temp].value = c;
        e[temp].next = p[b];
        p[b] = temp;
        temp++;
    }
}
void spfa(int s){
    dis[s] = 0;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int t = q.front();
        q.pop();
        vis[t] = false;
        int j;
        for(j=p[t]; j!=-1; j=e[j].next){
            int w=e[j].value;
            int temp = e[j].u;
            if(w+dis[t] < dis[temp]){
                dis[temp] = w+dis[t];
                if(!vis[temp]){
                    vis[temp] = true;
                    q.push(temp);
                }
            }
        }
    }
}
int main()
{
    while(cin>>n>>m && (n+m))
    {
        init();
        spfa(1);
        cout<<dis[m]<<endl;
    }
    return 0;
}

 

 

转载于:https://www.cnblogs.com/o8le/archive/2011/10/07/2200400.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值