最短路-SPAF模板
2024-10-08 01:46:11
以hdu1874畅通工程续为例
#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
using namespace std;
const int maxn = ;
vector<pair<int, int> > E[maxn];
int d[maxn], inq[maxn];
int n, m,s,t; void SPFA(int s)
{
queue<int> Q;
Q.push(s); d[s] = , inq[s] = ;
while (!Q.empty())
{
int now = Q.front(); Q.pop();
inq[now] = ;
for (int i = ; i < E[now].size(); i++) {
int v = E[now][i].first;
if (d[v] > d[now] + E[now][i].second)
{
d[v] = d[now] + E[now][i].second;
if (inq[v] == ) continue;
inq[v] = ;
Q.push(v);
}
}
}
} int main()
{
while (scanf("%d%d",&n,&m)==)
{
for (int i = ; i < maxn; i++) {
E[i].clear(); inq[i] = , d[i] = 1e9;
}
for (int i = ; i < m; i++) {
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
E[x].push_back(make_pair(y, z));
E[y].push_back(make_pair(x, z));
}
scanf("%d%d", &s, &t);
SPFA(s);
if (d[t] == 1e9)
printf("-1\n");
else
printf("%d\n", d[t]);
}
return ;
}
最新文章
- JSP基础语法
- android studio集成融云 SDK 后在部分机型启动对话时崩溃
- asp.net 把图片压缩成zip之后再进行下载
- VLAN是什么
- awesome awesomeness
- fmt:formatDate标签的输出格式
- canvas 下载
- PCI、PCIE配置空间的訪问(MCFG,Bus,Device,Funtion)
- ACM—循环小数转变成分数知识点_C++实现
- JS中的Replace只会替换第一处解决办法
- Day05_JAVAEE系列:Junit
- synchronized与Lock的区别
- k8s学习笔记之六:Pod控制器(kube-controller-manager)
- P3244 [HNOI2015]落忆枫音
- 【转】SQLServer汉字转全拼音函数
- excel的宏与VBA实践——建表语句
- 【转载】COM 组件设计与应用(七)——编译、注册、调用
- Python Post img
- ASP.NET MVC &; WebApi 中实现Cors来让Ajax可以跨域访问 (转载)
- Android系统信息获取