博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu 2112 HDU Today (最短路)
阅读量:7078 次
发布时间:2019-06-28

本文共 1305 字,大约阅读时间需要 4 分钟。

  就一个普通的最短路,除了用floyd会超时外,要注意起点终点地名相同是一个trick。

代码如下:

#include 
#include
#include
#include
#include
using namespace std;map
id;const int INF = 11111111;const int N = 222;int mat[N][N];char s[44], t[44];int q[N << 3];void spfa(int s) { int qh, qt; qh = qt = 0; for (int i = 0; i < N; i++) if (mat[s][i] < INF) q[qt++] = i; while (qh < qt) { int cur = q[qh++]; for (int i = 0; i < N; i++) { if (mat[s][i] > mat[s][cur] + mat[cur][i]) { mat[s][i] = mat[s][cur] + mat[cur][i]; q[qt++] = i; } } }}int main() { int n; while (cin >> n && n >= 0) { for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) mat[i][j] = INF; mat[i][i] = 0;} id.clear(); int d; scanf("%s%s", s, t); if (!strcmp(s, t)) { for (int i = 0; i < n; i++) scanf("%s%s%d", s, t, &d); cout << '0' << endl; continue; } id[s] = id.size(); id[t] = id.size(); for (int i = 0; i < n; i++) { scanf("%s%s%d", s, t, &d); if (id.find(s) == id.end()) id[s] = id.size(); if (id.find(t) == id.end()) id[t] = id.size(); mat[id[s]][id[t]] = mat[id[t]][id[s]] = d; } spfa(1); if (mat[1][2] >= INF) puts("-1"); else printf("%d\n", mat[1][2]); } return 0;}
View Code

 

——written by Lyon

转载于:https://www.cnblogs.com/LyonLys/p/hdu_2112_Lyon.html

你可能感兴趣的文章
Centos 7&6分布式lamp平台
查看>>
Microsoft Windows Server 2016 Build 10.0.9926.0下载
查看>>
Configuring Oracle Data Integrator for Cloudera
查看>>
Varnish缓存部署方式及原理详解
查看>>
C语言字符串拷贝strcpy函数的陷阱分析
查看>>
KVM虚拟化开源高可用方案(四)sheepdog
查看>>
收集DC中失败的登录信息并邮件通知
查看>>
Ubuntu安装Docker引擎和支持HTTPS的docker-registry服务
查看>>
排错之网络映射缓存凭证记录导致备份计划任务失败
查看>>
OGG升级运行ggsic报Unable to find library 'libclntsh.so.11.1'
查看>>
Python任意字符集转换
查看>>
Shell 常用命令与工具
查看>>
Centos7系统安装docker18.03
查看>>
Linux下实现LVM
查看>>
telnet 22端口可以ssh不行
查看>>
OSPF LSA类型详解
查看>>
Powershell DSC 5.0 - Push 模式
查看>>
通过深度学习来识别人脸(deep learning)
查看>>
vSphere Data Protection 6.1.2部署与配置
查看>>
PfSense基于BSD的软件防火墙的安装、配置与应用
查看>>