2016-10-12 10:21:16 +08:00
|
|
|
#include <iostream>
|
2016-10-11 22:15:41 +08:00
|
|
|
#include <cstdio>
|
|
|
|
#include <cstdlib>
|
2016-10-12 10:21:16 +08:00
|
|
|
#include <cmath>
|
2016-10-11 22:15:41 +08:00
|
|
|
#include <algorithm>
|
2016-10-12 10:21:16 +08:00
|
|
|
#include <climits>
|
|
|
|
#include <cstring>
|
|
|
|
#include <string>
|
|
|
|
#include <set>
|
|
|
|
#include <map>
|
|
|
|
#include <queue>
|
|
|
|
#include <stack>
|
|
|
|
#include <vector>
|
|
|
|
#include <list>
|
|
|
|
#define mod 1000000007
|
|
|
|
#define inf 0x3f3f3f3f
|
|
|
|
#define pi acos(-1.0)
|
2016-10-11 22:15:41 +08:00
|
|
|
using namespace std;
|
2016-10-12 10:21:16 +08:00
|
|
|
typedef long long ll;
|
|
|
|
#define RANGE 1005
|
|
|
|
#define MAX 0x3f3f3f3f
|
|
|
|
int cost[RANGE][RANGE];
|
|
|
|
int d[RANGE];
|
|
|
|
bool used[RANGE];
|
|
|
|
int n,m;
|
|
|
|
int pre[1005];
|
|
|
|
void spfa(int s)
|
2016-10-11 22:15:41 +08:00
|
|
|
{
|
2016-10-12 10:21:16 +08:00
|
|
|
int i,j,now;
|
2016-10-12 10:23:41 +08:00
|
|
|
for(i=0;i<=n+1;i++)
|
2016-10-12 10:21:16 +08:00
|
|
|
{
|
|
|
|
d[i]=MAX;
|
|
|
|
used[i]=false;
|
|
|
|
}
|
|
|
|
int flag=0;
|
2016-10-12 10:23:41 +08:00
|
|
|
for(i=0;i<=n+1;i++)
|
2016-10-12 10:21:16 +08:00
|
|
|
{
|
|
|
|
if(cost[0][i]<MAX)pre[i]=i;//初始化pre数组
|
|
|
|
}
|
|
|
|
used[s]=true;
|
|
|
|
d[s]=0;
|
|
|
|
queue<int> q;
|
|
|
|
q.push(s);
|
|
|
|
while(!q.empty())
|
2016-10-11 22:15:41 +08:00
|
|
|
{
|
2016-10-12 10:21:16 +08:00
|
|
|
now=q.front();
|
|
|
|
q.pop();
|
2016-10-12 10:23:41 +08:00
|
|
|
for(i=0;i<=n+1;i++)
|
2016-10-11 22:15:41 +08:00
|
|
|
{
|
2016-10-12 10:21:16 +08:00
|
|
|
if(d[i]>d[now]+cost[now][i])
|
2016-10-11 22:15:41 +08:00
|
|
|
{
|
2016-10-12 10:21:16 +08:00
|
|
|
d[i]=d[now]+cost[now][i];
|
|
|
|
if(used[i]==0)
|
2016-10-11 22:15:41 +08:00
|
|
|
{
|
2016-10-12 10:21:16 +08:00
|
|
|
if(now!=0)pre[i]=pre[now];
|
|
|
|
used[i]==true;
|
|
|
|
q.push(i);
|
2016-10-11 22:15:41 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-12 10:21:16 +08:00
|
|
|
if(d[i]==d[now]+cost[now][i])
|
2016-10-11 22:15:41 +08:00
|
|
|
{
|
2016-10-12 10:23:41 +08:00
|
|
|
if(now!=0)pre[i]=min(pre[i],pre[now]);
|
2016-10-11 22:15:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
int main()
|
|
|
|
{
|
2016-10-12 10:21:16 +08:00
|
|
|
int i,j,a,b,c;
|
|
|
|
int we;
|
|
|
|
scanf("%d",&we);
|
|
|
|
while(we--)
|
2016-10-12 10:23:41 +08:00
|
|
|
{scanf("%d%d",&n,&m);
|
2016-10-11 22:15:41 +08:00
|
|
|
if(!n&&!m) break;
|
2016-10-12 10:23:41 +08:00
|
|
|
for(i=0;i<=n+1;++i)
|
2016-10-11 22:15:41 +08:00
|
|
|
{
|
2016-10-12 10:23:41 +08:00
|
|
|
for(j=0;j<=i;j++)
|
2016-10-11 22:15:41 +08:00
|
|
|
{
|
2016-10-12 10:21:16 +08:00
|
|
|
if(i==j) cost[i][j]=0;
|
|
|
|
else cost[i][j]=cost[j][i]=MAX;
|
2016-10-11 22:15:41 +08:00
|
|
|
}
|
|
|
|
}
|
2016-10-12 10:23:41 +08:00
|
|
|
for(i=0;i<m;i++)
|
2016-10-11 22:15:41 +08:00
|
|
|
{
|
2016-10-12 10:21:16 +08:00
|
|
|
scanf("%d%d%d",&a,&b,&c);
|
|
|
|
cost[a][b]=c;
|
2016-10-11 22:15:41 +08:00
|
|
|
}
|
|
|
|
spfa(0);
|
2016-10-12 10:21:16 +08:00
|
|
|
if(d[n+1]<MAX)
|
2016-10-11 22:15:41 +08:00
|
|
|
{
|
2016-10-12 10:21:16 +08:00
|
|
|
if(pre[n+1]==n+1)printf("0\n");
|
|
|
|
else printf("%d\n",pre[n+1]);
|
2016-10-11 22:15:41 +08:00
|
|
|
}
|
|
|
|
else printf("-1\n");
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|