当前位置: 首页 > news >正文

算法训练营day72

题目:117. 软件构建 (kamacoder.com)

#include<iostream>
#include<unordered_map>
#include<vector>
#include<queue>using namespace std;int main() {int n, m;cin >> n >> m;vector<int> indegree(n, 0);unordered_map<int, vector<int>> map;int s, t;while(m--) {cin >> s >> t;indegree[t]++;map[s].push_back(t);}queue<int> qu;for(int i = 0;i < n;i++) {if(indegree[i] == 0) {qu.push(i);}}vector<int> result;while(!qu.empty()) {int cur = qu.front();qu.pop();result.push_back(cur);vector<int> files = map[cur];if(files.size()) {for(int i = 0;i < files.size();i++) {indegree[files[i]]--;if(indegree[files[i]] == 0) qu.push(files[i]);}   }}if(result.size() == n) {for(int i = 0;i < n - 1;i++) {cout << result[i] << " ";}cout << result[n - 1] << endl;}else {cout << -1 << endl;}return 0;
}

题目2:47. 参加科学大会(第六期模拟笔试) (kamacoder.com)

#include<bits/stdc++.h>using namespace std;int main() {int n, m;cin >> n >> m;vector<vector<int>> grid(n + 1, vector<int>(n + 1, INT_MAX));vector<bool> vistied(n + 1, false);vector<int> minDist(n + 1, INT_MAX);while(m--) {int s, e, v;cin >> s >> e >> v;grid[s][e] = v;}minDist[1] = 0;int cur = 1;for(int i = 1;i <= n;i++) {int minVal = INT_MAX;for(int v = 1;v <= n;v++) {if(!vistied[v] && minDist[v] < minVal) {minVal = minDist[v];cur = v;}}vistied[cur] = true;for(int j = 1;j <= n;j++) {if(!vistied[j] && grid[cur][j] != INT_MAX && minDist[cur] + grid[cur][j] < minDist[j]) {minDist[j] = minDist[cur] + grid[cur][j];}}}if(minDist[n] == INT_MAX) cout << -1 << endl;else cout << minDist[n] << endl;return 0;
}

http://www.lryc.cn/news/401532.html

相关文章:

  • C语言------指针讲解(2)
  • 大数据技术基础
  • 【文心智能体】前几天百度热搜有一条非常有趣的话题《00后疯感工牌》,看看如何通过低代码工作流方式实现图片显示
  • C++20中的constinit说明符
  • Java 中的正则表达式
  • 华为配置蓝牙终端定位实验
  • 搭建hadoop+spark完全分布式集群环境
  • pytorch-pytorch之LSTM
  • jvm优化
  • 网络安全——防御课实验二
  • 朴素模式匹配算法与KMP算法(非重点)
  • [k8s源码]2.CURD deployment
  • 使用base64通用文件上传
  • Python深度学习
  • django报错(三):No crontab program或got an unexpected keyword argument ‘user’
  • 数据库(创建数据库和表)
  • Log4j的原理及应用详解(一)
  • ubuntu系统Docker常用命令
  • 韦东山嵌入式linux系列-驱动设计的思想(面向对象/分层/分离)
  • 0/1背包
  • Linux的进程和权限的基本命令
  • 鼠标录制工具怎么挑选?9款电脑鼠标录制工具分享(2024)
  • C1W4.LAB.Vector manipulation+Hash functions and multiplanes
  • YOLOv8改进 | 检测头 | 融合渐进特征金字塔的检测头【AFPN4】
  • 数据采集监控平台:挖掘数据价值 高效高速生产!
  • 【算法笔记自学】第 9 章 提高篇(3)——数据结构专题(2)
  • Objective-C 中字符串的保存位置
  • git 想要创建一个新的本地分支并检出远程分支的内容
  • C语言学习笔记[24]:循环语句while②
  • 安全运营概述