poj 2387 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.
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.
 
题目给出每两个点之间距离,要求求出n到1最短路径,由于路是互通的。
Bellman-Ford  算法:
#include <iostream>
#include <cstdlib>
#include <queue>
#include <cstring>
#include <cstdio>
#define inf 1000000000
using namespace std;

int main()
{
    int m,n,check;
    int u[4000],v[4000],w[4000],dis[1001]={0};
    cin>>m>>n;
    for(int i=1;i<=n-1;i++)
    dis[i]=inf;
    dis[n]=0;
    for(int i=0;i<m;i++)
    {
        cin>>u[i]>>v[i]>>w[i];
    }
    for(int i=m;i<m+m;i++)//反向记录,保证路互通
    {
        u[i]=v[i-m];
        v[i]=u[i-m];
        w[i]=w[i-m];
    }
    for(int i=0;i<n-1;i++)
    {
        check=0;
        for(int j=0;j<m*2;j++)
            if(dis[v[j]]>dis[u[j]]+w[j])dis[v[j]]=dis[u[j]]+w[j],check=1;
        if(!check)break;
    }
    cout<<dis[1];
}

Dijkstra 算法:

#include <iostream>
#define inf 1000000000
#define FLAG 0
using namespace std;
int mp[1001][1001];
int main()
{
    int t,n,u,v,w,book[1001]={0},mind=0,mi;
    int dis[1001];
    cin>>t>>n;
    //initialized
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
            mp[i][j]=inf;
        mp[i][i]=0;
    }
    ///input
    for(int i=0;i<t;i++)
    {
        cin>>u>>v>>w;
        if(mp[u][v]>w)mp[u][v]=mp[v][u]=w;///both-way update the minmum value
    }
    //dijkstra
    for(int i=1;i<=n;i++)
    dis[i]=mp[n][i];
    #if(FLAG)
    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        cout<<mp[i][j]<<' ';
        cout<<endl;
    }
    #endif
    for(int i=1;i<=n;i++)
    {
        mind=inf;
        for(int j=1;j<=n;j++)
        if(!book[j]&&dis[j]<mind)mind=dis[j],mi=j;//found the minmum one
        book[mi]=1;
        for(int j=1;j<=n;j++)
        if(!book[j]&&dis[j]>dis[mi]+mp[mi][j])dis[j]=dis[mi]+mp[mi][j];
    }
    cout<<dis[1];
}

 邻接表版:

#include <iostream>
#include <map>
#include <queue>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define inf 10001
using namespace std;
int first[4002],next[4002];
int u[4002],v[4002],w[4002];
int dis[1001],vis[1001];
int main()
{
    int N,T,k;
    cin>>T>>N;
    memset(dis,inf,sizeof(dis));
    memset(first,-1,sizeof(next));
    for(int i = 0;i < T;i ++)//无向图
    {
        cin>>u[i]>>v[i]>>w[i];
        u[T + i] = v[i];
        v[T + i] = u[i];
        w[T + i] = w[i];
    }
    for(int i = 0;i < T * 2;i ++)
    {
        next[i] = first[u[i]];
        first[u[i]] = i;
    }
    queue<int>q;
    q.push(N);//N点起始
    dis[N] = 0;
    vis[N] = 1;
    while(!q.empty())
    {
        k = first[q.front()];
        while(k != -1)
        {
            if(dis[q.front()] + w[k] < dis[v[k]])
            {
                dis[v[k]] = w[k] + dis[q.front()];
                if(!vis[v[k]])///如果v[k]没有入队,且到v[k]的距离宽松了,就入队
                {
                    vis[v[k]] = 1;
                    q.push(v[k]);
                }
            }
            k = next[k];
        }
        vis[q.front()] = 0;
        q.pop();
    }
    cout<<dis[1];//终点1
}
原文地址:https://www.cnblogs.com/8023spz/p/7273250.html