跳到正文

个人记录

7-20课程解析+总结

xiangrui0305约 13 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年4月30日
篇幅
约 13 分钟

版本

  1. v1当前

7-20课程解析+总结(3.0版本)

题目解析+总结

P1135

在一个长度为nn的数轴上,第ii个点对应的数字为aia_i。从第AA个点上,可以实现以下操作:

  • 左移:在不越界的情况下,可以移动到A+aiA+a_i个格子上
  • 右移:在不越界的情况下,可以移动到A−aiA-a_i个格子上

那么,从第AA个点到第BB个点至少要多少次以上操作呢?

分析

这道题有两种方法来写这道题。 方法一:纯 BFS 搜索。 每次BFS遍历,都有两种选择:往左移动和往右移动。 在没有越界和没有走过的情况下,可以执行这两种操作。

C++
if(top.x + k[top.x] <= n && !vis[top.x + k[top.x]]){
  vis[top.x + k[top.x]] = 1;
  op.push({top.x + k[top.x],top.step+1});
}
if(top.x - k[top.x] >= 1 && !vis[top.x - k[top.x]]){
  vis[top.x - k[top.x]] = 1;
  op.push({top.x - k[top.x],top.step+1});
}

建议函数为int类型,在找到终点时直接返回操作次数,找不到则返回−1-1。

C++
if(top.x == b) return top.step;

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a,b,k[205],vis[205];
struct node{
	int x,step;
};queue<node>op;
int BFS(int cur){
	vis[cur] = 1;
	op.push({cur,0});
	while(op.size()){
		node top = op.front();
		op.pop();
		if(top.x == b) return top.step;
		if(top.x + k[top.x] <= n && !vis[top.x + k[top.x]]){
			vis[top.x + k[top.x]] = 1;
			op.push({top.x + k[top.x],top.step+1});
		}
		if(top.x - k[top.x] >= 1 && !vis[top.x - k[top.x]]){
			vis[top.x - k[top.x]] = 1;
			op.push({top.x - k[top.x],top.step+1});
		}
	}
	return -1;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>a>>b;
    for(int i=1;i<=n;++i) cin>>k[i];
    cout<<BFS(a)<<endl;
	return 0;
}

方法二:图论 + BFS 搜索。 输入时,若这个点在执行操作时满足条件,则将它存入图中。

C++
for(int i=1;i<=n;++i){
    int x;
    cin>>x;
    if(i + x <= n) pic[i].push_back(i+x);
    if(i - x >= 1) pic[i].push_back(i-x);
}

这样,在遍历的时候,直接遍历它能走到的点,就可以了。

C++
for(int i=0;i<pic[top.x].size();++i){
    if(!vis[pic[top.x][i]]){
        vis[pic[top.x][i]] = 1;
        op.push({pic[top.x][i],top.step+1});
    }
}

代码:

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,a,b,vis[205];
vector<int>pic[205];
struct node{
	int x,step;
};queue<node>op;
int BFS(int cur){
	vis[cur] = 1;
	op.push({cur,0});
	while(op.size()){
		node top = op.front();
		op.pop();
		if(top.x == b) return top.step;
		for(int i=0;i<pic[top.x].size();++i){
			if(!vis[pic[top.x][i]]){
				vis[pic[top.x][i]] = 1;
				op.push({pic[top.x][i],top.step+1});
			}
		}
	}
	return -1;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n>>a>>b;
	for(int i=1;i<=n;++i){
		int x;
		cin>>x;
		if(i + x <= n) pic[i].push_back(i+x);
		if(i - x >= 1) pic[i].push_back(i-x);
	}
	cout<<BFS(a)<<endl;
	return 0;
}

P5663

工厂里有nn位工人,编号为11~nn,某些工人之间存在一个双向传送带。 若xx号工人要生产一个被加工到第LL阶段的零件,则所有与xx号工人有传送带直接相连的工人,都需要生产一个被加工到第L−1L−1阶段的零件或者提供原材料(但xx号工人自己无需生产第L−1L−1阶段的零件)。 给出qq张工单,表示xx号工人要生产一个被加工到第LL阶段的零件。11号工人是否需要提供原材料?

分析

样例解释: 样例一:

代码
3 2 3
1 2
2 3
1 1
1 2
1 3

①11号员工要生产第11阶段的零件,需要22号员工提供原材料,输出”No”; ②11号员工要生产第22阶段的零件,需要22号员工生产第11阶段的零件, 2\quad2号员工要生产第11阶段的零件,需要11号员工、33号员工提供原材料,输出”Yes”; ③11号员工要生产第33阶段的零件,需要22号员工生产第22阶段的零件, 2\quad2号员工要生产第22阶段的零件,需要11号员工、33号员工生产第11阶段的零件, 1\quad1号员工要生产第11阶段的零件,需要22号员工提供原材料,输出”No”; 像这种情况,从11出发,所有的偶数阶段都可以,否则都不可以 样例:

代码
......
2 1
2 2
2 3
2 4
2 5
2 6

输出:

代码
Yes
No
Yes
Yes
Yes
Yes

像这种情况,从11出发,到22的步数≥4\ge 4,且是偶数,一定可以55从11出发,到22的步数为奇数,一定可以。

通过以上样例可以发现:

  • 若LL是≥2\ge2的偶数,一定可以;
  • 若LL是奇数,比较奇数的最短路径。l≥vxl \ge v_x,一定可以。
代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,q,vis1[100005],vis2[100005];
vector<int>CSD[100005];
struct node{
	int x,step;
};queue<node>op;
void BFS(){
	memset(vis1,0x3f,sizeof vis1);
	memset(vis2,0x3f,sizeof vis2);
	op.push({1,0});
	while(op.size()){
		node f = op.front();
		op.pop();
		for(int i=0;i<CSD[f.x].size();i++){
			int v = CSD[f.x][i];
			if((f.step+1)%2 == 1){
				if(f.step+1 < vis1[v]){
					vis1[v] = f.step+1;
					op.push({v,f.step+1});
				}
			}
			else{
				if(f.step+1 < vis2[v]){
					vis2[v] = f.step+1;
					op.push({v,f.step+1});
				}
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0); cout.tie(0);
	cin>>n>>m>>q;
	for(int i=1;i<=m;++i){
		int x,y;
		cin>>x>>y;
		CSD[x].push_back(y);
		CSD[y].push_back(x);
	}
	BFS();
	while(q--){
		int cur,yuan;
		cin>>cur>>yuan;
		if(yuan % 2 == 1){
			if(vis1[cur] > yuan) cout<<"No\n";
			else cout<<"Yes\n";
		}
		else{
			if(vis2[cur] > yuan) cout<<"No\n";
			else cout<<"Yes\n";
		}
	}
	return 0;
}

homework~

CF910A

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
vector<int>op[105];
int n,d,vis[105];
string s;
struct node{
	int x,step;
};
queue<node>LJ;
int BFS(int cur){
	LJ.push({cur,0});
	vis[cur] = 1;
	while(LJ.size()){
		node top = LJ.front();
		LJ.pop();
		if(top.x == n) return top.step;
		for(int i=0;i<op[top.x].size();++i){
			int v = op[top.x][i];
			if(vis[v]) continue;
			vis[v] = 1;
			LJ.push({v,top.step+1});
		}
	}
	return -1;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>d>>s;
    s = " " + s;
    for(int i=1;i<=n;++i){
    	if(s[i] == '1'){
    		for(int j=i+1;j<=i+d;++j){
    			if(s[j] == '1') op[i].push_back(j);
			}
		}
	}
	cout<<BFS(1)<<endl;
	return 0;
}

CF115A

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,vis[2005];
vector<int>op[2005];
queue<int>CSP;
void BFS(int cur){
	CSP.push(cur);
	while(CSP.size()){
		int top = CSP.front();
		CSP.pop();
		for(int i=0;i<op[top].size();++i){
			int v = op[top][i];
			if(vis[v] < vis[top] + 1) {
                vis[v] = vis[top] + 1;
                CSP.push(v);
            }
		}
	}
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n;
    for(int i=1;i<=n;++i){
    	int x;
    	cin>>x;
    	if(x != -1) op[x].push_back(i);
		else vis[i] = 1;
	}
	for(int i=1;i<=n;++i){
		if(vis[i] == 1) BFS(i);
	}
	int maxx = 0;
	for(int i=1;i<=n;++i){
		maxx = max(maxx,vis[i]);
	}
	cout<<maxx<<endl;
	return 0;
}

CF292B

代码
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n,m,P[100005],V[100005];
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;
    for(int i=1;i<=m;++i){
    	int x,y;
    	cin>>x>>y;
    	P[x]++,P[y]++;
	}
	for(int i=1;i<=n;++i) V[P[i]]++;
	if(V[1] == 2 && V[2] == n-2) cout<<"bus topology\n";
	else if(V[2] == n) cout<<"ring topology\n";
	else if(V[n-1] == 1 && V[1] == n-1) cout<<"star topology\n";
	else cout<<"unknown topology\n";
	return 0;
}
原文