Til the Cows Come Home 【SPFA】 【djk】

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.

不多说,模版题,只是为了熟悉下两种算法
第一种 SPFA + 邻接表 (建图快一些) 63 ms
代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#define CLR(a,b) memset((a),(b),sizeof(a))
#define inf 0x3f3f3f3f
#define mod 100009
#define LL long long
#define M  10000
#define ll o<<1
#define rr o<<1|1
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
using namespace std;
struct edge {int to,cost;};
vector<edge >G[M];
int dis[M];
int n,m;
void init(){ for(int i=1;i<=n;i++) dis[i]=inf,G[i].clear(); }
void getmap()
{
    int i,j;
    for(i=1;i<=m;i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        G[a].push_back({b,c});
        G[b].push_back({a,c});
    }
}
void spfa(int st,int ed)
{
    dis[st]=0;int i,j;
    queue<int>Q;
    Q.push(st);
    while(!Q.empty())
    {
        int next=Q.front();Q.pop();
        for(i=0;i<G[next].size();i++)
        {
            edge e=G[next][i];
            if(dis[e.to]>dis[next]+e.cost)
            {
                dis[e.to]=dis[next]+e.cost;
                Q.push(e.to);
            }
        }
     }
     printf("%d\n",dis[ed]) ;
}
int main()
{
    scanf("%d%d",&m,&n);
    init();
    getmap();
    spfa(n,1);
    return 0;
}

第二种 DJK + 邻接表 用优先队列维护的,时间复杂度为 E*logV 94ms
代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#define CLR(a,b) memset((a),(b),sizeof(a))
#define inf 0x3f3f3f3f
#define mod 100009
#define LL long long
#define M  10000
#define ll o<<1
#define rr o<<1|1
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
using namespace std;
typedef struct pair <int ,int >P;
struct edge{int to,cost;};
vector<edge>G[M];
int dis[M];
int n,m;
void init(){ for(int i=1;i<=n;i++) G[i].clear(),dis[i]=inf;}
void getmap()
{
    int i,j;
    for(i=0;i<m;i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        G[a].push_back({b,c});
        G[b].push_back({a,c});
    }
}
void djk(int st,int ed)
{
    int i,j;
    priority_queue<P ,vector <  P > ,greater  < P  > > Q;
    dis[st]=0;Q.push({0,st});
    while(!Q.empty())
    {
        P now = Q.top();Q.pop();
        int value=now.first;int next=now.second;
        if(dis[next]<value) continue;
        for(i=0;i<G[next].size();i++)
        {
            edge e=G[next][i];
            if(dis[next]+e.cost<dis[e.to])
            {
                dis[e.to]=dis[next]+e.cost;
                Q.push({dis[e.to],e.to});
                }   
        }
    }
    printf("%d\n",dis[ed]);
}
int main()
{
    scanf("%d%d",&m,&n);
    init();
    getmap();
    djk(n,1);
    return 0;
}

第三种 SPFA + 链式向前星 (手动建链表) 不到 20ms
代码

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<vector>
#include<set>
#define CLR(a,b) memset((a),(b),sizeof(a))
#define inf 0x3f3f3f3f
#define mod 100009
#define LL long long
#define M 100000
#define ll o<<1
#define rr o<<1|1
#define lson o<<1,l,mid
#define rson o<<1|1,mid+1,r
using namespace std;
int n,m;
struct edge{int to,cost,next;}G[M];
int dis[M],vis[M],head[M],top;
void addedge(int a,int b,int c)
{
    G[top].to=b;
    G[top].cost=c;
    G[top].next=head[a];
    head[a]=top++;
}
void init()
{
    top=0;
    for(int i=1;i<=n;i++)  // 这里决定了,点的序号是从 0 or 1 开始的  
    {
        head[i]=-1;
        vis[i]=0;
        dis[i]=inf;
    }
}
void getmap()
{
    int i,j;
    for(i=0;i<m;i++)
    {
        int a,b,c;
        scanf("%d%d%d",&a,&b,&c);
        addedge(a,b,c);
        addedge (b,a,c);
    }
}
void spfa(int st,int ed)
{
    dis[st]=0;vis[st]=1;
    queue<int>Q;
    Q.push(st);
    while(!Q.empty())
    {
        int now=Q.front();Q.pop();
        vis[now]=0;
        for(int i=head[now];i!=-1;i=G[i].next)
        {
            int next=G[i].to;
            if(dis[next]>dis[now]+G[i].cost)
            {
                dis[next]=dis[now]+G[i].cost;
                if(!vis[next])  //  小优化,不会让多余的点进
                {
                    vis[next]=1;
                    Q.push(next);
                }
            }
        }
    }
    printf("%d\n",dis[ed]);
}
int main()
{
    scanf("%d%d",&m,&n);
    init();
    getmap();
    spfa(n,1);
    return 0;
}

第四种 djk + 邻接矩阵 当图的边比较稠密时候,这个会更快 //93ms
代码

#include<stdio.h>  
#include<string.h>  
#include<algorithm>  
#include<math.h>  
#define inf 0x3f3f3f  
#define mod 1000007  
#define LL long long   
#define M 1000+10   
using namespace std;  
int t,n;  
int map[M][M];  
int v[M];  
int dis[M];  
void getmap()  
{  
    int i,j;  
    for(i=1;i<=n;i++)  
    for(j=1;j<=n;j++)  
    if(j==i) map[i][j]=0;  
    else map[i][j]=map[j][i]=inf;  

    for(i=1;i<=t;i++)  
    {  
        int a,b,c;  
        scanf("%d%d%d",&a,&b,&c);  
        if(map[a][b]>c)  
        map[a][b]=map[b][a]=c;  
    }  
}    
void djk(int st,int ed)  
{  
    int i,j,k,u;  
    int min,next;  
    for(i=1;i<=n;i++)  
    {  
        v[i]=0;  
        dis[i]=map[st][i];  
     }  
     v[st]=1;  
     for(u=2;u<=n;u++)  
     {  
        min=inf;  
        for(j=1;j<=n;j++)  
        {  
            if(!v[j]&&dis[j]<min)  
            {  
                min=dis[j];  
                next=j;  
             }  
        }  
             if(min==inf)  
              break;  
              v[next]=1;  
              for(j=1;j<=n;j++)  
              {  
                if(!v[j]&&dis[j]>dis[next]+map[next][j])  
                dis[j]=dis[next]+map[next][j];  
              }  

     }  
     printf("%d\n",dis[ed]);  
}  
int main()  
{  
    scanf("%d%d",&t,&n);  
    getmap();  
    djk(n,1);  

    return 0;  
 }   
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值