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

hdu 2112 HDU Today (最短路)

Problem - 2112

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

代码如下:

#include <cstdio>
#include <map>
#include <cstring>
#include <algorithm>
#include <iostream>

using namespace std;

map<string, int> 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

相关文章:

  • 自动化测试工具QTP(QuickTest)10.0的安装
  • Windows的定时任务(Schedule Task)设置
  • 线程和进程区别
  • 在Silverlight中动态绑定页面报表(PageReport)的数据源
  • xsd
  • Apache Struts2 远程命令执行漏洞
  • perf 简介
  • linux 压缩解压缩命令
  • Qt4过渡至Qt5
  • 啊速度发说法
  • Tiny6410 LED字符设备驱动
  • java对文件的检索
  • ×××服务让用户看得见
  • Sencha Touch 2.1学习图表Chart概述
  • tail tailf 使用
  • 【JavaScript】通过闭包创建具有私有属性的实例对象
  • Angular js 常用指令ng-if、ng-class、ng-option、ng-value、ng-click是如何使用的?
  • Angular 响应式表单之下拉框
  • Apache Zeppelin在Apache Trafodion上的可视化
  • Django 博客开发教程 8 - 博客文章详情页
  • JS笔记四:作用域、变量(函数)提升
  • JS变量作用域
  • JS函数式编程 数组部分风格 ES6版
  • Lsb图片隐写
  • Netty+SpringBoot+FastDFS+Html5实现聊天App(六)
  • Python学习之路13-记分
  • 等保2.0 | 几维安全发布等保检测、等保加固专版 加速企业等保合规
  • 分享几个不错的工具
  • 高度不固定时垂直居中
  • 关于字符编码你应该知道的事情
  • 将回调地狱按在地上摩擦的Promise
  • 前端路由实现-history
  • 前端学习笔记之观察者模式
  • 如何学习JavaEE,项目又该如何做?
  • 如何在 Tornado 中实现 Middleware
  • 项目实战-Api的解决方案
  • 携程小程序初体验
  • 移动互联网+智能运营体系搭建=你家有金矿啊!
  • 1.Ext JS 建立web开发工程
  • 如何在招聘中考核.NET架构师
  • ​二进制运算符:(与运算)、|(或运算)、~(取反运算)、^(异或运算)、位移运算符​
  • #控制台大学课堂点名问题_课堂随机点名
  • (14)Hive调优——合并小文件
  • (Forward) Music Player: From UI Proposal to Code
  • (心得)获取一个数二进制序列中所有的偶数位和奇数位, 分别输出二进制序列。
  • (译) 理解 Elixir 中的宏 Macro, 第四部分:深入化
  • (转)jdk与jre的区别
  • .babyk勒索病毒解析:恶意更新如何威胁您的数据安全
  • .gitignore文件—git忽略文件
  • .NET Core 中的路径问题
  • .net oracle 连接超时_Mysql连接数据库异常汇总【必收藏】
  • .NET 的静态构造函数是否线程安全?答案是肯定的!
  • .one4-V-XXXXXXXX勒索病毒数据怎么处理|数据解密恢复
  • :“Failed to access IIS metabase”解决方法
  • @Builder用法