输入的第一行包含三个整数 n, m, q ,分别表示设备数、物理连接数和询问数。接下来 m 行,每行包含三个整数 ui, vi,wi ,分别表示 ui 和 vi 之间有一条稳定性为 wi 的物理连接。
接下来 q 行,每行包含两个整数 xi, yi ,表示查询 xi 和 yi 之间的通信稳定性。
输出 q 行,每行包含一个整数依次表示每个询问的答案。
5 4 3
1 2 5
2 3 6
3 4 1
1 4 3
1 5
2 4
1 3
-1
3
5
对于 30% 的评测用例,n, q ≤ 500,m ≤ 1000 ;
对于 60% 的评测用例,n, q ≤ 5000,m ≤ 10000 ;
对于所有评测用例,2 ≤ n, q ≤ 10^5,1 ≤ m ≤ 3 × 10^5,1 ≤ ui, vi, xi, yi ≤ n,
1 ≤ wi ≤ 10^6,ui!= vi,xi!=yi 。
由于本题中所有边权值均为正数,因此可以使用 Dijkstra 算法进行优化。从起点开始,依次扫描与其相连的所有边,更新到各个顶点的最小距离信息。对于每一个询问,只需使用 Dijkstra 算法计算出从 xi 到 yi 的所有可行路径,然后取这些路径中的最小边权值,即为 xi 和 yi 之间通信的稳定性。当然,如果不存在路径,则输出 -1。
#include
#include
#include
using namespace std;
const int N = 10010, M = 2 * N;
typedef pair<int, int> PII;
int h[N], e[M], ne[M], w[M], idx;
int dist[N];
bool st[N];
int n, m, q;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx++;
}
void dijkstra(int start)
{
memset(dist, 0x3f, sizeof dist);
memset(st, false, sizeof st);
priority_queue<PII, vector<PII>, greater<PII>> heap;
heap.push({ 0, start });
dist[start] = 0;
while (heap.size())
{
auto t = heap.top();
heap.pop();
int ver = t.second, distance = t.first;
if (st[ver]) continue;
st[ver] = true;
for (int i = h[ver]; ~i; i = ne[i])
{
int j = e[i];
if (dist[j] > max(dist[ver], w[i]))
{
dist[j] = max(dist[ver], w[i]);
heap.push({ dist[j], j });
}
}
}
}
int main()
{
scanf("%d%d%d", &n, &m, &q);
memset(h, -1, sizeof h);
while (m--)
{
int a, b, c;
scanf("%d%d%d", &a, &b, &c);
add(a, b, c), add(b, a, c);
}
while (q--)
{
int a, b;
scanf("%d%d", &a, &b);
dijkstra(a);
if (dist[b] == 0x3f3f3f3f) puts("-1");
else printf("%d\n", dist[b]);
}
return 0;
}
以个人刷题整理为目的,如若侵权,请联系删除~