POJ2387 Til the Cows Come Home(最短路,Dijkstra算法,spfa算法,Floyd算法,深搜DFS)

题目:

Til the Cows Come Home

Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 47642 Accepted: 16216

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.

Source

USACO 2004 November

[Submit]   [Go Back]   [Status]   [Discuss]

思路:

给了一个无向图,输入t和n, t代表几个顶点,n代表问的是从第一个顶点到第n的顶点的最短距离,各种最短路算法的模板题

代码1(迪杰斯特拉算法AC):

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=1<<29;
int map[1010][1010];//map[i][j]表示从i-->j的距离  
int dist[1010];//dist[i]从v1到i的距离  
int vis[1010];//标记有没有被访问过  
void dijkstra(int n)
{
    int k,min;
    for(int i=1; i<=n; i++)
    {
        dist[i]=map[1][i];
        vis[i]=0;
    }
    for(int i=1; i<=n; i++)//遍历顶点  
    {
        k=0;
        min=inf;
        for(int j=1; j<=n; j++)
            if(vis[j]==0&&dist[j]<min)
            {
                min=dist[j];
                k=j;
            }
        vis[k]=1;
        for(int j=1; j<=n; j++)
            if(vis[j]==0&&dist[k]+map[k][j]<dist[j])
                dist[j]=dist[k]+map[k][j];//如果找到了通路就加上 
    }
    return;
}
int main()
{
    int t,n,a,b,w;
    while(~scanf("%d%d",&t,&n))
    {
        mem(map,0);
        mem(vis,0);
        mem(dist,0);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                map[i][j]=inf;//初始化为无穷大  
        for(int i=1; i<=t; i++)
        {
            scanf("%d%d%d",&a,&b,&w);
            if(w<map[a][b])
            {
                map[a][b]=w;
                map[b][a]=map[a][b];//建立无向图
            }//这里是判断是否有重边,应为两点之间的路,未必只有一条。
        }
        dijkstra(n);
        printf("%d\n",dist[n]);
    }
    return 0;
}

代码2(深搜DFS,未AC,超时了):

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=1<<29;
int map[1010][1010];
int vis[1010];
int minn,n;
void dfs(int i,int step)
{
    if(step>minn)return;
    if(i==n)
    {
        if(step<minn)
            minn=step;
        return;
    }
    for(int j=1;j<=n;j++)
    {
        if(map[i][j]!=inf&&vis[j]==0)//判断当前的位置有值
        {
            vis[j]=1;
            dfs(j,step+map[i][j]);
            vis[j]=0;
        }
    }
    return;
}
int main()
{
    int t,a,b,w;
    while(~scanf("%d%d",&t,&n))
    {
        minn=inf;
        mem(map,0);
        mem(vis,0);
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(i==j)
                    map[i][j]=0;
                else
                    map[i][j]=inf;
        for(int i=1;i<=t;i++)
        {
            scanf("%d%d%d",&a,&b,&w);
            if(w<map[a][b])
            {
                map[a][b]=w;
                map[b][a]=map[a][b];
            }//去重复边
        }
        vis[1]=1;
        dfs(1,0);
        printf("%d\n",minn);
    }
    return 0;
}

代码3(弗洛伊德(Floyd)算法)(未AC,超时):

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int inf=1<<29;
int map[1010][1010];//map[i][j]表示从i-->j的距离
int main()
{
    int t,n,a,b,w;
    while(~scanf("%d%d",&t,&n))
    {
        mem(map,0);
        //初始化
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                if(i==j)
                    map[i][j]=0;
                else
                    map[i][j]=inf;//初始化为无穷大
        //建立图
        for(int i=1; i<=t; i++)
        {
            scanf("%d%d%d",&a,&b,&w);
            if(w<map[a][b])
            {
                map[a][b]=w;
                map[b][a]=map[a][b];//建立无向图
            }//这里是判断是否有重边,应为两点之间的路,未必只有一条。
        }
        //弗洛伊德(Floyd)核心语句
        for(int k=1; k<=n; k++)
            for(int i=1; i<=n; i++)
                for(int j=1; j<=n; j++)
                    if(map[i][k]+map[k][j]<map[i][j])
                        map[i][j]=map[i][k]+map[k][j];
        printf("%d\n",map[1][n]);
    }
    return 0;
}

代码4(贝尔曼福特算法的队列优化(spfa)):

#include <stdio.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
#define N 4000+10
using namespace std;
int n,m,len;
struct node
{
    int u,v,w;
}G[N];
int first[N],next[N],dis[N],vis[N];
void add_edge(int u,int v,int w)
{
    G[len].u=u,G[len].v=v,G[len].w=w;
    next[len]=first[u];
    first[u]=len++;
}
void spfa(int st)
{
    for(int i=1;i<=n;i++)
    {
        dis[i]=inf;
        vis[i]=0;
    }
    dis[st]=0;
    vis[st]=1;
    queue<int>q;
    q.push(st);
    while(!q.empty())
    {
        st=q.front();
        q.pop();
        vis[st]=0;
        for(int i=first[st];i!=-1;i=next[i])
        {
            if(dis[G[i].v]>dis[st]+G[i].w)
            {
                dis[G[i].v]=dis[st]+G[i].w;
                if(!vis[G[i].v])
                {
                    vis[G[i].v]=1;
                    q.push(G[i].v);
                }
            }
        }
    }

}
int main()
{
    int x,y,z;
    while(~scanf("%d%d",&m,&n))
    {
        mem(dis,0);mem(vis,0);mem(first,-1);mem(next,-1);
        len=1;
        for(int i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            add_edge(x,y,z);
            add_edge(y,x,z);
        }
        spfa(1);
        printf("%d\n",dis[n]);
    }
    return 0;
}

在本题中,我用的方法可能多了一点,但是身为一道模板题,先保证答案对再说~

    原文作者:Dijkstra算法
    原文地址: https://blog.csdn.net/riba2534/article/details/54428324
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞