当前位置:网站首页>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;
}
边栏推荐
- 函数高阶-柯里化实现
- Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- Web2.0 giants have deployed VC, and tiger Dao VC may become a shortcut to Web3
- GMapping代码解析[通俗易懂]
- codeforces每日5题(均1700)-第四天
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- Quanzhi A33 uses mainline u-boot
- In pytorch function__ call__ And forward functions
- xml开发方式下AutowiredAnnotationBeanPostProcessor的注册时机
猜你喜欢
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management
Machine learning notes - time series prediction research: monthly sales of French champagne
Yolov3 trains its own data set to generate train txt
全志A33使用主线U-Boot
450-深信服面经1
Refactoring: improving the design of existing code (Part 1)
AcWing 342. 道路与航线 题解 (最短路、拓扑排序)
Data dimensionality reduction principal component analysis
Develop fixed asset management system, what voice is used to develop fixed asset management system
高级性能测试系列《24. 通过jdbc执行sql脚本》
随机推荐
《代碼整潔之道》讀書筆記
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
教程篇(5.0) 10. 故障排除 * FortiEDR * Fortinet 网络安全专家 NSE 5
Golang:[]byte to string
codeforces每日5题(均1700)-第四天
Refactoring: improving the design of existing code (Part 1)
使用 Cheat Engine 修改 Kingdom Rush 中的金钱、生命、星
453-atoi函数的实现
以太网PHY层芯片LAN8720A简介
In pytorch function__ call__ And forward functions
Introduction of Ethernet PHY layer chip lan8720a
Data dimensionality reduction factor analysis
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
AcWing 1135. 新年好 题解(最短路+搜索)
mysql备份后缀是什么_mysql备份还原
教程篇(5.0) 09. RESTful API * FortiEDR * Fortinet 网络安全专家 NSE 5
MySQL
全志A33使用主线U-Boot
Microservice technology - distributed global ID in high concurrency
Fastdfs installation