当前位置:网站首页>AcWing 903. Expensive bride price solution (the shortest path - building map, Dijkstra)
AcWing 903. Expensive bride price solution (the shortest path - building map, Dijkstra)
2022-07-02 19:40:00 【Mr. Qiao Da】
AcWing 903. Expensive betrothal gifts
The difficulty of this problem is building a map , The trick is to establish a virtual starting point , Let each road become a virtual starting point ( In the picture S, Points in the code 0) Reach the end point ( spot 1) A path for , Then find the shortest path
#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]; // Master level
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){
// What we are looking for here is Li i Point the nearest point , So include the virtual starting point 0
if(!st[j] && (t == -1 || d[t] > d[j])) t = j; // This step is to find the point with the smallest distance at present and update the distance of other points , So we must first judge whether this point has been used
}
st[t] = true; // Marking this point has been used , Because each point is the closest when it is first used , Later, it will not be updated to a smaller distance
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); // The question is id Can replace i, When building the map id towards i The edge of , So it should be 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;
}
边栏推荐
- 《重构:改善既有代码的设计》读书笔记(上)
- Golang concurrent programming goroutine, channel, sync
- Detailed tutorial on installing stand-alone redis
- Machine learning notes - time series prediction research: monthly sales of French champagne
- 451 implementation of memcpy, memmove and memset
- AcWing 383. Sightseeing problem solution (shortest circuit)
- Solution: vs2017 cannot open the source file stdio h main. H header document [easy to understand]
- 搭建哨兵模式reids、redis从节点脱离哨兵集群
- AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
- KS004 基于SSH通讯录系统设计与实现
猜你喜欢
《重构:改善既有代码的设计》读书笔记(上)
KT148A语音芯片ic的硬件设计注意事项
Build a master-slave mode cluster redis
守望先锋世界观架构 ——(一款好的游戏是怎么来的)
Reading notes of "the way to clean structure" (Part 2)
编写完10万行代码,我发了篇长文吐槽Rust
嵌入式(PLD) 系列,EPF10K50RC240-3N 可编程逻辑器件
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
SQLite 3.39.0 release supports right external connection and all external connection
定了,就是它!
随机推荐
KT148A语音芯片ic的软件参考代码C语言,一线串口
《架构整洁之道》读书笔记(下)
AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
Implementation of 452 strcpy, strcat, StrCmp, strstr, strchr
Use cheat engine to modify money, life and stars in Kingdom rush
《重构:改善既有代码的设计》读书笔记(上)
字典
使用IDM下载百度网盘的文件(亲测有用)[通俗易懂]
AcWing 1134. 最短路计数 题解(最短路)
《MongoDB入门教程》第03篇 MongoDB基本概念
What is the MySQL backup suffix_ MySQL backup restore
Reading notes of code neatness
AcWing 342. Road and route problem solving (shortest path, topological sorting)
多态的理解以及作用
Data Lake (XII): integration of spark3.1.2 and iceberg0.12.1
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
450-深信服面经1
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
Think about the huge changes caused by variables