2026.7.29思沁集训题解
A - Instrction Arrangement
题意
共有 条指令和 组依赖关系(表示 和 之间的安全距离为 ,且 必须在 之后执行),每条指令只需 纳秒即可完成,编排指令顺序,使得 能在最短时间内完成所有指令。
思路
这是一道拓扑排序 + dp模版题
过程
模版题,不用多说
伪代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, m;
const int N = 1005;
struct P{
int v, w;
};
vector<P> g[N];
int in[N];
queue<int> q;
int dp[N];
void topsort(){
for(int i = 0; i < n; i++){
if(in[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
for(auto &e : g[u]){
int v = e.v;
int w = e.w;
if(dp[v] < dp[u] + w){
dp[v] = dp[u] + w;
}
in[v]--;
if(in[v] == 0){
q.push(v);
}
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
while(cin >> n >> m){
while(!q.empty()){
清空1
}
for(int i = 0;i < n;i++){
清空2
}
for(int i = 1;i <= m;i++){
建图
}
topsort();
int ans = 0;
for(int i = 1;i <= n;i++){
ans = max(ans, dp[i]);
}
cout << ans<< '\n';
}
return 0;
}AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
int n, m;
const int N = 1005;
struct P{
int v, w;
};
vector<P> g[N];
int in[N];
queue<int> q;
int dp[N];
void topsort(){
for(int i = 0; i < n; i++){
if(in[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
for(auto &e : g[u]){
int v = e.v;
int w = e.w;
if(dp[v] < dp[u] + w){
dp[v] = dp[u] + w;
}
in[v]--;
if(in[v] == 0){
q.push(v);
}
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
while(cin >> n >> m){
while(!q.empty()){
q.pop();
}
for(int i = 0;i < n;i++){
g[i].clear();
in[i] = 0;
dp[i] = 1;
}
for(int i = 1;i <= m;i++){
int u, v, w;
cin >> u>> v>> w;
g[u].push_back({v, w});
in[v]++;
}
topsort();
int ans = 0;
for(int i = 1;i <= n;i++){
ans = max(ans, dp[i]);
}
cout << ans<< '\n';
}
return 0;
}B - Timeline G
题意
有 次挤奶,编号 。第 次挤奶发生时间不能早于 天。另有 条记忆,每条记忆 表示第 次挤奶发生在第 次挤奶之后至少 天。求满足所有条件下每次挤奶的最早可能日期
思路
这是一道拓扑排序 + dp模版题
过程
这道题和上一道题类似,不必多说
伪代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 100005;
int n, m, c;
int dp[N], in[N];
vector <pair <int,int>> g[N];
queue <int> q;
void topsort(){
for(int i = 1;i <= n;i++){
if(in[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
for(auto e : g[u]){
int v = e.first, w = e.second;
dp[v] = max(dp[v], dp[u] + w);
in[v]--;
if(in[v] == 0){
q.push(v);
}
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> m>> c;
for(int i = 1;i <= n;i++){
cin >> dp[i];
}
for(int i = 1;i <= c;i++){
建图
}
topsort();
for(int i = 1;i <= n;i++){
cout << dp[i]<< '\n';
}
return 0;
}AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 100005;
int n, m, c;
int dp[N], in[N];
vector <pair <int,int>> g[N];
queue <int> q;
void topsort(){
for(int i = 1;i <= n;i++){
if(in[i] == 0){
q.push(i);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
for(auto e : g[u]){
int v = e.first, w = e.second;
dp[v] = max(dp[v], dp[u] + w);
in[v]--;
if(in[v] == 0){
q.push(v);
}
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> m>> c;
for(int i = 1;i <= n;i++){
cin >> dp[i];
}
for(int i = 1;i <= c;i++){
int a, b, x;
cin >> a>> b>> x;
g[a].push_back({b, x});
in[b]++;
}
topsort();
for(int i = 1;i <= n;i++){
cout << dp[i]<< '\n';
}
return 0;
}C - Longest Path
题意
给定一个 个顶点、 条边的有向无环图(),求其中最长有向路径的边数
思路
这是一道拓扑排序 + dp模版题
过程
和前两题很像,只不过求得是路径长度
伪代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 100005;
int n, m;
int in[N], dp[N];
vector <int> g[N];
queue <int> q;
void topsort(){
for(int i = 1;i <= n;i++){
if(in[i] == 0){
q.push(i);
}
}
while(q.size()){
int u = q.front();
q.pop();
for(auto v : g[u]){
dp[v] = max(dp[v], dp[u] + 1);
in[v]--;
if(in[v] == 0){
q.push(v);
}
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> m;
for (int i = 1;i <= m;i++) {
建图
}
topsort();
int ans = 0;
for(int i = 1;i <= n;i++){
ans = max(ans, dp[i]);
}
cout << ans;
return 0;
}AC代码
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 100005;
int n, m;
int in[N], dp[N];
vector <int> g[N];
queue <int> q;
void topsort(){
for(int i = 1;i <= n;i++){
if(in[i] == 0){
q.push(i);
}
}
while(q.size()){
int u = q.front();
q.pop();
for(auto v : g[u]){
dp[v] = max(dp[v], dp[u] + 1);
in[v]--;
if(in[v] == 0){
q.push(v);
}
}
}
}
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;
g[x].push_back(y);
in[y]++;
}
topsort();
int ans = 0;
for(int i = 1;i <= n;i++){
ans = max(ans, dp[i]);
}
cout << ans;
return 0;
}D - 神经网络
题意
给定一个有向无环图(DAG),节点代表神经元,边代表信号传递路径并带有权值。每个神经元有初始状态 和阈值 。根据公式(因为太难写了就不写了)计算出每个神经元的状态。只有状态大于 的神经元会向下一层发送信号(信号强度为 )。求所有输出层(出度为 )中最终状态大于 的神经元编号和状态,若没有则输出
思路
这是一道拓扑排序 + dp + 状态传播题
过程
- 取出当前节点 。遍历它指向的所有后继节点
- 如果 ,说明 处于兴奋状态,它会向后继发送信号,累加
- 当 的入度减为 时,意味着它的所有前驱都已经处理完毕。此时计算它的最终状态 。无论计算结果是否大于 ,都必须将 加入队列。因为只有入队后才能去处理 的出边,从而保证整个 的拓扑排序正常进行(例如当中间层节点处于平静状态时,虽然不发送信号,但依然会减少后继节点的入度)
- 结果就是出度为 且状态大于 的节点
伪代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 105;
int n, p;
int a[N], b[N], sum[N];
int in[N], out[N];
vector <pair <int, int>> g[N];
queue <int> q;
void topsort(){
for(int i = 1;i <= n;i++){
if (in[i] == 0) {
q.push(i);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
for(auto e : g[u]){
int v = e.first, w = e.second;
if(a[u] > 0){
sum[v] += w * a[u];
}
in[v]--;
if(in[v] == 0){
a[v] = sum[v] - b[v];
q.push(v);
}
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> p;
for(int i = 1;i <= n;i++){
cin >> a[i]>> b[i];
}
for(int i = 1;i <= p;i++){
建图
}
topsort();
bool f = false;
for(int i = 1;i <= n;i++){
if(out[i] == 0 && a[i] > 0){
cout << i<< ' '<< a[i] << '\n';
f = true;
}
}
if (!f) {
cout << "NULL"<< '\n';
}
return 0;
}AC代码
#include <bits/stdc++.h>
#define int long long
using namespace std;
const int N = 105;
int n, p;
int a[N], b[N], sum[N];
int in[N], out[N];
vector <pair <int, int>> g[N];
queue <int> q;
void topsort(){
for(int i = 1;i <= n;i++){
if (in[i] == 0) {
q.push(i);
}
}
while(!q.empty()){
int u = q.front();
q.pop();
for(auto e : g[u]){
int v = e.first, w = e.second;
if(a[u] > 0){
sum[v] += w * a[u];
}
in[v]--;
if(in[v] == 0){
a[v] = sum[v] - b[v];
q.push(v);
}
}
}
}
signed main(){
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
cin >> n>> p;
for(int i = 1;i <= n;i++){
cin >> a[i]>> b[i];
}
for(int i = 1;i <= p;i++){
int u, v, w;
cin >> u>> v>> w;
g[u].push_back({v, w});
in[v]++;
out[u]++;
}
topsort();
bool f = false;
for(int i = 1;i <= n;i++){
if(out[i] == 0 && a[i] > 0){
cout << i<< ' '<< a[i] << '\n';
f = true;
}
}
if (!f) {
cout << "NULL"<< '\n';
}
return 0;
}E - 菜肴制作
题意
思路
过程
伪代码
AC代码
allow pasting
localStorage.cp6_player_id=‘14b18c2c-c15d-4f1e-a7f1-ead07687ba88’
sc stop tdnetfilter