当前位置:网站首页>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;
}
边栏推荐
- 安装单机redis详细教程
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 網絡安全專家 NSE 5
- [error record] problems related to the installation of the shuttle environment (follow-up error handling after executing the shuttle doctor command)
- Golang:[]byte to string
- 教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
- xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
- Registration opportunity of autowiredannotationbeanpostprocessor in XML development mode
- Introduction of Ethernet PHY layer chip lan8720a
- Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
- AcWing 1135. 新年好 题解(最短路+搜索)
猜你喜欢

全志A33使用主线U-Boot

How performance testing creates business value

Use cheat engine to modify money, life and stars in Kingdom rush

End-to-End Object Detection with Transformers(DETR)论文阅读与理解

中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖

Usage of ieda refactor

Machine learning notes - time series prediction research: monthly sales of French champagne

Quanzhi A33 uses mainline u-boot

Codeworks 5 questions per day (1700 average) - day 4

高级性能测试系列《24. 通过jdbc执行sql脚本》
随机推荐
云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
Which video recording software is better for the computer
AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
2022.7.1-----leetcode. two hundred and forty-one
AcWing 1127. 香甜的黄油 题解(最短路—spfa)
452-strcpy、strcat、strcmp、strstr、strchr的实现
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
注解开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
What is 9D movie like? (+ common sense of dimension space)
zabbix5客户端安装和配置
AcWing 1137. 选择最佳线路 题解(最短路)
Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
移动机器人路径规划:人工势场法[通俗易懂]
450-深信服面经1
MySQL
Transformation of thinking consciousness is the key to the success or failure of digital transformation of construction enterprises
Binary operation
End to end object detection with transformers (Detr) paper reading and understanding
How to print mybats log plug-in using XML file
LeetCode 0871. Minimum refueling times - similar to poj2431 jungle adventure