Algoritmo de Dijkstra para el camino más corto

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
/*
* 单源最短路径,Dijkstra 算法,邻接矩阵形式,复杂度为O(n^2)
* 求出源beg 到所有点的最短路径,传入图的顶点数,和邻接矩阵cost[][]
* 返回各点的最短路径lowcost[], 路径pre[].pre[i] 记录beg 到i 路径上的
    父结点,pre[beg]=-1
* 可更改路径权类型,但是权值必须为非负
*/
const int MAXN=1010;
const int INF=0x3f3f3f3f;//防止后面溢出,这个不能太大
bool vis[MAXN];
int pre[MAXN];

void Dijkstra(int cost[][MAXN],int lowcost[],int n,int beg)
{
    for(int i=1; i<=n; i++)
    {
        lowcost[i]=INF;
        if(lowcost[i]>cost[beg][i]) lowcost[i]=cost[beg][i];
        vis[i]=false;
        pre[i]=-1;
    }
    lowcost[beg]=0;
    vis[beg] = 1;
    for(int j=1; j<=n; j++)
    {
        int k=-1;
        int Min=INF;
        for(int i=1; i<=n; i++)
            if(!vis[i]&&lowcost[i]<Min)
            {
                Min=lowcost[i];
                k=i;
            }
        if(k==-1)
            break;
        vis[k]=true;
        for(int i=1; i<=n; i++)
            if(!vis[i]&&lowcost[k]+cost[k][i]<lowcost[i]&&cost[k][i]<INF)
            {
                lowcost[i]=lowcost[k]+cost[k][i];
                pre[i]=k;
            }
    }
    if(lowcost[n]!=INF)
        printf("%d\n", lowcost[n]);
    else printf("-1\n");
}
int cost[MAXN][MAXN];
int lowcost[MAXN];
int main()
{
    int n,m;
    while (scanf("%d%d", &n,&m)!=EOF)
    {

        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= n; j++)
            {
                cost[i][j] =INF;
            }
        }
        int a,b,c;
        for(int i=0; i<m; i++)
        {
            cin>>a>>b>>c;
            if(cost[a][b]>c){
                cost[a][b] = c;
                cost[b][a] = c;
            }
        }
        Dijkstra(cost, lowcost,n,1);
    }
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100
*/

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN=1010;
const int INF=0x3f3f3f3f;//防止后面溢出,这个不能太大
void Floyd(int cost[][MAXN],int n,int beg)
{
    for(int k=1; k<=n; k++)
    {
        for(int i=1; i<=n; i++)
        {
            for(int j=1; j<=n; j++)
            {
                if(cost[i][j]>cost[i][k]+cost[k][j])
                    cost[i][j]=cost[i][k]+cost[k][j];
            }
        }
    }
    printf("%d\n", cost[beg][n]);
}
int cost[MAXN][MAXN];
int main()
{
    int n,m;
    while (scanf("%d%d", &n,&m)!=EOF)
    {

        for (int i = 0; i <= n; i++)
        {
            for (int j = 0; j <= n; j++)
            {
                cost[i][j] =INF;
                if(i==j) cost[i][j] = 0;
            }
        }
        int a,b,c;
        for(int i=0; i<m; i++)
        {
            cin>>a>>b>>c;
            if(cost[a][b]>c){
                cost[a][b] = c;
                cost[b][a] = c;
            }
        }
        Floyd(cost,n,1);
    }
}
/*
5 5
1 2 20
2 3 30
3 4 20
4 5 20
1 5 100


*/

 

Supongo que te gusta

Origin blog.csdn.net/weixin_42137874/article/details/110409903
Recomendado
Clasificación