解题思路:
注意事项:
参考代码:
#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 人评分
C二级辅导-进制转换 (C语言代码)浏览:831 |
C语言程序设计教程(第三版)课后习题6.4 (C语言代码)浏览:653 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:561 |
C语言程序设计教程(第三版)课后习题1.5 (C语言代码)浏览:589 |
WU-格式化数据输出 (C++代码)浏览:1212 |
C语言程序设计教程(第三版)课后习题6.6 (C语言代码)浏览:350 |
C语言程序设计教程(第三版)课后习题5.7 (C语言代码)浏览:695 |
C语言程序设计教程(第三版)课后习题8.7 (C语言代码)浏览:526 |
用筛法求之N内的素数。 (C语言代码) 详解………………浏览:1024 |
ACM俱乐部密码浏览:935 |