当前位置:网站首页>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;
}
边栏推荐
- IDEA编辑器去掉sql语句背景颜色SQL语句警告No data sources are configured to run this SQL...和SQL Dialect is Not Config
- 为什么要做企业固定资产管理系统,企业如何加强固定资产管理
- Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
- Mobile robot path planning: artificial potential field method [easy to understand]
- The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
- PHP非对称加密方法私钥及公钥加密解密的方法
- Quanzhi A33 uses mainline u-boot
- MySQL advanced (Advanced) SQL statement
- NPOI导出Excel2007
- Codeworks 5 questions per day (1700 average) - day 4
猜你喜欢

Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出

Compile oglpg-9th-edition source code with clion

AcWing 342. 道路与航线 题解 (最短路、拓扑排序)

安装单机redis详细教程

守望先锋世界观架构 ——(一款好的游戏是怎么来的)

How performance testing creates business value

Yunna | why use the fixed asset management system and how to enable it

codeforces每日5题(均1700)-第四天

High frequency interview questions

Yolov3 trains its own data set to generate train txt
随机推荐
AcWing 1129. 热浪 题解(最短路—spfa)
Use cheat engine to modify money, life and stars in Kingdom rush
《代码整洁之道》读书笔记
AcWing 340. 通信线路 题解(二分+双端队列BFS求最短路)
Idea editor removes SQL statement background color SQL statement warning no data sources are configured to run this SQL And SQL dialect is not config
The mybatieshelperpro tool can be generated to the corresponding project folder if necessary
Virtual machine initialization script, virtual machine mutual secret key free
Windows2008r2 installing php7.4.30 requires localsystem to start the application pool, otherwise 500 error fastcgi process exits unexpectedly
Qpropertyanimation use and toast case list in QT
zabbix5客户端安装和配置
Watchful pioneer world outlook Architecture - (how does a good game come from)
[0701] [paper reading] allowing data imbalance issue with perforated input during influence
Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
451-memcpy、memmove、memset的实现
以太网PHY层芯片LAN8720A简介
《代碼整潔之道》讀書筆記
《重构:改善既有代码的设计》读书笔记(上)
Juypter notebook modify the default open folder and default browser
Chapter 7 - class foundation
MySQL advanced (Advanced) SQL statement