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 ;
}

最新文章

  1. JSP基础语法
  2. android studio集成融云 SDK 后在部分机型启动对话时崩溃
  3. asp.net 把图片压缩成zip之后再进行下载
  4. VLAN是什么
  5. awesome awesomeness
  6. fmt:formatDate标签的输出格式
  7. canvas 下载
  8. PCI、PCIE配置空间的訪问(MCFG,Bus,Device,Funtion)
  9. ACM—循环小数转变成分数知识点_C++实现
  10. JS中的Replace只会替换第一处解决办法
  11. Day05_JAVAEE系列:Junit
  12. synchronized与Lock的区别
  13. k8s学习笔记之六:Pod控制器(kube-controller-manager)
  14. P3244 [HNOI2015]落忆枫音
  15. 【转】SQLServer汉字转全拼音函数
  16. excel的宏与VBA实践——建表语句
  17. 【转载】COM 组件设计与应用(七)——编译、注册、调用
  18. Python Post img
  19. ASP.NET MVC &amp; WebApi 中实现Cors来让Ajax可以跨域访问 (转载)
  20. Android系统信息获取

热门文章

  1. Redis源码解析:18Hiredis同步API和回复解析API代码解析
  2. 使用Python的requests库作接口测试——对HTTP动词的支持
  3. 珂朵莉树(ODT)笔记
  4. Web三大组件之控制器组件Servlet(转载)
  5. Java借助itext pdf生成固定格式pdf的模板工具类
  6. day36 03-Hibernate检索方式:排序、参数绑定、投影查询
  7. 2019阿里云开年Hi购季域名与商标分会场全攻略!
  8. 二维vector基本使用
  9. 微信小程序--轮播图,标题,盒子,tab栏的合成例子
  10. map.(parseInt)方法详解