///POJ 2387为例
#include
#include
#include
#include
#include <string.h>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn = 1e3 + 10;
struct EdgeNode{ int v, w, nxt; };
EdgeNode Edge[maxn*maxn];
bool vis[maxn];
int Head[maxn], Dis[maxn], cnt;
int N, M;
/// int PushCnt[maxn]; ///记录每一个节点的入队次数、方便判断负环
inline void init()
{
for(int i=0; i<=N; i++)
///PushCnt[i] = 0;
Head[i] = -1,
Dis[i] = INF,
vis[i] = false;
cnt = 0;
}
inline void AddEdge(int from, int to, int weight)
{
Edge[cnt].w = weight;
Edge[cnt].v = to;
Edge[cnt].nxt = Head[from];
Head[from] = cnt++;
}
void SPFA(int st)///若要判断负环、改为 bool
{
deque<int> que;
que.push_back(st);
vis[st]=true;
Dis[st]=0;
while (!que.empty())
{
int T=que.front(); que.pop_front();
vis[T]=false;
for (int i=Head[T]; i!=-1; i=Edge[i].nxt)
{
int v=Edge[i].v;
int w=Edge[i].w;
if (Dis[v]>Dis[T]+w){
Dis[v]=Dis[T]+w;
///p[v] = T;
if (!vis[v]){
///if(++PushCnt[v] > N) return false; //有负环
vis[v]=true;
if(!que.empty() && Dis[v] < Dis[que.front()]) que.push_front(v);
else que.push_back(v);
//que.push_back(v); ///无SLF优化是这样写的
}
}
}
}
/// return true;
}
int main(void)
{
while(~scanf("%d %d", &M, &N)){
init();
int from, to, weight;
for(int i=0; i){
scanf("%d %d %d", &from, &to, &weight);
AddEdge(from, to, weight);
AddEdge(to, from, weight);
}
SPFA(1);
printf("%d\n", Dis[N]);
}
return 0;
}