大致思路:
最短路变形,建立dis数组。不过dis[i]保存的不是到达i点的最短路径,而是能到达i点的所有路径中每条路径中最长的天数的最小值。(有点绕脑,就像样例说明一样,1到6有两条路径,其中一条路径要花6天完成(完成这条路的时间肯定是修这条路径的所有隧道花费时间最长的),另一条要7天,那么答案就是6天。)
所以当用spfa进行点松弛时,有:dis[to]=max(dis[top],cost)
代码:
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<map>
#include<vector>
#include<queue>
#define inf 99999999
using namespace std;
int dis[200005];
int vis[200005];
struct Lu
{
int to;
int cost;
Lu(int too,int costt)
{
to=too;
cost=costt;
}
};
vector<Lu>V[200005];
int spfa(int n)
{
for(int i=0; i<=200005; i++)
{
vis[i]=0;
dis[i]=inf;
}
dis[1]=0;
queue<int>Q;
Q.push(1);
vis[1]=1;
while(!Q.empty())
{
int top=Q.front();
Q.pop();
vis[top]=0;
for(int i=0; i<V[top].size(); i++)
{
int to=V[top][i].to;
int cost=V[top][i].cost;
if(dis[to]>max(dis[top],cost))
{
dis[to]=max(dis[top],cost);
if(!vis[to])
{
Q.push(to);
vis[to]=1;
}
}
}
}
return dis[n];
}
int main()
{
int n,m;
while(~scanf("%d%d",&n,&m))
{
for(int i=0; i<=200005; i++)
{
V[i].clear();
}
int a,b,c;
for(int i=0; i<m; i++)
{
scanf("%d%d%d",&a,&b,&c);
V[a].push_back(Lu(b,c));
V[b].push_back(Lu(a,c));
}
printf("%d\n",spfa(n));
}
}