营救
Luogu P1396 营救
题目背景
“咚咚咚……”“查水表!”原来是查水表来了,现在哪里找这么热心上门的查表员啊!小明感动得热泪盈眶,开起了门……
题目描述
妈妈下班回家,街坊邻居说小明被一群陌生人强行押上了警车!妈妈丰富的经验告诉她小明被带到了 \(t\) 区,而自己在 \(s\) 区。
该市有 \(m\) 条大道连接 \(n\) 个区,一条大道将两个区相连接,每个大道有一个拥挤度。小明的妈妈虽然很着急,但是不愿意拥挤的人潮冲乱了她优雅的步伐。所以请你帮她规划一条从 \(s\) 至 \(t\) 的路线,使得经过道路的拥挤度最大值最小。
输入格式
第一行有四个用空格隔开的 \(n\),\(m\),\(s\),\(t\),其含义见【题目描述】。
接下来 \(m\) 行,每行三个整数 \(u, v, w\),表示有一条大道连接区 \(u\) 和区 \(v\),且拥挤度为 \(w\)。
两个区之间可能存在多条大道。
输出格式
输出一行一个整数,代表最大的拥挤度。
输入输出样例 #1
输入 #1
3 3 1 3
1 2 2
2 3 1
1 3 3
输出 #1
2
说明/提示
数据规模与约定
- 对于 \(30\%\) 的数据,保证 \(n\leq 10\)。
- 对于 \(60\%\) 的数据,保证 \(n\leq 100\)。
- 对于 \(100\%\) 的数据,保证 \(1 \leq n\leq 10^4\),\(1 \leq m \leq 2 \times 10^4\),\(w \leq 10^4\),\(1 \leq s, t \leq n\)。且从 \(s\) 出发一定能到达 \(t\) 区。
样例输入输出 1 解释
小明的妈妈要从 \(1\) 号点去 \(3\) 号点,最优路线为 \(1 \to 2 \to 3\)。
Code
蒟蒻 kruskal 或 prim 练的少, dijkstra 相对用得更熟一点,就这样吧
这相当于是 dijkstra 裸题了,就只需要改一行
#include <bits/stdc++.h>
#define maxn 40005
#define inf 0x3f3f3f3f
using namespace std;
int n, m, s, t, cnt, ans, head[maxn], to[maxn], nxt[maxn], w[maxn], dist[maxn];
void add(int u, int v, int x)
{
to[++cnt] = v;
w[cnt] = x;
nxt[cnt] = head[u];
head[u] = cnt;
}
void dijkstra()
{
fill(dist, dist + n + 1, inf);
priority_queue<pair<int, int>, vector<pair<int, int> >, greater<pair<int, int> > > pq;
dist[s] = 0;
pq.push(make_pair(0, s));
while (!pq.empty())
{
pair<int, int> tp = pq.top();
pq.pop();
int d = tp.first, u = tp.second;
if (d > dist[u])
{
continue;
}
for (int i = head[u]; i; i = nxt[i])
{
int v = to[i], nd = max(d, w[i]); //路径上的最大值,而不是求和
if (nd < dist[v])
{
dist[v] = nd;
pq.push(make_pair(nd, v));
}
}
}
}
int main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m >> s >> t;
for (int i = 0; i < m; i++)
{
int a, b, c;
cin >> a >> b >> c;
add(a, b, c);
add(b, a, c);
}
dijkstra();
cout << dist[t];
return 0;
}

浙公网安备 33010602011771号