Djikstra算法

Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

这一专题讲解最短路算法,最短路算法顾名思义就是求解两点之间的最短路径,在数据结构中出现过,常用的有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 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. 
模板题之所以称为模板题,就是因为简单、直白、粗暴的解题引导,明摆着求最短路。
废话不多说,题意大概就是求点一到点N的最短路径。
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define maxsize 1010
#define inf 99999999
int 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;
}

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

您可能感兴趣的与本文相关的镜像

Wan2.2-T2V-A5B

Wan2.2-T2V-A5B

文生视频
Wan2.2

Wan2.2是由通义万相开源高效文本到视频生成模型,是有​50亿参数的轻量级视频生成模型,专为快速内容创作优化。支持480P视频生成,具备优秀的时序连贯性和运动推理能力

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值