大二带专生


私信TA

用户名:dotcpp0676254

访问量:343

签 名:

等  级
排  名 8772
经  验 1147
参赛次数 0
文章发表 18
年  龄 0
在职情况 学生
学  校 职业技术学院
专  业

  自我简介:

解题思路:

注意事项:

参考代码:

#include<iostream>

#include<cstring>

#include<queue>

#include<algorithm>

using namespace std;


const int N = 5e6 + 10;

int n, m;

int h[N], e[N], ne[N], idx;

int dist[N], w[N];

bool st[N];


void add(int a, int b, int c)

{

    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;

}


int spfa()

{

    memset(dist, 0x3f, sizeof dist);

    dist[1] = 0;


    queue<int>q;

    q.push(1);

    st[1] = true;


    while (q.size())

    {

        int t = q.front();

        q.pop();

        st[t] = false;

        for (int i = h[t]; i != -1; i=ne[i])

        {

            int j = e[i];

            if (dist[j] > dist[t] + w[i])

            {

                dist[j] = dist[t] + w[i];

                if (!st[j])

                {

                    q.push(j);

                    st[j] = true;

                }

            }

        }

    }

    if (dist[n] == 0x3f3f3f3f)

        return -1;

    else

        return dist[n];

}


int main()

{

    memset(h, -1, sizeof h);

    scanf("%d%d", &n, &m);

    for(int i=0;i<m;i++)

    {

        int a, b, w;

        scanf("%d%d%d", &a, &b, &w);

        add(a, b, w);

        add(b, a, w);

    }

    int t = spfa();

    printf("%d\n", t);

    return 0;


}


 

0.0分

2 人评分

看不懂代码?想转换其他语言的代码? 或者想问其他问题? 试试问问AI编程助手,随时响应你的问题:

编程语言转换

万能编程问答  

代码解释器

代码纠错

SQL生成与解释

  评论区