当前位置:网站首页>AcWing 383. Sightseeing problem solution (shortest circuit)
AcWing 383. Sightseeing problem solution (shortest circuit)
2022-07-02 19:32:00 【Mr. Qiao Da】
AcWing 383. Sightseeing
It is similar to finding the shortest path , It's just another short circuit , It is necessary to consider under what circumstances it is necessary to update the secondary short circuit status , It's wonderful to use a two-dimensional array here , Worth learning
#include<bits/stdc++.h>
using namespace std;
const int N = 1010, M = 20010;
int cse;
int h[N], e[M], w[M], ne[M], idx;
int S, T, n, m;
int dist[N][2], cnt[N][2]; // First dimension record point , Is the second dimension record the shortest or secondary short circuit
bool st[N][2]; // First dimension record point , Is the second dimension record the shortest or secondary short circuit
struct Ver{
int id, type, dist;
bool operator> (const Ver &W) const{
return dist > W.dist;
}
};
void add(int a, int b, int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++ ;
}
int dijkstra(){
memset(st, 0, sizeof st);
memset(cnt, 0, sizeof cnt);
memset(dist, 0x3f, sizeof dist);
dist[S][0] = 0; // Initialize the distance value
cnt[S][0] = 1; // Number of initialization schemes
priority_queue<Ver, vector<Ver>, greater<Ver>>heap; // Big root pile
heap.push({
S, 0, 0}); // The first point into the heap
while(heap.size()){
Ver v = heap.top();
heap.pop();
int ver = v.id, type = v.type, d = v.dist, ct = cnt[ver][type];
if(st[ver][type]) continue;
st[ver][type] = true;
for(int i = h[ver]; ~i; i = ne[i]){
int j = e[i];
if(dist[j][0] > d + w[i]){
// First update the status of the secondary short circuit , Directly inherit the shortest state
dist[j][1] = dist[j][0];
cnt[j][1] = cnt[j][0];
heap.push({
j, 1, dist[j][1]});
// Update the status of the shortest circuit
dist[j][0] = d + w[i];
cnt[j][0] = ct;
heap.push({
j, 0, dist[j][0]});
}
else if(dist[j][0] == d + w[i]){
cnt[j][0] += ct;
}
else if(dist[j][1] > d + w[i]){
// Find a new secondary short circuit
dist[j][1] = d + w[i];
cnt[j][1] = ct;
heap.push({
j, 1, dist[j][1]}); // Remember to join the team after finding a new point state
}
else if(dist[j][1] == d + w[i]){
cnt[j][1] += ct;
}
}
}
int res = cnt[T][0];
if(dist[T][0] + 1 == dist[T][1]) res += cnt[T][1];
return res;
}
int main()
{
cin>>cse;
while(cse -- ){
cin>>n>>m;
memset(h, -1, sizeof h);
idx = 0;
while(m -- ){
int a, b, c;
cin>>a>>b>>c;
add(a, b, c);
}
cin>>S>>T;
cout<<dijkstra()<<endl;
}
return 0;
}
边栏推荐
- Gmapping code analysis [easy to understand]
- Horizontal ultra vires and vertical ultra vires [easy to understand]
- 高级性能测试系列《24. 通过jdbc执行sql脚本》
- AcWing 1127. 香甜的黄油 题解(最短路—spfa)
- Microservice technology - distributed global ID in high concurrency
- 【ERP软件】ERP体系二次开发有哪些危险?
- golang:[]byte转string
- Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
- Golang concurrent programming goroutine, channel, sync
- MySQL table historical data cleaning summary
猜你喜欢
AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
Quanzhi A33 uses mainline u-boot
Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
High frequency interview questions
Refactoring: improving the design of existing code (Part 1)
高级性能测试系列《24. 通过jdbc执行sql脚本》
End-to-End Object Detection with Transformers(DETR)论文阅读与理解
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
搭建哨兵模式reids、redis从节点脱离哨兵集群
Machine learning notes - time series prediction research: monthly sales of French champagne
随机推荐
451-memcpy、memmove、memset的实现
Educational Codeforces Round 129 (Rated for Div. 2) 补题题解
QT中的QPropertyAnimation使用和toast案列
Notes de lecture sur le code propre
Processing strategy of message queue message loss and repeated message sending
Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
453-atoi函数的实现
Data dimensionality reduction factor analysis
电脑使用哪个录制视频软件比较好
PHP非对称加密方法私钥及公钥加密解密的方法
Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
高级性能测试系列《24. 通过jdbc执行sql脚本》
Fastdfs installation
2022 compilation principle final examination recall Edition
AcWing 1135. 新年好 题解(最短路+搜索)
MySQL表历史数据清理总结
4274. 后缀表达式-二叉表达式树
股票证券公司排名,有安全保障吗
Refactoring: improving the design of existing code (Part 1)
AcWing 383. 观光 题解(最短路)