当前位置:网站首页>AcWing 1129. 热浪 题解(最短路—spfa)
AcWing 1129. 热浪 题解(最短路—spfa)
2022-07-02 18:27:00 【乔大先生】
AcWing 1129. 热浪
SPFA求最短路的,找到单源最短路,就是建立一个队列,按照图中的路径遍历,将更新过找到最短路径的点加入队列中,然后将队列中的元素依次出队,按照出队元素在图中继续遍历,最终找到最短路
#include<bits/stdc++.h>
using namespace std;
const int N = 7000;
int e[N * 2], ne[N * 2], h[N], w[N * 2], idx;
int T, C, S, E;
int dist[N];
bool st[N];
int q[N];
void add(int a, int b, int c){
e[idx] = b;
w[idx] = c;
ne[idx] = h[a];
h[a] = idx ++ ;
}
void spfa(){
memset(dist, 0x3f, sizeof dist);
dist[S] = 0;
int hh = 0, tt = 1;
q[0] = S;
st[S] = true;
while(hh != tt){
int t = q[hh ++ ];
if(t == N) hh = 0; //如果遍历到最后一个点了
st[t] = false;
for(int i = h[t]; ~ i; i = ne[i]){
int j = e[i];
if(dist[j] > dist[t] + w[i]){
dist[j] = dist[t] + w[i];
if(!st[j]){
q[tt ++ ] = j;
st[j] = true;
if(j == N) tt = 0;
}
}
}
}
}
int main()
{
cin>>T>>C>>S>>E;
memset(h, -1, sizeof h);
for(int i = 0; i < C; i ++ ){
int a, b, c;
cin>>a>>b>>c;
add(a, b, c);
add(b, a, c);
}
spfa();
cout<<dist[E]<<endl;
return 0;
}
边栏推荐
- In pytorch function__ call__ And forward functions
- 2022.7.1-----leetcode. two hundred and forty-one
- Web2.0的巨头纷纷布局VC,Tiger DAO VC或成抵达Web3捷径
- 数据降维——主成分分析
- What is the MySQL backup suffix_ MySQL backup restore
- juypter notebook 修改默认打开文件夹以及默认浏览器
- MySQL
- 仿京东放大镜效果(pink老师版)
- Codeworks 5 questions per day (1700 average) - day 4
- 性能测试如何创造业务价值
猜你喜欢

Novice must see, click two buttons to switch to different content

Usage of ieda refactor

搭建主从模式集群redis

Data dimensionality reduction principal component analysis

程序猿入门攻略(十二)——数据的存储

云呐|为什么要用固定资产管理系统,怎么启用固定资产管理系统
![[0701] [paper reading] allowing data imbalance issue with perforated input during influence](/img/c7/9b7dc4b4bda4ecfe07aec1367fe059.png)
[0701] [paper reading] allowing data imbalance issue with perforated input during influence

High frequency interview questions

zabbix5客户端安装和配置

Tutorial (5.0) 09 Restful API * fortiedr * Fortinet network security expert NSE 5
随机推荐
Data dimensionality reduction factor analysis
Golang:[]byte to string
Binary operation
juypter notebook 修改默认打开文件夹以及默认浏览器
Page title component
Microservice technology - distributed global ID in high concurrency
Usage of ieda refactor
LeetCode 0871.最低加油次数 - 类似于POJ2431丛林探险
搭建主从模式集群redis
A4988驱动步进电机「建议收藏」
冒泡排序数组
安装单机redis详细教程
Windows2008R2 安装 PHP7.4.30 必须 LocalSystem 启动应用程序池 不然500错误 FastCGI 进程意外退出
metric_ Logger urination
QT中的QPropertyAnimation使用和toast案列
新手必看,點擊兩個按鈕切換至不同的內容
机器学习笔记 - 时间序列预测研究:法国香槟的月销量
Gamefi chain game system development (NFT chain game development function) NFT chain game system development (gamefi chain game development source code)
golang:[]byte转string
Why should we build an enterprise fixed asset management system and how can enterprises strengthen fixed asset management