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.

最短路径的裸题,目前只会最复杂的dijkstra算法,还没有优化

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define INF (0x3f3f3f3f)
/*
dijktra
*/
const int Max=2000+10;
int mp[Max][Max];
int T,N;
int vis[Max];
void dijkstra()
{
    int d[Max];

    int i,j;
    for(i=1;i<=N;i++)
    {
        vis[i]=0;
        d[i]=mp[1][i];//直接和1相邻的距离

    }

    int v;
    for(i=1;i<=N;i++)
    {
        int Min=INF;
        for( j=1;j<=N;j++)//找到和i相邻的并且没有访问过的最小的点
        {
            if(!vis[j]&&d[j]<Min)
            {
                v=j;
                Min=d[j];
            }

        }
        vis[v]=1;
        for( j=1;j<=N;j++)
        {
            if( !vis[j] && d[j]>mp[v][j]+d[v])
            {
                d[j]=mp[v][j]+d[v];
            }
        }

    }
    printf("%d\n",d[N]);
}

int main()
{
  while(~scanf("%d %d",&T,&N))
  {
      for(int i=1;i<=N;i++)
        for(int j=1;j<=N;j++)
          if(i==j)
            mp[i][j]=0;
          else
            mp[i][j]=mp[j][i]=INF;
      for(int i=1;i<=T;i++)
      {
          int u,v,w;
          scanf("%d %d %d",&u,&v,&w);
          if(w<mp[u][v])//防止重边
          {
              mp[u][v]=mp[v][u]=w;
          }

      }
      dijkstra();
  }
   return 0;
}

用优先队列实现的dijkstra,不过实在是很麻烦

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<string>
using namespace std;
const int maxn=4010;//因为是双向边,所以我们需要是T*2
const int INF=0x3f3f3f3f;
int cnt;//记录边所在的下标
int T,N;
int head[maxn],dis[maxn];//dis[]表示距离原点的距离 ,head【cnt】表示的是同起点的下一条边的存储下标cnt
int vis[maxn];//这是用来标记已经在访问之列的

typedef struct node//创建节点的目的是为了用在队列里面找到距离原点最小的
{
    int point,distance;//起点
    //这种赋值的方式第一次见,先学习
    node(){}
    node(int _point,int _distance)
    {
        point=_point,distance=_distance;
    }
    //慢慢学习这种结构
    bool operator <(const node &other)const
    {
        return distance > other.distance;
    }
}Node;

struct edge
{
    int v,w,next;//同起点的下一条变得存储位置
}Edge[maxn];

void add_edge(int u,int v,int w)//有多少条边,就会建立多少个Edge[]。至于同起点边的联系,前向星
{
    Edge[cnt].v=v;
    Edge[cnt].w=w;
    Edge[cnt].next=head[u];//上一个u的存储位置
    head[u]=cnt++;//更新这个u起点的存储下标
}

void Dijkstra(int s)
{
    for(int i=0;i<=N;i++)
        dis[i]=INF;
    memset(vis,0,sizeof(vis));
    Node now,next;
    dis[s]=0;//自己距离自己是0
    priority_queue<node> pq;
    pq.push(node(s,dis[s]));
    while(pq.size())
    {
        now=pq.top();
        pq.pop();
    //    if(vis[now.point])没有必要写这个
    //        continue;
    //    vis[now.point]=1;
        for(int i=head[now.point];i!=-1;i=Edge[i].next)//i是存储边的下标,访问的从后往前
        {
            int to=Edge[i].v;
            if(dis[to]>dis[now.point]+Edge[i].w)//找到同起点的边中能松弛的点,压进队列
                                                //因为我们始终是找的最短的那个点,所以也不需要担心这个
                                                //会不会再次更短的可能
            {
                dis[to]=dis[now.point]+Edge[i].w;
                pq.push(node(to,dis[to]));
            }
        }
    }
}

int main()
{
    memset(head,-1,sizeof(head));
    scanf("%d %d",&T,&N);
    cnt=0;
    while(T--)
    {
        int u,v,w;
        scanf("%d %d %d",&u,&v,&w);
        add_edge(u,v,w);
        add_edge(v,u,w);//存储这张图,因为边是双向的,但是我们存储的时候却是只记录下末点
                         //所以要想两点能够互相找到的话,我们就得直接建立双向边
    }
    Dijkstra(1);//起点
    cout<<dis[N]<<endl;
    return 0;
}

SPFA,还是这种方式比较好,代码少,再就是可以看负数,不过不能是负数环

#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<queue>
#include<string>
using namespace std;
const int maxn=4010;//因为是双向边,所以我们需要是T*2
const int INF=0x3f3f3f3f;
int cnt;//记录边所在的下标
int T,N;
int head[maxn],dis[maxn];//dis[]表示距离原点的距离 ,head【cnt】表示的是同起点的下一条边的存储下标cnt
int vis[maxn];//这是用来标记已经在访问之列的
int time[maxn];//这应该是用来标记负环的,防止访问次数过多,还不太懂

struct node
{
    int v,w;
    int next;
}Edge[maxn];

void add_edge(int u,int v,int w)
{
    int a;
    Edge[cnt].v=v;
    Edge[cnt].w=w;
    Edge[cnt].next=head[u];
    head[u]=cnt++;
}

int  Spfa(int s,int tol)
{
    memset(time,0,sizeof(time));
    memset(vis,0,sizeof(vis));
    for(int i=0;i<=N;i++)
        dis[i]=INF;
    dis[s]=0;vis[s]=1;time[s]++;
    queue<int> q;
    q.push(s);
    while(q.size())
    {
        int tmp=q.front();
        q.pop();
        vis[tmp]=0;//扔掉之后,还是可以继续放进来的
        for(int i=head[tmp];i!=-1;i=Edge[i].next)
        {
            int to=Edge[i].v;
            if(dis[tmp]+Edge[i].w<dis[to])
            {


                dis[to]=dis[tmp]+Edge[i].w;
                if(!vis[to])
                {
                    vis[to]=1;
                    q.push(to);
                    time[to]++;
                    if(time[to]>=N)
                        return -1;//存在负环
                }

            }
        }
    }
    return dis[N];
}

int main()
{
    memset(head,-1,sizeof(head));//标记截止符,这个东西,必须放在前面,这是一个位置信息
    scanf("%d %d",&T,&N);
    cnt=0;
    while(T--)
    {

        int u,v,w;
        scanf("%d %d %d",&u,&v,&w);
        add_edge(u,v,w);
        add_edge(v,u,w);//存储这张图,因为边是双向的,但是我们存储的时候却是只记录下末点
                         //所以要想两点能够互相找到的话,我们就得直接建立双向边
    }
    int ans=Spfa(1,N);//起点
    cout<<ans<<endl;
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值