//#pragma GCC optimize(3,"Ofast","inline")
//#include<unordered_map>
//#include<unordered_set>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<functional>
#include<cstring>
#include<string>
#include<cstdlib>
#include<queue>
#include<map>
#include<algorithm>
#include<set>
#include<stack>
#include<vector>
#include<sstream>
#include<list>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
int mod=1000000007;
const int N=2e3+10;
const int maxn=1e7+10000;
int n,m;
int d[maxn],inq[maxn];
struct edge
{
int from,to,len,next;
} e[maxn];
int num;
int head[maxn];
void add(int from,int to,int len)
{
e[num]= {from,to,len,head[from]};
head[from]=num++;
}
int s,t;
int spfa()
{
queue<int>q;
d[s]=0;
q.push(s);
inq[s]=1;
while(!q.empty())
{
int now=q.front();
q.pop();
inq[now]=0;
for(int i=head[now]; i!=-1; i=e[i].next)
{
int v=e[i].to;
if(d[v]>d[now]+e[i].len)
{
d[v]=d[now]+e[i].len;
if(inq[v])continue;
inq[v]=1;
q.push(v);
}
}
}
if(d[t]==INF)return -1;
else return d[t];
}
int main()
{
int n,m,x,y,l;
while(cin>>n>>m)
{
memset(head,-1,sizeof head);
memset(d,INF,sizeof d);
memset(inq,0,sizeof inq);
num=0;
for(int i=1; i<=m; i++)
{
cin>>x>>y>>l;
add(x,y,l);
add(y,x,l);
}
cin>>s>>t;
cout<<spfa()<<endl;
}
return 0;
}
08-29
08-29
06-03