博客
关于我
HDU1595(最短路 + 枚举)
阅读量:228 次
发布时间:2019-02-28

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

?????????????1??n??????????????????????????????????????????????????????????????

????

????????1??n????????????????????????????????????????????????????????????

  • ????1??n??????
  • ?????????? - ??????????
  • ????

    ????????????????????

  • ??????????????????????????????????????????????

  • ?????????????????????????????????????????????????????????0?????????????

  • Dijkstra?????????????????????Dijkstra???????1??n??????????????????????????????????????????????????????????????

  • ?????????Dijkstra?????????????????????????????????????????????????????

  • ????????????????????????????????????????????????????????????????-1?

  • ????

    ?????????C++???

    #include 
    #include
    #include
    #include
    #include
    using namespace std;const int inf = 1 << 30;const int maxn = 1005;const int maxm = 1e6 + 1e4;struct edge { int to; int w; int h; int next;};struct Node { int k, s; Node(int a, int b) { k = a; s = b; } bool operator<(const Node a) const { return s < a.s; }};int dijkstra(int s, int t, int x) { int dist[s + 1]; fill(dist, dist + s + 1, inf); dist[s] = 0; priority_queue
    q; q.push(Node(s, 0)); int visited[s + 1] = {0}; while (!q.empty()) { Node p = q.top(); q.pop(); if (visited[p.k]) continue; visited[p.k] = 1; for (int i = head[p.k]; i != -1; i = e[i].next) { edge f = e[i]; if (f.h != -1 && f.h < x) continue; if (dist[f.to] > dist[p.k] + f.w) { dist[f.to] = dist[p.k] + f.w; q.push(Node(f.to, dist[f.to])); } } } return (dist[t] == inf) ? -1 : dist[t];}int main() { int t; scanf("%d", &t); while (t--) { int n, m; scanf("%d", &n); scanf("%d", &m); int height[n + 1]; for (int i = 1; i <= n; ++i) { scanf("%d", &height[i]); } edge e[maxm * 2]; int head[n + 1] = {-1}; int cnt = 0; for (int i = 0; i < m; ++i) { int x, y, z, c; scanf("%d", &x); scanf("%d", &y); scanf("%d", &z); scanf("%d", &c); addedge(x, y, z, c); addedge(y, x, z, c); } int l = 0, r = height[1] - height[n]; int max_height = -1; int max_length = 0; while (l <= r) { int mid = l + (r - l) / 2; int current_max_height = mid; int ans = dijkstra(1, n, current_max_height); if (ans != -1) { if (current_max_height > max_height) { max_height = current_max_height; max_length = ans; } l = mid + 1; } else { r = mid - 1; } } if (max_height == -1) { puts("cannot reach destination"); } else { puts("maximum height = " + to_string(max_height)); puts("length of shortest route = " + to_string(max_length)); } }}

    ????

  • ?????

    • edge ???????????????????????????????
    • Node ??????????????????????????????
  • Dijkstra???

    • ?????????Dijkstra???????????????????????
    • ?????????????????????????????
  • ?????

    • ???????????????????????????????
  • ?????

    • ????????????????????????????
  • ??

    ??????????????????1??n????????????????????Dijkstra?????????????????????????????????????????

    转载地址:http://thep.baihongyu.com/

    你可能感兴趣的文章
    NIFI从PostGresql中离线读取数据再导入到MySql中_带有数据分页获取功能_不带分页不能用_NIFI资料太少了---大数据之Nifi工作笔记0039
    查看>>
    NIFI同步MySql数据_到SqlServer_错误_驱动程序无法通过使用安全套接字层(SSL)加密与SQL Server_Navicat连接SqlServer---大数据之Nifi工作笔记0047
    查看>>
    Nifi同步过程中报错create_time字段找不到_实际目标表和源表中没有这个字段---大数据之Nifi工作笔记0066
    查看>>
    NIFI大数据进阶_FlowFile拓扑_对FlowFile内容和属性的修改删除添加_介绍和描述_以及实际操作---大数据之Nifi工作笔记0023
    查看>>
    NIFI大数据进阶_NIFI的模板和组的使用-介绍和实际操作_创建组_嵌套组_模板创建下载_导入---大数据之Nifi工作笔记0022
    查看>>
    NIFI大数据进阶_NIFI监控的强大功能介绍_处理器面板_进程组面板_summary监控_data_provenance事件源---大数据之Nifi工作笔记0025
    查看>>
    NIFI大数据进阶_内嵌ZK模式集群1_搭建过程说明---大数据之Nifi工作笔记0015
    查看>>
    NIFI大数据进阶_外部ZK模式集群1_实际操作搭建NIFI外部ZK模式集群---大数据之Nifi工作笔记0017
    查看>>
    NIFI大数据进阶_离线同步MySql数据到HDFS_01_实际操作---大数据之Nifi工作笔记0029
    查看>>
    NIFI大数据进阶_离线同步MySql数据到HDFS_02_实际操作_splitjson处理器_puthdfs处理器_querydatabasetable处理器---大数据之Nifi工作笔记0030
    查看>>
    NIFI大数据进阶_连接与关系_设置数据流负载均衡_设置背压_设置展现弯曲_介绍以及实际操作---大数据之Nifi工作笔记0027
    查看>>
    NIFI数据库同步_多表_特定表同时同步_实际操作_MySqlToMysql_可推广到其他数据库_Postgresql_Hbase_SqlServer等----大数据之Nifi工作笔记0053
    查看>>
    NIFI汉化_替换logo_二次开发_Idea编译NIFI最新源码_详细过程记录_全解析_Maven编译NIFI避坑指南001---大数据之Nifi工作笔记0068
    查看>>
    NIFI集群_内存溢出_CPU占用100%修复_GC overhead limit exceeded_NIFI: out of memory error ---大数据之Nifi工作笔记0017
    查看>>
    NIFI集群_队列Queue中数据无法清空_清除队列数据报错_无法删除queue_解决_集群中机器交替重启删除---大数据之Nifi工作笔记0061
    查看>>
    NIH发布包含10600张CT图像数据库 为AI算法测试铺路
    查看>>
    Nim教程【十二】
    查看>>
    Nim游戏
    查看>>
    NIO ByteBuffer实现原理
    查看>>
    Nio ByteBuffer组件读写指针切换原理与常用方法
    查看>>