当前位置: 首页 > news >正文

[最短路dijkstra],启动!!!

总时间复杂度为 O ( ( n + m ) log ⁡ m )

P4779 【模板】单源最短路径(标准版)

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e5+5;
int n,m,s;
vector<PII> v[N];
int dis[N];
bool vis[N];
void dijkstra(int s)
{priority_queue<PII,vector<PII>,greater<PII> > q;//for(int i=1;i<=n;i++) dis[i] = 1e9;q.push({0,s});//dis[s] = 0;while(q.size()){auto t = q.top();//q.pop();int now = t.se;//if(vis[now]==1) continue;//vis[now] = 1;//for(auto tt:v[now]){int spot = tt.fi,w = tt.se;if(dis[spot]>dis[now]+w){dis[spot] = dis[now]+w;q.push({dis[spot],spot});//}}}
}
int main()
{IOS;cin>>n>>m>>s;while(m--){int a,b,w;cin>>a>>b>>w;v[a].pb({b,w});} dijkstra(s);for(int i=1;i<=n;i++) cout<<dis[i]<<' ';return 0;
}

P3905 道路重建

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define PII pair<int,int >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e6+5;
int n,m,A,B;
int va[10005][10005];
vector<PII> v[N];
int dis[N];
bool vis[N];
void dijkstra(int s)
{priority_queue<PII,vector<PII>,greater<PII> > q;for(int i=1;i<=n;i++) dis[i] = 1e9;dis[s] = 0;q.push({0,s});while(q.size()){auto t = q.top();q.pop();int now = t.se;if(vis[now]==1) continue;vis[now] = 1;for(auto tt:v[now]){int spot = tt.fi,w = 0;if(va[now][spot]==1) w = tt.se;if(dis[spot]>dis[now]+w){dis[spot] = dis[now]+w;q.push({dis[spot],spot});}	}}
}
int main()
{IOS;cin>>n>>m;while(m--){int a,b,w;cin>>a>>b>>w;v[a].pb({b,w});v[b].pb({a,w});}	int d;cin>>d;while(d--){int a,b;cin>>a>>b;va[a][b] = 1;va[b][a] = 1;	}cin>>A>>B;dijkstra(A);cout<<dis[B];
}

P4554 小明的游戏

#include<bits/stdc++.h>
#define ll long long
#define fi first
#define se second
#define pb push_back
#define PII pair< int,pair<int,int> >
#define IOS ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
using namespace std;
const int N = 1e4+5;
int n,m,s,stx,sty,edx,edy;
int dis[N][N];
bool vis[N][N];
char va[N][N];
int dx[4] ={0,0,1,-1};
int dy[4] ={1,-1,0,0};
void distra()
{priority_queue<PII,vector<PII>,greater<PII> > q;//for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){dis[i][j] = 1e9;vis[i][j] = 0;}}q.push({0,{stx,sty}});//dis[stx][sty] = 0;while(q.size()){auto t = q.top();//q.pop();auto now = t.se;//int x = now.fi,y = now.se;if(vis[x][y]==1) continue;//vis[x][y] = 1;//for(int i=0;i<=3;i++){int xx = x+dx[i],yy = y+dy[i];if(xx>=1&&xx<=n&&yy>=1&&yy<=m){int w = 0;if(va[x][y]!=va[xx][yy]) w = 1;if(dis[xx][yy]>dis[x][y]+w){dis[xx][yy] = dis[x][y]+w;q.push({dis[xx][yy],{xx,yy}});//}}}}
}
int main()
{IOS;while(1)    {cin>>n>>m;if(n==0&&m==0) break;for(int i=1;i<=n;i++){for(int j=1;j<=m;j++){cin>>va[i][j];}}cin>>stx>>sty>>edx>>edy;stx += 1;sty += 1;edx += 1;edy += 1;distra();cout<<dis[edx][edy]<<"\n";}
}
http://www.lryc.cn/news/414597.html

相关文章:

  • Java企业微信服务商代开发获取AccessToken示例
  • How does age change how you learn?(2)年龄如何影响学习能力?(二)
  • 可验证随机函数 vrf 概述
  • 鸿蒙双向绑定组件:TextArea、TextInput、Search、Checkbox,文本输入组件,图案解锁组件PatternLock
  • JS 算法 - 计数器
  • JavaScript基础——JavaScript运算符
  • E23.【C语言】练习:不创建第三个变量实现两个整数的交换
  • 如何搭建一个web系统?
  • 三十种未授权访问漏洞复现 合集( 二 )
  • C语言学习笔记[29]:函数①
  • 使用Springboot + netty 打造聊天服务之Nacos集群问题记录
  • 全网唯一!R语言顶刊配色包TheBestColors
  • 链表题型思路错误总结
  • 算法学习day28
  • C语言基础题:迷宫寻路(C语言版)
  • 力扣-1两数之和2两数相加-2024/8/3
  • 简站WordPress主题 专业的WordPress建站服务商
  • Final Shell for Mac 虚拟机连接工具【简单易操作,轻松上手】【开发所需连接工具】
  • Oracle JDK:版本、支持与许可
  • 大模型学习笔记 - LLM 之RLHF人类对齐的简单总结
  • 【从零开始一步步学习VSOA开发】 概述
  • 小程序背景图片无法通过 WXSS 获取
  • CC++内存魔术:掌控无形资源
  • 算法--初阶
  • 通过Java实现插入排序(直接插入,希尔)与选择排序(直接选择,堆排)
  • 大型分布式B2B2C多用户商城7.0企业版源码分享【java语言、方便二次开发】
  • C++的结构体、联合体、枚举类型(一)
  • 搭建高可用OpenStack(Queen版)集群(一)之架构环境准备
  • 通过Stack Overflow线程栈溢出的问题实例,详解C++程序线程栈溢出的诸多细节
  • LeetCode刷题笔记 | 3 | 无重复字符的最长子串 | 双指针 | 滑动窗口 | 2025兴业银行秋招笔试题 | 哈希集合