题目分析
题目要求我们求出信使送信的最长时间。由于信使数量充足,所以从哨所1送信需要要的最长时间就是哨所1到其他哨所的最短路的最大值。
> 既然最长的一条最短路都已经送到了,那些更短的最短路肯定已经送到了。
如果最长的最短路长度为$INF$,则说明不连通,输出-1即可。
样例
输入样例
4 4
1 2 4
2 3 7
2 4 1
3 4 6
输出样例
11
(spfa最短路)
因为数据范围很小,其实怎么做都可以。
C++ 代码
#include <queue>
#include <cstring>
#include <iostream>
using namespace std;
const int N = 100010;
int h[N], w[N], e[N], ne[N], idx;
int dist[N], n, m;
bool st[N];
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
void spfa()
{
memset(dist, 0x3f, sizeof dist);
dist[1] = 0;
queue<int> q;
q.push(1);
st[1] = true;
while(q.size())
{
int t = q.front();
q.pop();
st[t] = false;
for(int i = h[t]; i != -1; i = ne[i])
{
int j = e[i];
if(dist[j] > dist[t] + w[i])
{
dist[j] = dist[t] + w[i];
if(st[j] == false)
{
st[j] = true;
q.push(j);
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
memset(h, -1, sizeof h);
cin >> n >> m;
while(m --)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c), add(b, a, c);
}
spfa();
int maxn = 0;
for(int i = 1; i <= n; i ++) maxn = max(dist[i], maxn);
if(maxn == 0x3f3f3f3f) cout << -1;
else cout << maxn;
return 0;
}