Djikstra算法

来源:互联网 发布:百度seo规则 编辑:程序博客网 时间:2024/06/03 21:53

这一专题讲解最短路算法,最短路算法顾名思义就是求解两点之间的最短路径,在数据结构中出现过,常用的有Djikstra和Floyed算法,本文先介绍Djikstra算法。
同样,为了帮助理解,先放一道模板题:
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 51 2 202 3 303 4 204 5 201 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的最短路径。
#include <iostream>#include <cstdio>#include <cstring>using namespace std;#define maxsize 1010#define inf 99999999int map[maxsize][maxsize];//存储邻接矩阵int dis[maxsize],used[maxsize];//dis表示起点到某一点的最短距离,used表示这一点是否找到最短路径int n,m,a,b,c;void djikstra(int start,int end,int size)//start表示起点,end是终点,size表示点的总个数{    int i,cnt=1;    memset(used,0,sizeof(used));//初始化    used[start]=1;//起点到自己最短,所以起点已经找到最短点,标记为1    //当前起点到各点最短路径初始化为起点到各点的直接距离,联通则为边的长度,不联通则为无穷大    for(i=1;i<=size;i++)        {            if(map[start][i]>0)            {                dis[i]=map[start][i];            }            else if(i==start)            {                dis[i]=0;            }            else            {                dis[i]=inf;            }        }    //这个循环是由于有size个点,只有每一个点都找到最短路径才算完成    for(cnt=1;cnt<=size;cnt++)    {        //第一步是找到最小值        int min=inf,pos;        for(i=1;i<=size;i++)        {            if(used[i]==0&&dis[i]<min)//保证这个点有找到的价值            {                min=dis[i];                pos=i;            }        }        used[pos]=1;//这个点找到最小值        //第二步是由  当前找到的点  更新 各 未找到最短路径的点 到起点的路径        //如果经过当前找到的最近点到某节点的通路小于原路径,则更新        for(i=1;i<=size;i++)        {            if(map[i][pos]>0&&used[i]==0&&dis[i]>min+map[i][pos])            {                dis[i]=min+map[i][pos];            }        }    }    printf("%d\n",dis[end]);}int main(){    while(scanf("%d%d",&m,&n)!=EOF)    {        int i;        //矩阵初始化        for(i=1;i<=n;i++)        {            for(int j=1;j<=n;j++)            {                map[i][j]=0;            }        }        //矩阵输入        for(i=1;i<=m;i++)        {            scanf("%d%d%d",&a,&b,&c);            if((map[a][b]>0&&c<map[a][b])||map[a][b]==0)            map[a][b]=c;            map[b][a]=c;        }        //算法主体        djikstra(1,n,n);    }    return 0;}

该算法的核心是不断找到 当前情况下 新延伸出来的 已找到最短路径的点,并用这个点更新各点到起点的路径长度。
值得说明的是,经过这个点可能优化原路径,但也可能不如原来的方案。

原创粉丝点击