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

寒假刷题第六天

PTA甲级

1030 Travel Plan

迪杰斯特拉

#include<iostream>
#include<vector>
#include<cstring>using namespace std;const int N = 510 , INF = 0x3f3f3f3f3f;
int n , m , s , d;
int g[N][N] , cost[N][N] , dist[N] , min_cost[N];
bool st[N];
int path[N];int main()
{memset(g , 0x3f , sizeof g) , memset(cost , 0x3f , sizeof cost);memset(dist , 0x3f , sizeof dist) , memset(min_cost , 0x3f , sizeof min_cost);memset(st , 0 , sizeof st);cin >> n >> m >> s >> d;while(m --){int a , b , c , e;cin >> a >> b >> c >> e;g[a][b] = g[b][a] = c;cost[a][b] = cost[b][a] = e;}dist[s] = 0;min_cost[s] = 0;for(int i = 0;i < n;i ++){int t = -1;for(int j = 0;j < n;j ++)if(!st[j] && (t == -1 || dist[t] > dist[j]))t = j;st[t] = true;for(int j = 0;j < n;j ++){if(dist[j] > dist[t] + g[t][j]) {dist[j] = dist[t] + g[t][j];path[j] = t;min_cost[j] = min_cost[t] + cost[t][j];}else if(dist[j] == dist[t] + g[t][j]){if(min_cost[j] > min_cost[t] + cost[t][j]){min_cost[j] = min_cost[t] + cost[t][j];path[j] = t;}}}}vector<int>res;for(int i = d;i != s;i = path[i]) res.push_back(i);res.push_back(s);for(int i = res.size() - 1;i >= 0;i --)cout << res[i] << " ";cout << dist[d] << " " << min_cost[d];return 0;
}

1034 Head of a Gang

#include<iostream>
#include<algorithm>
#include<unordered_map>
#include<unordered_set>
#include<vector>
#include<set>using namespace std;typedef pair<string , int> PSI;
int n , k;
unordered_map<string , vector<PSI>>g;
unordered_set<string>se;
unordered_map<string , bool>st;
unordered_map<string , int>total;int dfs(string i , vector<string>&v)
{st[i] = true;v.push_back(i);int sum = 0;for(auto j : g[i]){string x = j.first;int y = j.second;sum += y;if(!st[x]) sum += dfs(x , v);}return sum;
}int main()
{cin >> n >> k;while(n --){string a , b;int x;cin >> a >> b >> x;g[a].push_back({b , x});g[b].push_back({a , x});se.insert(a) , se.insert(b);total[a] += x , total[b] += x;}vector<PSI>res;for(auto i : g){vector<string>v;int sum = dfs(i.first , v) / 2;if(v.size() <= 2) continue;if(sum <= k) continue;string boss = v[0];for(auto j : v)if(total[boss] < total[j]) boss = j;res.push_back({boss , v.size()});}cout << res.size() << endl;sort(res.begin() , res.end());for(auto i : res)cout << i.first << " " << i.second << endl;return 0;
}

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

相关文章:

  • 深度学习笔记(七)——基于Iris/MNIST数据集构建基础的分类网络算法实战
  • Windows启动MongoDB服务报错(错误 1053:服务没有及时响应启动或控制请求)
  • Android Framework 常见解决方案(25-2)定制CPUSET解决方案-system修改及编译部分调整
  • OpenAI推出GPT商店和ChatGPT Team服务
  • 3D建模素材分层渲染怎么操作?
  • SAICP(模拟退火迭代最近点)的实现
  • FineBI实战项目一(23):订单商品分类词云图分析开发
  • DOS命令
  • 【Python】torch中的.detach()函数详解和示例
  • 二级域名分发系统源码 对接易支付php源码 全开源
  • 二分查找与搜索树的高频问题(算法村第九关白银挑战)
  • Python爬虫快速入门
  • 部署MinIO
  • RK3566环境搭建
  • 精确掌控并发:滑动时间窗口算法在分布式环境下并发流量控制的设计与实现
  • Python展示 RGB立方体的二维切面视图
  • 03 顺序表
  • 2023年全球软件开发大会(QCon北京站2023)9月:核心内容与学习收获(附大会核心PPT下载)
  • ChatGPT 和 文心一言 的优缺点及需求和使用场景
  • 架构师之超时未支付的订单进行取消操作的几种解决方案
  • 【容器固化】 OS技术之OpenStack容器固化的实现原理及操作
  • 设置 SSH 通过密钥登录
  • 1.6 面试经典150题 - 买卖股票的最佳时机
  • locust快速入门--使用分布式提高测试压力
  • K8s(三)Pod资源——pod亲和性与反亲和性,pod重启策略
  • 免费的域名要不要?
  • 高通sm7250与765G芯片是什么关系?(一百八十一)
  • [Python进阶] Python操作MySQL数据库:pymysql
  • Vue3实现带点击外部关闭对应弹出框(可共用一个变量)
  • 可视化试题(一)