当前位置:网站首页>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;
}
边栏推荐
- Tutoriel (5.0) 10. Dépannage * fortiedr * fortinet Network Security expert NSE 5
- AcWing 1137. 选择最佳线路 题解(最短路)
- juypter notebook 修改默认打开文件夹以及默认浏览器
- 第七章-类基础
- MySQL
- Which video recording software is better for the computer
- 数据降维——主成分分析
- AcWing 903. 昂贵的聘礼 题解(最短路—建图、dijkstra)
- xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
- Typescript 之 快速入门
猜你喜欢

Registration opportunity of autowiredannotationbeanpostprocessor under annotation development mode

开发固定资产管理系统,开发固定资产管理系统用什么语音

Advanced performance test series "24. Execute SQL script through JDBC"

Reading notes of "the way to clean structure" (Part 2)

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

安装单机redis详细教程

数据降维——主成分分析

450-深信服面经1

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统

Markdown basic grammar
随机推荐
C的内存管理
C文件输入操作
函数高阶-柯里化实现
Codeforces Round #802 (Div. 2) 纯补题
以太网PHY层芯片LAN8720A简介
AcWing 1134. 最短路计数 题解(最短路)
Golang:[]byte to string
AcWing 1129. 热浪 题解(最短路—spfa)
Mobile robot path planning: artificial potential field method [easy to understand]
2022 compilation principle final examination recall Edition
metric_logger小解
高级性能测试系列《24. 通过jdbc执行sql脚本》
NPOI导出Excel2007
中国信通院《数据安全产品与服务图谱》,美创科技实现四大板块全覆盖
Tutorial (5.0) 10 Troubleshooting * fortiedr * Fortinet network security expert NSE 5
IEDA refactor的用法
Binary operation
Markdown basic grammar
Chapter 7 - class foundation
juypter notebook 修改默认打开文件夹以及默认浏览器