Poj 2387 Til the Cows Come Home

Til the Cows Come Home
Time Limit: 1000MS
Memory Limit: 65536K
Total Submissions: 47474
Accepted: 16143

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

题解:题意很简单就是两点之间的最短路……裸的不要不要的……

Djkstra算法:其实就两个数组和一个邻接矩阵……Djkstra太简单了……直接水之……

代码如下

#include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<queue>;
#include<stack>;
#define INF 99999999
#define MAX 1005
using namespace std;
int n,t;
int dij[MAX],v[MAX];               /*dij[i]为起点到i的距离,v[]存储标记过的点*/
int w[MAX][MAX];                    /*w[i][j]为邻接矩阵,表示i到j的距离*/
void init()                           /*初始化*/
{
    for(int i=1; i<MAX; i++)       
    {
        dij[i]=i==1?0:INF;         /*起点距离为零,其他全部初始为无穷大*/
        v[i]=0;                    /*全部标记为未被访问过*/
        for(int j=1; j<MAX; j++)   /*初始为无穷大*/
            w[i][j]=INF;
    }
}
void dijkstra()
{
    for(int i=1; i<=n; i++)        /*遍历……*/
    {
        int x=0,m=INF;
        for(int j=1; j<=n; j++)    /*寻找最小边……*/
        {
            if(!v[j]&&dij[j]<m)     
                m=dij[x=j];
        }
        v[x]=1;
        for(int j=1; j<=n; j++)    /*更新边的权值……*/
        {
            if(!v[j]&&w[x][j]+dij[x]<dij[j])
                dij[j]=dij[x]+w[x][j];
        }
    }
}
int main()
{
    int a,b,c;
    while(cin>>t>>n)
    {
        init();
        for(int i=1; i<=t; i++)
        {
            cin>>a>>b>>c;
            if(w[a][b]>c)       /*防止一条边多次输入,且权值不同,所以直接暴力找最小的……*/
            {
                w[a][b]=w[b][a]=c;
            }
        }
        dijkstra();
        cout<<dij[n]<<endl;
    }
}

spfa算法:

spfa相比dijkstra时间复杂度降低了……而且可以处理有负权(但它无法处理带负环的最短路)的单源最短路……

#include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdlib.h>
#include<time.h>
#include<math.h>
#include<queue>;
#include<stack>;
#define INF 0x3f3f3f3f
#define MAX 1005
using namespace std;
int n,t;
int d[MAX],w[MAX][MAX];
bool v[MAX];
void spfa()
{
    queue<int>q;
    q.push(1);                       /*源点入队……*/
    v[1]=true;                       /*表示已进队*/
    while(!q.empty())
    {
        int x=q.front();
        q.pop();                     /*删减*/
        v[x]=false;                  /*重新赋予入队资格……*/
        for(int i=1; i<=n; i++)       /*对x临近的都进行松弛操作……*/
        {
            if(d[x]+w[x][i]<d[i])     /*更新权值……*/
            {
                d[i]=d[x]+w[x][i];
                if(!v[i])             /*对更新过权值的并且没有入队的点进行入队操作*/
                {
                    q.push(i);
                    v[i]=true;        /*表示已入队&*/
                }
            }
        }
    }
}
int main()
{
    int a,b,c;
    while(cin>>t>>n)
    {
        memset(d,INF,sizeof(d));     /*0x3f3f3f3f可以被memset赋值*/
        d[1]=0;
        memset(v,false,sizeof(v));
        memset(w,INF,sizeof(w));
        for(int i=1; i<=t; i++)
        {
            cin>>a>>b>>c;
            if(w[a][b]>c)
                w[a][b]=w[b][a]=c;   /*一边多次输入,且权值不一样,所以直接取最小的权值*/
        }
        spfa();
        cout<<d[n]<<endl;
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值