hiho 23 最短路径 一(Dijstra)

来源:互联网 发布:阿根廷专利数据库 编辑:程序博客网 时间:2024/05/16 07:51

问题描述

单源最短路径。
http://hihocoder.com/contest/hiho23/problem/1

解决方法

算法复杂度: 顶点个数n, 边个数m, O(n*m)

#include <vector>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;int N, M, S, T;enum {maxn = 1000+5, MAX_INT= 1<<30};struct E{    int n;    int dis;};vector<E> g[maxn];struct Node{    bool vis;    int dis;};Node MinDis[maxn];int main(){    //freopen("in.txt", "r", stdin);    scanf("%d %d %d %d", &N, &M, &S, &T);    memset(MinDis, 0, sizeof(MinDis));    MinDis[S].vis = true;    MinDis[S].dis = 0;    for (int i=0; i< M; i++)    {        int a, b, dis;        scanf("%d %d %d", &a, &b, &dis);        g[a].push_back(E{b, dis});        g[b].push_back(E{a, dis});    }    while(1){        int minPos = 0;        int minDis = MAX_INT;        for (int i=1; i<=N; i++){            if (MinDis[i].vis)            {                for (int j=0; j< g[i].size(); j++)                {                    if (MinDis[g[i][j].n].vis == false && MinDis[i].dis + g[i][j].dis < minDis)                    {                        minPos = g[i][j].n;                        minDis = MinDis[i].dis + g[i][j].dis;                    }                }            }        }        MinDis[minPos].vis = true;        MinDis[minPos].dis = minDis;        if (minPos == T)            break;    }    printf("%d\n", MinDis[T].dis);    return 0;}

使用堆优化的Dijstra
复杂度O(mlogm) m为边的个数;

#include <bits/stdc++.h>using namespace std;enum {maxn = 1000+5};class E{public:    E(int bb, int l):b(bb), len(l){};    int b;    int len;};class cmp{public:    bool operator()(E a, E b)    {        return a.len > b.len;    }};vector<E> G[maxn];int sp[maxn];int N, M, S, T;int main(){    scanf("%d %d %d %d", &N, &M, &S, &T);    for (int i=0; i< M; i++)    {        int a, b, l;        scanf("%d %d %d", &a, &b, &l);        G[a].push_back(E(b, l));        G[b].push_back(E(a, l));    }    memset(sp, -1, sizeof(sp));    sp[S] = 0;    priority_queue<E, vector<E>, cmp> A;    int last = S;    while(sp[T]<0)    {        // add last's E;        for (int i=0; i< G[last].size(); i++)            if (sp[G[last][i].b] < 0)                A.push(E(G[last][i].b, sp[last]+ G[last][i].len));        E now(1, 1);        for(now = A.top(), A.pop(); sp[now.b] >=0; now = A.top(), A.pop())            ;        last = now.b;        sp[now.b] = now.len;    }    printf("%d\n", sp[T]);    return 0;}
0 0
原创粉丝点击