You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

48 lines
1.2 KiB
C++

#define _CRT_SECURE_NO_WARNINGS
#define _SILENCE_CXX17_C_HEADER_DEPRECATION_WARNING
#include <bits/stdc++.h>
using namespace std;
const int N = 1005, M = 10050;
int n, m;
int adj[N], nxt[M << 1], to[M << 1], len[M << 1], ecnt;
int dis[N];
inline void addEdge(int f, int t, int l)
{
ecnt++;
to[ecnt] = t;
len[ecnt] = l;
nxt[ecnt] = adj[f];
adj[f] = ecnt;
}
int inq[N], que[M];
void spfa(int s)
{
memset(inq, 0, sizeof(inq));
memset(dis, 0x3f3f3f3f, sizeof(dis));
int h, t, x;
h = t = x = 0;
for (dis[s] = 0, que[t++] = s, inq[s] = true; h ^ t; inq[que[h++]] = false)
for (int cur = adj[que[h]]; cur; cur = nxt[cur])
if (dis[to[cur]] > dis[que[h]] + len[cur])
{
dis[to[cur]] = dis[que[h]] + len[cur];
if (!inq[to[cur]]) que[t++] = to[cur], inq[to[cur]] = true;
}
}
int main()
{
while (scanf("%d%d", &n, &m), n | m)
{
memset(adj, 0, sizeof(adj)), ecnt = 0;
for (int i = 0, a, b, c; i < m; i++)
{
scanf("%d%d%d", &a, &b, &c);
addEdge(a, b, c);
addEdge(b, a, c);
}
spfa(1);
printf("%d\n", dis[n]);
}
return 0;
}