题目链接
思路:二分枚举答案 + \(dijkstra\) 验证答案
二分枚举答案 \(mid\),通过 \(dijkstra\) 求最短路,将需要升级的边的权值看作 \(1\),不需要升级的边的权值看作 \(0\),这样求得的最小值就是需要升级的次数
这个将边权值根据需要设置为 \(0/1\) 的技巧需要注意
#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int N = 1010, M = 20010;
int n, m, k;
int h[N], e[M], w[M], ne[M], idx;
int dist[N];
bool st[N];
typedef pair<int, int> PII;
void add(int a, int b, int c)
{
e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++ ;
}
bool dijkstra(int mid)
{
memset(dist, 0x3f, sizeof dist);
memset(st, false, sizeof st);
dist[1] = 0;
priority_queue<PII, vector<PII>, greater<PII>> q;
q.push({0, 1});
while(q.size())
{
auto [distance, ver] = q.top(); q.pop();
if(st[ver]) continue;
st[ver] = true;
for(int i = h[ver]; i != -1; i = ne[i])
{
int j = e[i];
int flag = w[i] > mid; // 修改边权,若s[i]<=mid,边权为0,反之为1
if(dist[j] > distance + flag)
{
dist[j] = distance + flag;
q.push({dist[j], j});
}
}
}
return dist[n] <= k;
}
int main()
{
memset(h, -1, sizeof h);
cin >> n >> m >> k;
int l = 0, r = 0;
while (m -- )
{
int a, b, c; cin >> a >> b >> c;
add(a, b, c); add(b, a, c);
r = max(r, c);
}
int maxn = ++ r; // r+1 为不合法的,表示n不可达
while(l < r)
{
int mid = l + r >> 1;
if(dijkstra(mid)) r = mid;
else l = mid + 1;
}
cout << (l == maxn ? -1 : l) << endl;
return 0;
}
标签:合法性,dist,idx,int,视为,mid,权值,include
From: https://www.cnblogs.com/ALaterStart/p/18061851