2026.7.13思沁集训题解
2026.7.13思沁集训题解
A - Honeymoon Hike
题意
给定一个边长为
思路
- 因为海拔差很小,最多为
200 ,所以我们可以用二分海拔差+枚举下线上线+BFS过程
- 二分海拔差值
- 枚举所有可能的
low (下线)并计算high (上线)的大小low + 海拔差 ,若起点和终点在区间内,则用BFS 搜索(搜索过程中要加上区间判断)。若存在从起点到终点的合法路径,则X 可行 - 一直循环以上步骤直到求出结果即可
伪代码
#include<bits/stdc++.h> #define int long long using namespace std; int t; int n; const int N = 105; int a[N][N]; bool v[N][N]; struct P{ int x, y; }; int maxs = INT_MAX; int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; bool bfs(int limt){ 枚举low{ 计算high 判断起点终点是否合法(在区间内) queue <P> q; memset(v, 0, sizeof v); q.push(P{1, 1}); v[1][1] = true; while(q.size()){ 常规BFS,只不过要判断是否在区间内 } } return false; } signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> t; int cnt = 0; while(t--){ cin >> n; for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ cin >> a[i][j]; } } 二分枚举海拔差值 ++cnt; cout << "Scenario #"<< cnt<< ":"<< '\n'; cout << ans<< '\n'<< '\n'; } return 0; }AC代码
#include<bits/stdc++.h> #define int long long using namespace std; int t; int n; const int N = 105; int a[N][N]; bool v[N][N]; struct P{ int x, y, s, maxn, minn; }; int maxs = INT_MAX; int d[4][2] = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}}; bool bfs(int limt){ for(int low = 0;low + limt <= 210;low++){ int high = low + limt; if(a[1][1] < low || a[1][1] > high){ continue; } if(a[n][n] < low || a[n][n] > high){ continue; } queue <P> q; memset(v, 0, sizeof v); q.push(P{1, 1}); v[1][1] = true; while(q.size()){ P now = q.front(); q.pop(); if(now.x == n && now.y == n){ return true; } for(int i = 0;i < 4;i++){ int nx = now.x + d[i][0]; int ny = now.y + d[i][1]; if(nx < 1 || ny < 1 || nx > n || ny > n){ continue; } if(v[nx][ny]){ continue; } if(a[nx][ny] < low || a[nx][ny] > high){ continue; } v[nx][ny] = true; q.push(P{nx, ny}); } } } return false; } signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> t; int cnt = 0; while(t--){ cin >> n; for(int i = 1;i <= n;i++){ for(int j = 1;j <= n;j++){ cin >> a[i][j]; } } int l = 0, r = 210, ans = 210; while(l <= r){ int mid = (l + r) / 2; if(bfs(mid)){ ans = mid; r = mid - 1; }else{ l = mid + 1; } } ++cnt; cout << "Scenario #"<< cnt<< ":"<< '\n'; cout << ans<< '\n'<< '\n'; } return 0; }B - Trucking
题意
给定
n 个城市和m 条双向道路,每条道路有一个最大高度限制h_i 和道路长度len_i (若h_i = -1 则表示无高度限制)。一辆卡车自身高度限制为limt 。求从起点s 到终点t ,让货车能运送的货物最高,接着在保证最大高度下求出最短路径长度,如果不能到达n 就输出cannot reach destination 思路
这是一道二分答案 + SPFA判定的经典问题
过程
- 二分最大的可行高度
- 使用
SPFA ,只走高度限制大于等于mid 的边,如果能到达终点,则说明高度mid 可行 - 一直循环以上步骤,直到求出
max_high ,在此高度下的最短路就是答案伪代码
#include<bits/stdc++.h> #define int long long using namespace std; const int INF = 0x3f3f3f3f; const int N = 1010; struct P{ int v, h, w; }; vector <P> edge[N]; int dist[N]; bool vis[N]; int n, m; void spfa(int s, int limt) { 正常SPFA算法 } signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int count = 0; while(){ for(int i = 1;i <= n;i++){ 每次要清空图 } for(int i = 1;i <= m;i++){ 读入图 } int s, t, limt; cin >> s>> t>> limt; int l = 0, r = limt, ans = INF, max_high = -1; 二分枚举最大高度 if(count){ cout << '\n'; } ++count; cout << "Case "<< count<< ":\n"; if(ans != INF){ cout << "maximum height = "<< max_high<< "\n"; cout << "length of shortest route = "<< ans<< "\n"; }else{ cout << "cannot reach destination\n"; } } return 0; }AC代码
#include<bits/stdc++.h> #define int long long using namespace std; const int INF = 0x3f3f3f3f; const int N = 1010; struct P{ int v, h, w; }; vector <P> edge[N]; int dist[N]; bool vis[N]; int n, m; void spfa(int s, int limt) { for(int i = 1;i <= n;i++){ dist[i] = INF; vis[i] = false; } dist[s] = 0; vis[s] = true; queue<int> q; q.push(s); while(!q.empty()){ int u = q.front(); q.pop(); vis[u] = false; for(auto e : edge[u]){ if(e.h < limt){ continue; } int v = e.v; if(dist[v] > dist[u] + e.w){ dist[v] = dist[u] + e.w; if(!vis[v]){ vis[v] = true; q.push(v); } } } } } signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int count = 0; while(cin >> n>> m && n != 0 && m != 0){ for(int i = 1;i <= n;i++){ edge[i].clear(); } for(int i = 1;i <= m;i++){ int u, v, h, w; cin >> u>> v>> h>> w; if(h == -1){ h = INF; } edge[u].push_back({v, h, w}); edge[v].push_back({u, h, w}); } int s, t, limt; cin >> s>> t>> limt; int l = 0, r = limt, ans = INF, max_high = -1; while(l <= r){ int mid = (l + r) / 2; spfa(s, mid); if(dist[t] != INF){ l = mid + 1; ans = dist[t]; max_high = mid; }else{ r = mid - 1; } } if(count){ cout << '\n'; } ++count; cout << "Case "<< count<< ":\n"; if(ans != INF){ cout << "maximum height = "<< max_high<< "\n"; cout << "length of shortest route = "<< ans<< "\n"; }else{ cout << "cannot reach destination\n"; } } return 0; }C - Delay Constrained Maximum Capacity Path
题意
给定一个
n 个点、m 条边的无向图,点1 为矿山,点n 为加工厂。每条边有最大容量限制c 和通行时间d 。路径容量为路径上所有边容量的最小值。要求在总通行时间不超过t 的前提下,找一条从1 到n 的路径使容量最大,并输出该最大容量。思路
这是一道二分答案 + SPFA判定的经典问题
过程
- 二分最大的可行容量
- 使用
SPFA ,只走容量限制大于等于mid 的边,如果能到达终点,则说明容量mid 可行 - 一直循环以上步骤,直到求出最大的容量,在此容量下的最短路就是答案
伪代码
#include<bits/stdc++.h> #define int long long using namespace std; const int INF = 0x3f3f3f3f; const int N = 10005; struct P{ int v, cap, time; }; vector <P> edge[N]; int dist[N]; bool vis[N]; int limt; void spfa(int s){ 正常SPFA算法 } signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int t; cin >> t; while(t--){ int n, m, T; cin >> n>> m>> T; for(int i = 1;i <= n;i++){ 每次要清空图 } vector <int> a(m + 10); for(int i = 1;i <= m;i++){ 读入图、容量 } 给容量从大到小排序 int l = 0, r = m - 1, ans = a[0]; 二分枚举容量 cout << ans<< '\n'; } return 0; }AC代码
#include<bits/stdc++.h> #define int long long using namespace std; const int INF = 0x3f3f3f3f; const int N = 10005; struct P{ int v, cap, time; }; vector <P> edge[N]; int dist[N]; bool vis[N]; int limt; void spfa(int s){ memset(dist, 0x3f, sizeof(dist)); memset(vis, 0, sizeof(vis)); queue <int> q; dist[s] = 0; q.push(s); vis[s] = true; while(q.size()){ int u = q.front(); q.pop(); vis[u] = false; for(auto e : edge[u]){ if(e.cap < limt){ continue; } int v = e.v; if(dist[v] > dist[u] + e.time){ dist[v] = dist[u] + e.time; if(!vis[v]){ vis[v] = true; q.push(v); } } } } } signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); int t; cin >> t; while(t--){ int n, m, T; cin >> n>> m>> T; for(int i = 1;i <= n;i++){ edge[i].clear(); } vector <int> a(m + 10); for(int i = 1;i <= m;i++){ int u, v, c, d; cin >> u>> v>> c>> d; edge[u].push_back({v, c, d}); edge[v].push_back({u, c, d}); a[i] = c; } sort(a.begin(), a.end()); int l = 0, r = m - 1, ans = a[0]; while(l <= r){ int mid = (l + r) / 2; limt = a[mid]; spfa(1); if(dist[n] <= T){ ans = a[mid]; l = mid + 1; }else{ r = mid - 1; } } cout << ans<< '\n'; } return 0; }D - Telephone Lines S
题意
给定
n 个点、m 条边的无向图,每条边有一个长度l_i 。要找到一条从点1 到点n 的最短路径,可以指定k 条路径免费,其他要缴费,总费用为剩余边中的最大长度,求最小总费用思路
这道题其实还是二分答案 + SPFA
过程
- 二分一个费用
limt ,判断是否存在一条从1 到n 的路径,使得路径上长度大于limt 的边数不超过k 。把图转换一下:如果某条边的长度> limt ,则其边权视为1 ;如果长度\le limt ,则视为0 - 在这个转化后的图上,跑一次
SPFA 求从1 到n 的最短路径长度。如果最短长度\le k ,说明阈值limt 可行 - 最小的可行
limt 即为答案。若从1 无法到达n ,则直接输出-1 伪代码
#include<bits/stdc++.h> #define int long long using namespace std; const int INF = 0x3f3f3f3f3f3f3f3f; const int N = 1005; struct P{ int v, w; }; vector <P> edge[N]; int dist[N]; bool vis[N]; int n, m, k; bool spfa(int limt){ 普普通通的SPFA } signed main(){ ios::sync_with_stdio(false), cin.tie(0), cout.tie(0); cin >> n>> m>> k; int maxw = 0; for(int i = 1;i <= m;i++){ int u, v, w; cin >> u>> v>> w; edge[u].push_back({v, w}); edge[v].push_back({u, w}); maxw = max(maxw, w); } if(!spfa(maxw)){ cout << -1<< '\n'; return 0; } 二分费用 cout << ans<< '\n'; return 0; }
AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int INF = 0x3f3f3f3f3f3f3f3f;
const int N = 1005;
struct P{
int v, w;
};
vector <P> edge[N];
int dist[N];
bool vis[N];
int n, m, k;
bool spfa(int limt){
memset(dist, 0x3f, sizeof(dist));
memset(vis, 0, sizeof(vis));
queue <int> q;
dist[1] = 0;
q.push(1);
vis[1] = true;
while(q.size()){
int u = q.front();
q.pop();
vis[u] = false;
for(auto &e : edge[u]){
int v = e.v;
int w = (e.w > limt) ? 1 : 0;
if(dist[v] > dist[u] + w){
dist[v] = dist[u] + w;
if(!vis[v]){
vis[v] = true;
q.push(v);
}
}
}
}
return dist[n] <= k;
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> m>> k;
int maxw = 0;
for(int i = 1;i <= m;i++){
int u, v, w;
cin >> u>> v>> w;
edge[u].push_back({v, w});
edge[v].push_back({u, w});
maxw = max(maxw, w);
}
if(!spfa(maxw)){
cout << -1<< '\n';
return 0;
}
int l = 0, r = maxw, ans = -1;
while(l <= r){
int mid = (l + r) / 2;
if(spfa(mid)){
ans = mid;
r = mid - 1;
}else{
l = mid + 1;
}
}
cout << ans<< '\n';
return 0;
}