当前位置:网站首页>AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
2022-07-02 18:27:00 【乔大先生】
AcWing 903. 昂贵的聘礼
这题的难点在建图,巧妙之处在于建立虚拟起点,让每条路变成虚拟起点(图中的S,代码中的点0)到达终点(点1)的一条路径,然后找到最短路径
#include<bits/stdc++.h>
using namespace std;
const int N = 110, INF = 0x3f3f3f3f;
#define rep(i, a, b) for(int i = a; i <= b; i ++ )
#define rop(i, a, b) for(int i = a; i < b; i ++ )
int w[N][N];
int level[N]; //主人等级
int d[N];
int n, m;
bool st[N];
int dijkstra(int down, int up){
memset(d, 0x3f, sizeof d);
memset(st, 0, sizeof st);
d[0] = 0;
rep(i, 1, n){
int t = -1;
rep(j, 0, n){
//这里找的是离i点最近的点,所以要包含虚拟起点0
if(!st[j] && (t == -1 || d[t] > d[j])) t = j; //这一步是找到目前距离最小的点用于更新其他点的距离,所以要先判断这个点有没有用过
}
st[t] = true; //标记这个点已经用过,因为每个点第一次被用时都是距离最近的,后续不会再被更新成更小的距离
rep(j, 1, n){
if(level[j] >= down && level[j] <= up){
d[j] = min(d[j], d[t] + w[t][j]);
}
}
}
return d[1];
}
int main()
{
cin>>m>>n;
memset(w, 0x3f, sizeof w);
rep(i, 1, n) w[i][i] = 0;
rep(i, 1, n){
int price, cnt;
cin>>price>>level[i]>>cnt;
w[0][i] = min(w[0][i], price);
while(cnt -- ){
int pr, id;
cin>>id>>pr;
w[id][i] = min(w[id][i], pr); //题意是id可以替换i,建图的时候是id向i的边,所以应该是w[id][i]
}
}
int res = INF;
rep(i, level[1] - m, level[1]){
res = min(res, dijkstra(i, i + m));
}
cout<<res<<endl;
return 0;
}
边栏推荐
- 【测试开发】一文带你了解什么是软件测试
- According to the atlas of data security products and services issued by the China Academy of information technology, meichuang technology has achieved full coverage of four major sectors
- [error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
- Imitation Jingdong magnifying glass effect (pink teacher version)
- 思维意识转变是施工企业数字化转型成败的关键
- Which video recording software is better for the computer
- Juypter notebook modify the default open folder and default browser
- Preprocessing and preprocessing macros
- 云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
- PHP非对称加密方法私钥及公钥加密解密的方法
猜你喜欢
Introduction to the paper | analysis and criticism of using the pre training language model as a knowledge base
开发固定资产管理系统,开发固定资产管理系统用什么语音
《重构:改善既有代码的设计》读书笔记(上)
What is 9D movie like? (+ common sense of dimension space)
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
How performance testing creates business value
性能测试如何创造业务价值
Quanzhi A33 uses mainline u-boot
IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
随机推荐
Reading notes of "the way to clean structure" (Part 2)
Codeworks 5 questions per day (1700 average) - day 4
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
Usage of ieda refactor
GMapping代码解析[通俗易懂]
Typescript 之 快速入门
虚拟机初始化脚本, 虚拟机相互免秘钥
MySQL advanced (Advanced) SQL statement
PHP-Parser羽毛球预约小程序开发require线上系统
思考变量引起的巨大变化
mysql备份后缀是什么_mysql备份还原
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
How to print mybats log plug-in using XML file
End to end object detection with transformers (Detr) paper reading and understanding
2022.7.1-----leetcode.241
Date tool class (updated from time to time)
仿京东放大镜效果(pink老师版)
高级性能测试系列《24. 通过jdbc执行sql脚本》
移动机器人路径规划:人工势场法[通俗易懂]
IEDA refactor的用法