最短路专项测试
P25ffy
·
·
个人记录
考试总结
【天冷了,做张试卷暖你一整天】
考试总分 500pts,预估总分 200pts,实际总分 212pts,排名 10/19
1. 考场复盘
这次考试没有安排好时间,T3 花了太久时间,导致后两题没有时间做了。
惨痛的教训: 拿到题后一定要先通读全卷 !!!
2. 得分复盘
$T2$ 预估 $100pts$,实际 $100pts$。
$T3$ 预估 $0pts$,实际 $30pts$。
$T4$ 预估 $0pts$,实际 $0pts$。
$T5$ 预估 $0pts$,实际 $0pts$。
### 3. 逐题复盘
|题目|正解|我的思路|测试点|主要问题|
|:-:|:--|:--|:-:|:--|
|$T1$|$3$ 次 $dij$|<|**AC:82;WA:18**|提前结束了 $dij$ 函数(差一步美满)|
|$T2$|预处理可行节点后 $dij$|<|**AC:100**|无|
|$T3$|倍增+$floyd$|特判后`cout<<1`|**AC:30;WA:70**|~~直接`cout<<1`有50分~~|
|$T4$|$floyd$|$dij$|**RE:0**|1.没开long long;2.没想到 $floyd$,$dij$ 会爆|
|$T5$|分层图|暴搜~~还打错了~~|**0**|没时间……|
::::info[T1]
:::info[题目大意]
【做出这道题像呼吸一样简单】
~~但是我不会呼吸~~
Bessie 和 Elsie 在不同的区域放牧,他们希望花费最小的能量返回谷仓。从一个区域走到一个相连区域,Bessie 要花费 $B$ 单位的能量,Elsie要花费 $E$ 单位的能量。
如果某次他们俩走到同一个区域,Bessie 可以背(bēi?bèi?)着 Elsie 走路,花费 $P$ 单位的能量走到另外一个相连的区域。当然,存在 $P>B+E$ 的情况。
相遇后,他们可以一直背着走,也可以独立分开。
Bessie 从 $1$ 号区域出发,Elsie 从 $2$ 号区域出发,两个人都要返回到位于 $n$ 号区域的谷仓。
:::
:::warning[题目思路]
$3$ 次 $dij$,分别从 $1$,$2$,$n$ 开始,计算这 $3$ 个节点到其他点的距离,然后枚举汇合点。
**注意!** 函数不能搜到 $n$ 就结束!~~要不然82pts~~。
:::
:::success[代码留存]
```
//82ptscode
#include<bits/stdc++.h>
using namespace std;
inline int read();
int n, m;
int b, e, p;
vector<int> v[40005];
int dis[40005], dis2[40005], disn[40005];
bool vis[40005], vis2[40005], visn[40005];
struct node{int x, num;};
bool operator < (const node &x, const node &y){return x.num > y.num;}
void dj(){
priority_queue<node> q;
q.push({1, 0});
memset(dis, 0x3f, sizeof dis);
dis[1] = 0;
while(!q.empty()){
node tp = q.top(); q.pop();
int x = tp.x;
if(x == n) return;
if(vis[x]) continue;
vis[x] = 1;
for(int y : v[x]){
if(dis[y] > dis[x] + 1){
dis[y] = dis[x] + 1;
q.push({y, dis[y]});
}
}
}
}
//剩下两个dij差不多的写法。
int main(){
ios::sync_with_stdio(false); cout.tie(0);
freopen("piggy.in", "r", stdin);
freopen("piggy.out", "w", stdout);
b = read(), e = read(), p = read(), n = read(), m = read();
for(int i = 1; i <= m; i ++){
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
dj();
dj2();
if(p < b + e){
djn();
int mn = INT_MAX;
for(int i = 1; i <= n; i ++){
if(dis[i] != 0x3f3f3f3f && dis2[i] != 0x3f3f3f3f){
mn = min(mn, dis[i] * b + dis2[i] * e + disn[i] * p);
}
}
cout << mn;
}
else cout << dis[n] * b + dis2[n] * e;
return 0;
}
```
```
//ACcode
#include<bits/stdc++.h>
using namespace std;
inline int read();
int n, m;
int b, e, p;
vector<int> v[40005];
int dis[40005], dis2[40005], disn[40005];
bool vis[40005], vis2[40005], visn[40005];
struct node{int x, num;};
bool operator < (const node &x, const node &y){return x.num > y.num;}
void dj(){
priority_queue<node> q;
q.push({1, 0});
memset(dis, 0x3f, sizeof dis);
dis[1] = 0;
while(!q.empty()){
node tp = q.top(); q.pop();
int x = tp.x;
//if(x == n) return;
if(vis[x]) continue;
vis[x] = 1;
for(int y : v[x]){
if(dis[y] > dis[x] + 1){
dis[y] = dis[x] + 1;
q.push({y, dis[y]});
}
}
}
}
//剩下两个dij差不多
int main(){
ios::sync_with_stdio(false); cout.tie(0);
freopen("piggy.in", "r", stdin);
freopen("piggy.out", "w", stdout);
b = read(), e = read(), p = read(), n = read(), m = read();
for(int i = 1; i <= m; i ++){
int x = read(), y = read();
v[x].push_back(y); v[y].push_back(x);
}
dj();
dj2();
djn();
int mn = INT_MAX;
for(int i = 1; i <= n; i ++){
if(dis[i] != 0x3f3f3f3f && dis2[i] != 0x3f3f3f3f && disn[i] != 0x3f3f3f3f){
mn = min(mn, dis[i] * b + dis2[i] * e + disn[i] * p);
}
}
cout << mn;
return 0;
}
```
:::
::::
::::info[T2]
:::info[题目大意]
~~【太简单了,随便写写就ac了】~~
在一个有向图中,每条边的长度均为 $1$,现给定起点和终点,请你在图中找一条从起点到终点的路径,该路径满足:
1. 路径上的所有点的出边所指向的点都直接或间接与终点连通。
2. 在满足条件 $1$ 的情况下使路径最短。
请你输出符合条件的路径的长度。
:::
:::warning[题目思路]
反向建边预处理所有连通的点,然后 $dij$。
:::
:::success[代码留存]
```
//ACcode
#include<bits/stdc++.h>
using namespace std;
inline int read();
int n, m, st, ed;
vector<int> v[10005], vn[10005];
struct node{int x, num;};
bool operator < (const node &x, const node &y){return x.num > y.num;}
int dis[10005], disn[10005];
bool vis[10005], visn[10005], ca[10005];
void djn(){
//预处理
}
void dj(){
//求最短路
}
int main(){
ios::sync_with_stdio(false); cout.tie(0);
freopen("road.in", "r", stdin);
freopen("road.out", "w", stdout);
n = read(), m = read();
for(int i = 1; i <= m; i ++){
int x = read(), y = read();
v[x].push_back(y); vn[y].push_back(x);
}
st = read(), ed = read();
djn();
if(!ca[st]){cout << -1; return 0;}
for(int y : v[st]){
if(!ca[y]){cout << -1;return 0;}
}
dj();
if(dis[ed] == 0x3f3f3f3f){cout << -1; return 0;}
cout << dis[ed];
return 0;
}
```
:::
::::
::::info[T3]
:::info[题目大意]
小 A 的工作不仅繁琐,更有苛刻的规定,要求小 A 每天早上在 $6:00$ 之前到达公司,否则这个月工资清零。可是小 A 偏偏又有赖床的坏毛病。于是为了保住自己的工资,小 A 买了一个空间跑路器,每秒钟可以跑 $2^k$ 千米($k$ 是任意自然数)。当然,这个机器是用 `longlong` 存的,所以总跑路长度不能超过 $2^{31} - 1$ 千米。小 A 的家到公司的路可以看做一个有向图,小 A 家为点 $1$,公司为点 $n$,每条边长度均为一千米。
小 A 想每天能醒地尽量晚,所以让你帮他算算,他最少需要几秒才能到公司。
:::
:::warning[题目思路]
倍增预处理可以直接到达的节点,然后 $floyd$。
:::
:::success[代码留存]
```
//ACcode
#include<bits/stdc++.h>
using namespace std;
inline int read();
int n, m;
bool bz[55][55][55];
int dis[55][55];
void floyd(){
for(int k = 1; k <= n; k ++){
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
if(i == j || j == k || i == k) continue;
dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]);
}
}
}
}
int main(){
freopen("paolu.in", "r", stdin);
freopen("paolu.out", "w", stdout);
memset(dis, 0x3f, sizeof dis);
n = read(), m = read();
for(int i = 1; i <= m; i ++){
int x = read(), y = read();
dis[x][y] = 1, bz[x][y][0] = 1;
}
for(int l = 0; l <= 40; l ++){
for(int k = 1; k <= n; k ++){
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
if(bz[i][k][l] && bz[k][j][l]){
dis[i][j] = 1;
bz[i][j][l + 1] = 1;
}
}
}
}
}
floyd();
cout << dis[1][n];
return 0;
}
```
:::
::::
::::info[T4]
:::info[题目大意]
B 市共有 $m$ 个地铁站,编号为 $1$ 到 $m$。每个地铁站都有一个站厅,地铁口与站厅之间有一条通道,往返耗时相同。从第 $v$($1 \le v \le m$)个地铁站的地铁口走到站厅需要 $w_v$ 的时间。
B 市共有 $n$ 条地铁线路,编号为 $1$ 到 $n$,其中第 $i$($1 \le i \le n$)条线路经过 $k_i$ 个地铁站,编号依次为 $a_{i,1},a_{i,2},\ldots,a_{i,k_i}$。在第 $a_{i,j}$($1 \le j \le k_i$)个地铁站处,从站厅走到第 $i$ 条线路的站台需要 $t_{i,j}$ 的时间,往返耗时相同。同一条线路不会重复经过同一个地铁站。由于空间有限,每个地铁站最多有 $d$ 条线路经过。
小 R 有 $q$ 个出行计划。对于第 $r$($1 \le r \le q$)个出行计划,小 R 将从第 $x_r$ 个地铁站的地铁口出发,到达第 $y_r$ 个地铁站的地铁口,途中可能进行若干次站内换乘。每次站内换乘都需要从一条线路的站台先走到站厅,再从站厅走到另一条线路的站台。每个出行计划的总步行时间为进出站和换乘的步行时间之和。求每个出行计划的最小总步行时间。
:::
:::warning[题目思路]
**建图:**
节点为每个地铁线路,边为每个地铁站,边权为当前地铁站站厅到两个线路站台的时间和。
$floyd$ 求多源最短路,查询时枚举开始和结束时搭乘的线路,取 $min$。
**注意:** 开`long long`……
:::
:::success[代码留存]
```cpp
//RE0pts code
#include<bits/stdc++.h>
using namespace std;
inline int read();
int c, n, m, d, q;
int st, ed;
int tc[500005];
int g[500005][505];
bool qc[505][505];
int head[505], ne[500005], ver[500005], w[500005], tot;
void add(int x, int y, int z){
ver[++tot] = y, w[tot] = z;
ne[tot] = head[x]; head[x] = tot;
}
struct node{int x, num;};
bool operator < (const node &x, const node &y){return x.num > y.num;}
int dis[505];
bool vis[505];
int dj(){
priority_queue<node> q;
memset(dis, 0x3f, sizeof dis);
memset(vis, 0, sizeof vis);
for(int i = 1; i <= n; i ++){
if(g[st][i]){
q.push({i, 0});
dis[i] = 0;
}
}
while(!q.empty()){
node tp = q.top(); q.pop();
int x = tp.x;
if(g[ed][x]) return tp.num;
if(vis[x]) continue;
vis[x] = 1;
for(int i = head[x]; i; i = ne[i]){
int y = ver[i];
if(dis[y] > dis[x] + w[i]){
dis[y] = dis[x] + w[i];
q.push({y, dis[y]});
}
}
}
}
int main(){
ios::sync_with_stdio(false); cout.tie(0);
freopen("subway.in", "r", stdin);
freopen("subway.out", "w", stdout);
n = read(), m = read(), d = read(), q = read();
for(int i = 1; i <= m; i ++) tc[i] = read();
for(int i = 1; i <= n; i ++){
int tot = read();
for(int j = 1; j <= tot; j ++){
int x = read(), t = read();
g[x][i] = t;
}
}
for(int i = 1; i <= m; i ++){
for(int j = 1; j < n; j ++){
if(!g[i][j]) continue;
for(int k = j + 1; k <= n; k ++){
if(g[i][k]) add(j, k, g[i][j] + g[i][k]);
}
}
}
while(q --){
st = read(), ed = read();
cout << dj() + tc[st] + tc[ed] << '\n';
}
return 0;
}
```
```
//ACcode
#include<bits/stdc++.h>
using namespace std;
inline int read();
int c, n, m, d, q;
int st, ed;
int tc[500002];
int g[500002][502];
long long a[505][505];
void floyd(){
for(int k = 1; k <= n; k ++){
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
if(i == j || j == k || i == k) continue;
a[i][j] = min(a[i][j], a[i][k] + a[k][j]);
}
}
}
}
int main(){
ios::sync_with_stdio(false);cout.tie(0);
freopen("subway.in","r",stdin);
freopen("subway.out","w",stdout);
n = read(),m = read(),d = read(),q = read();
for(int i = 1; i <= m; i ++) tc[i] = read();
for(int i = 1; i <= n; i ++){
int tot = read();
for(int j = 1; j <= tot; j ++){
int x = read(), t = read();
g[x][i] = t;
}
}
memset(a, 0x3f, sizeof a);
for(int i = 1; i <= m; i ++){
for(int j = 1; j < n; j ++){
if(!g[i][j]) continue;
for(int k = j + 1; k <= n; k ++){
if(g[i][k]){
a[j][k] = g[i][j] + g[i][k];
a[k][j] = g[i][j] + g[i][k];
}
}
}
}
floyd();
while(q --){
st = read(), ed = read();
long long ans = 1e18;
for(int i = 1; i <= n; i ++){
if(!g[st][i]) continue;
for(int j = 1; j <= n; j ++){
if(g[ed][j]){
if(i == j) ans = min(ans, 1LL * (g[st][i] + g[ed][j]));
else ans = min(ans, a[i][j] + g[st][i] + g[ed][j]);
}
}
}
cout << ans + tc[st] + tc[ed] << '\n';
}
return 0;
}
```
:::
::::
::::info[T5]
:::info[题目大意]
FJ 的农场被安排成一个 $N \times N$ 的方形网格田地($3 \leq N \leq 100$),其中有 $N-1$ 条南北向的道路和 $N-1$ 条东西向的道路穿过农场内部,作为田地之间的分隔。奶牛 Bessie 可以自由地从任何田地移动到相邻的田地(北、东、南或西),她穿过一条道路需要花费的时间为 $T$($0 \leq T \leq 1,000,000$)。
有一天,FJ 邀请 Bessie 去他家进行一场友好的国际象棋比赛。Bessie 从西北角的田地出发,而 FJ 的家在东南角的田地,因此 Bessie 需要走很长一段路。由于她在路上会感到饥饿,她会在每经过第三个田地时停下来吃草(不包括她的起始田地,但可能包括最终到达的 FJ 家的田地)。有些田地的草比其他田地更茂盛,因此停下来吃草所需的时间取决于她停下的田地。
请帮助 Bessie 确定她到达 FJ 家所需的最少时间。
:::
:::warning[题目思路]
将每个点和走三步能到的点连边,$dij$,到达终点一定范围后即可结束。
:::
:::success[代码留存]
```
//0pts code
#include<bits/stdc++.h>
using namespace std;
const int dx[4] = {0, 0, 1, -1};
const int dy[4] = {1, -1, 0, 0};
inline int read();
int n, t;
int g[105][105];
struct node{int x, y, stp, num;};
bool operator < (const node &x, const node &y){return x.num > y.num;}
int dis[105][105];
bool vis[105][105];
int dj(){
priority_queue<node> q;
q.push({1, 1, 0, 0});
memset(dis, 0x3f, sizeof dis);
dis[1][1] = 0;
while(!q.empty()){
node tp = q.top(); q.pop();
int x = tp.x, y = tp.y;
if(x == n && y == n) return dis[x][y];
if(vis[x][y]) continue;
vis[x][y] = 1;
bool f = 0;
if(tp.stp % 3 == 0) f = 1;
for(int i = 0; i < 4; i ++){
int xx = x + dx[i], yy = y + dy[i];
if(xx < 1 || xx > n || yy < 1 || yy > n) continue;
if(dis[xx][yy] > dis[x][y] + t + g[xx][yy] * f){
dis[xx][yy] = dis[x][y] + t + g[xx][yy] * f;
q.push({xx, yy, tp.stp + 1, dis[xx][yy]});
}
}
}
}
int main(){
ios::sync_with_stdio(false); cout.tie(0);
freopen("cow.in", "r", stdin);
freopen("cow.out", "w", stdout);
n = read(), t = read();
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
g[i][j] = read();
}
}
cout << dj();
return 0;
}
```
```
#include<bits/stdc++.h>
using namespace std;
const int dx[16] = {0, 0, 1, -1, 0, 0, 3, -3, 1, 1, -1, -1, 2, 2, -2, -2};
const int dy[16] = {1, -1, 0, 0, 3, -3, 0, 0, 2, -2, 2, -2, 1, -1, 1, -1};
inline int read();
int n, t, ans = INT_MAX;
int g[105][105];
int head[10105], ne[600005], ver[600005], w[600005], tot;
void add(int x, int y, int z){
ver[++tot] = y, w[tot] = z;
ne[tot] = head[x], head[x] = tot;
}
struct node{int x, num;};
bool operator < (const node &x, const node &y){return x.num > y.num;}
int dis[10105];
bool vis[10105];
void dj(){
priority_queue<node> q;
q.push({n + 1, 0});
memset(dis, 0x3f, sizeof dis);
dis[n + 1] = 0;
while(!q.empty()){
node tp = q.top(); q.pop();
int x = tp.x;
if(x == n * (n + 1)){
ans = min(ans, dis[x]);
continue;
}
else if(x == n * n || x == n * n + n - 1){
ans = min(ans, dis[x] + t);
continue;
}
else if(x == (n - 1) * n || x == n * n - 1 || x == n * n + n - 2){
ans = min(ans, dis[x] + t * 2);
continue;
}
//模板
}
}
int main(){
ios::sync_with_stdio(false); cout.tie(0);
freopen("cow.in", "r", stdin);
freopen("cow.out", "w", stdout);
n = read(), t = read();
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
g[i][j] = read();
}
}
for(int i = 1; i <= n; i ++){
for(int j = 1; j <= n; j ++){
for(int k = 0; k < 16; k ++){
int xx = i + dx[k], yy = j + dy[k];
if(xx < 1 || xx > n || yy < 1 || yy > n) continue;
add(i * n + j, xx * n + yy, g[xx][yy] + 3 * t);
}
}
}
dj();
cout << ans;
return 0;
}
```
:::
::::
---
:::align{center}
$\Huge{Thank}$ $\Huge{for}$ $\Huge{your}$ $\Huge{watching!!!}
:::
:::info[小彩蛋]
:::