很笨地用DFS遞歸枚舉。。
然後果斷超時。。
一個數據都沒過。。
然後第二天早上起來果斷用BFS寫了一下。。
wa~~
有點小激動,因爲沒有超時。
然後將隊列改爲優先隊列果斷A了
yes!!
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
struct node
{
int ne;
int va;
};
struct node1
{
int next;
int m;
bool operator < (const node1& a) const
{
return m>a.m;
}
};
vector<node> map[10002];
int visit[10002];
int n,t,in;
int mmin(int aa,int bb)
{
return aa<bb?aa:bb;
}
int max(int aa,int bb)
{
return aa>bb?aa:bb;
}
int BFS()
{
int xiao = 1000008;
priority_queue<node1> qu;
struct node1 needru,needpush,var;
needru.next = 1;
needru.m = 0;
qu.push(needru);
visit[1] = 1;
while(!qu.empty())
{
needpush = qu.top();
qu.pop();
if(needpush.next==n)
xiao = mmin(xiao,needpush.m);
int len = map[needpush.next].size();
visit[needpush.next] = 1;
for(int i=0;i<len;i++)
{
if(!visit[map[needpush.next][i].ne])
{
var.next = map[needpush.next][i].ne;
var.m = max(map[needpush.next][i].va,needpush.m);
qu.push(var);
}
}
}
return xiao;
}
int main()
{
int i,j,a,b;
struct node qq;
while(scanf("%d%d",&n,&t)!=EOF)
{
in = 1000008;
memset(visit,0,sizeof(visit));
for(i=0;i<t;i++)
{
scanf("%d%d%d",&a,&b,&j);
qq.ne = b;
qq.va = j;
map[a].push_back(qq);
qq.ne = a;
map[b].push_back(qq);
}
visit[1] = 1;
in = BFS();
if(in!=1000008)
printf("%d\n",in);
else
printf("-1\n");
for(i=0;i<=n;i++)
map[i].clear();
}
return 0;
}