当前位置:网站首页>最短路模板
最短路模板
2022-08-01 22:42:00 【秦小咩】
迪杰斯特拉基础版
不能只记住链式前向星优化过的模板,那只适用于边比较少,点比较多的情况。一旦我们边有重复,点很少可以n^2解决的时候,这种基础版就发挥了很好的作用。
但值得一提的是,基础版的易错点在于边的重复性,必须严格取最小值才行。
甚至起点也会连接起点(caodan)
# include<iostream>
# include<deque>
# include<vector>
# include<map>
using namespace std;
typedef long long int ll;
map<ll,ll>edge[10000+10];
bool book[10000+10];
ll dis[10000+10];
int main ()
{
int n,m,st;
cin>>n>>m>>st;
book[st]=1;
fill(dis,dis+10000+10,(1ll<<50));
for(int i=1; i<=m; i++)
{
ll x,y,z;
cin>>x>>y>>z;
if(edge[x].find(y)==edge[x].end())
edge[x][y]=z;
else
edge[x][y]=min(edge[x][y],z);
}
dis[st]=0;
for(auto it:edge[st])
{
dis[it.first]=min(dis[it.first],it.second);
}
ll inf=(1ll<<50);
ll minn=inf;
int pos;
for(int i=1; i<=n-1; i++)
{
minn=inf;
for(int i=1; i<=n; i++)
{
if(!book[i]&&dis[i]<minn)
{
minn=dis[i];
pos=i;
}
}
book[pos]=1;
for(auto it:edge[pos])
{
if(dis[it.first]>it.second+dis[pos])
{
dis[it.first]=it.second+dis[pos];
}
}
}
for(int i=1; i<=n; i++)
{
if(dis[i]<(1ll<<50))
cout<<dis[i]<<" ";
else
cout<<((1ll<<31)-1)<<" ";
}
return 0;
}
Bellman-Ford
针对边进行n-1次循环,可以负权。
# include<iostream>
# include<deque>
# include<vector>
# include<map>
# include<queue>
# include<cstring>
using namespace std;
typedef long long int ll;
ll dis[1000000*5+10];
ll inf=(1ll<<31);
typedef struct
{
int b,e;
ll w;
} xinxi;
xinxi s[100000*5+10];
ll pre[100000*5+10];
int main ()
{
int n,m;
cin>>n>>m;
int st;
cin>>st;
for(int i=1; i<=m; i++)
{
cin>>s[i].b>>s[i].e>>s[i].w;
}
for(int i=1; i<=n; i++)
{
dis[i]=inf;
}
dis[st]=0;
for(int k=1; k<=n-1; k++)
{
for(int i=1;i<=n;i++)
{
pre[i]=dis[i];
}
for(int i=1; i<=m; i++)
{
if(dis[s[i].e]>dis[s[i].b]+s[i].w)
{
dis[s[i].e]=dis[s[i].b]+s[i].w;
}
}
int flag=0;
for(int i=1;i<=n;i++)
{
if(pre[i]!=dis[i])
{
flag=1;
break;
}
}
if(!flag)
{
break;
}
}
for(int i=1; i<=n; i++)
{
if(dis[i]<inf)
cout<<dis[i]<<" ";
else
cout<<(1ll<<31)-1<<" ";
}
return 0;
}
SPFA和迪杰斯特拉不同的是,无需优先队列,每个点可以频繁入队,但不会出现队内有某点又进入某点的情况,可以处理负权
# include<iostream>
# include<deque>
# include<vector>
# include<map>
# include<queue>
# include<cstring>
using namespace std;
typedef long long int ll;
struct node
{
int id;
ll dis;
};
queue<node>q;
ll inf=(1ll<<50);
ll dis[10000+10];
int len;
int f[5*100000+10];
int nex[5*100000+10];
bool book[10000+10];
typedef struct
{
int b,e;
ll w;
}xinxi;
xinxi s[5*100000+10];
void add(int x,int y,int z)
{
s[len].b=x;
s[len].e=y;
s[len].w=z;
nex[len]=f[x];
f[x]=len;
len++;
}
int main ()
{
memset(f,-1,sizeof(f));
int n,m;
cin>>n>>m;
int st;
cin>>st;
fill(dis,dis+10000+10,inf);
dis[st]=0;
for(int i=1;i<=m;i++)
{
int x,y,z;
cin>>x>>y>>z;
add(x,y,z);
}
struct node now;
now.id=st;
now.dis=0;
book[st]=1;
q.push(now);
while(!q.empty())
{
struct node now=q.front();
book[now.id]=0;
q.pop();
int x=f[now.id];
while(x!=-1)
{
if(dis[s[x].e]>dis[s[x].b]+s[x].w)
{
dis[s[x].e]=dis[s[x].b]+s[x].w;
if(!book[s[x].e])
{
struct node tail;
tail.id=s[x].e;
tail.dis=dis[s[x].e];
q.push(tail);
book[s[x].e]=1;
}
}
x=nex[x];
}
}
for(int i=1;i<=n;i++)
{
if(dis[i]<inf)
cout<<dis[i]<<" ";
else
cout<<(1ll<<31)-1<<" ";
}
return 0;
}
边栏推荐
- excel remove all carriage return from a cell
- 联邦学习入门
- Postman batch test interface detailed tutorial
- From 0 to 100: Notes on the Development of Enrollment Registration Mini Programs
- 10年稳定性保障经验总结,故障复盘要回答哪三大关键问题?|TakinTalks大咖分享
- PAM Palindromic Automata
- (Translation) How the contrasting color of the button guides the user's actions
- 使用 Zokrates 在 BSV 上创建您的第一个 zkSNARK 证明
- The must-have "fishing artifact" for programmers is here!
- SQL Server(设计数据库--存储过程--触发器)
猜你喜欢

深度学习Course2第二周Optimization Algorithms习题整理

罗克韦尔AB PLC RSLogix5000中的比较指令使用方法介绍

y84.第四章 Prometheus大厂监控体系及实战 -- prometheus告警机制进阶(十五)

毫秒级!千万人脸库快速比对,上亿商品图片检索,背后的极速检索用了什么神器?

feel so stupid

【Verilog刷题篇】硬件工程师从0到入门1|基础语法入门

Wechat Gymnasium Appointment Mini Program Graduation Design Finished Work (4) Opening Report

Quarantine and downgrade

Analysis of the development trend of game metaverse

Delicious this year
随机推荐
xctf攻防世界 Web高手进阶区 webshell
深度学习Course2第一周Practical aspects of Deep Learning习题整理
2022年最新河北建筑八大员(机械员)模拟考试题库及答案
NgRx Selector 的 Memoization 特性学习笔记
Small application project works WeChat stadium booking applet graduation design of the finished product (1) the development profile
Getting Started Database Days4
User Experience | How to Measure User Experience?
力扣第 304 场周赛复盘
visual studio code multiple editing
小程序毕设作品之微信美食菜谱小程序毕业设计成品(6)开题答辩PPT
使用Jenkins做持续集成,这个知识点必须要掌握
[深入研究4G/5G/6G专题-48]: 5G Link Adaption链路自适应-4-下行链路自适应DLLA-PDCCH信道
论文解读(GSAT)《Interpretable and Generalizable Graph Learning via Stochastic Attention Mechanism》
excel edit a cell without double clicking
Lecture 3: Several common table field data types in MySQL database
小程序毕设作品之微信体育馆预约小程序毕业设计成品(1)开发概要
selenium无头,防检测
华为无线设备配置双链路冷备份(AP指定配置方式)
excel remove all carriage return from a cell
[Recommended books] The first self-driving technology book