1808: 地铁

Description

Bobo 居住在大城市 ICPCCamp。

ICPCCamp 有 n 个地铁站,用 1,2,…,n 编号。 m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟(即从 ai 到 bi 需要 ti 分钟,从 bi 到 ai 也需要 ti 分钟)。
众所周知,换乘线路很麻烦。如果乘坐第 i 段地铁来到地铁站 s,又乘坐第 j 段地铁离开地铁站 s,那么需要额外花费 |ci-cj | 分钟。注意,换乘只能在地铁站内进行。
Bobo 想知道从地铁站 1 到地铁站 n 所需要花费的最小时间。
 

Input

输入包含不超过 20 组数据。
每组数据的第一行包含两个整数 n,m (2≤n≤105,1≤m≤105).
接下来 m 行的第 i 行包含四个整数 ai,bi,ci,ti (1≤ai,bi,ci≤n,1≤ti≤109).
保证存在从地铁站 1 到 n 的地铁线路(不一定直达)。
 

Output

对于每组数据,输出一个整数表示要求的值。

Sample Input

  1. 3 3
  2. 1 2 1 1
  3. 2 3 2 1
  4. 1 3 1 1
  5. 3 3
  6. 1 2 1 1
  7. 2 3 2 1
  8. 1 3 1 10
  9. 3 2
  10. 1 2 1 1
  11. 2 3 1 1

Sample Output

  1. 1
  2. 3
  3. 2

题解:

    现在假设i号线在深度i的世界里跑

   换线相当于换层

    然后换乘站相当于一个垂直通道,拆出一些点放到对应的层里

    然后按照深度从小到大连……

   你从1楼上到3楼就和你从1楼上到2楼,再从2楼上到3楼是一样的

   连边侯跑一发最短路就好了 ,spfa超时

   感谢quailty的题解和代码

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cmath>
  5. #include<iostream>
  6. #include<algorithm>
  7. #include<queue>
  8. using namespace std;
  9. #pragma comment(linker, "/STACK:102400000,102400000")
  10. #define ls i<<1
  11. #define rs ls | 1
  12. #define mid ((ll+rr)>>1)
  13. typedef long long LL;
  14. const LL INF = (1LL<<)-;
  15. const double Pi = acos(-1.0);
  16. const int N = 1e5+, M = 1e6+, mod = 1e9+, inf = (<<)-;
  17.  
  18. LL dist[N<<];
  19. int vis[N<<];
  20. struct Edge
  21. {
  22. int a,b,t;
  23. Edge(int _a=,int _b=,int _t=) : a(_a), b(_b), t(_t) {}
  24. };
  25. struct qnode {
  26. LL v,c;
  27. qnode(LL _v=,LL _c=) : v(_v), c(_c) {}
  28. bool operator < (const qnode &r) const
  29. {
  30. return c > r.c;
  31. }
  32. };
  33. vector< Edge > tmp[N<<];
  34. vector< pair<int,int > > G[N<<];
  35. vector<pair<int ,int > > p[N<<];
  36. void Add(int u,int v,int w) {
  37. G[u].push_back(make_pair(v,w));
  38. G[v].push_back(make_pair(u,w));
  39. }
  40. void init() {
  41. for(int i = ; i < N; ++i) p[i].clear(), tmp[i].clear();
  42. for(int i = ; i < * N; ++i) G[i].clear();
  43. }
  44. void Dij(int n,int u) {
  45. for(int i = ; i <= n; ++i) dist[i] = INF, vis[i] = ;
  46. priority_queue<qnode> q;
  47. for(int i = ; i < p[u].size(); ++i) {
  48. dist[p[u][i].first] = ;
  49. q.push(qnode{p[u][i].first,});
  50. }
  51. while(!q.empty()) {
  52. int k = q.top().v;
  53. q.pop();
  54. if(vis[k]) continue;
  55. vis[k] = ;
  56. for(int i = ; i < G[k].size(); ++i)
  57. {
  58. int v = G[k][i].first;
  59. int c = G[k][i].second;
  60. if(!vis[v] && dist[v] > dist[k] + c) {
  61. dist[v] = dist[k] + c;
  62. q.push(qnode{v,dist[v]});
  63. }
  64. }
  65. }
  66. }
  67. int n,m;
  68. int main() {
  69. while(scanf("%d%d",&n,&m)!=EOF) {
  70. init();
  71. for(int i = ; i <= m; ++i) {
  72. int a,b,c,t;
  73. scanf("%d%d%d%d",&a,&b,&c,&t);
  74. tmp[c].push_back(Edge(a,b,t));
  75. }
  76. int tot = ;
  77. for(int i = ; i < N; ++i) {
  78. for(int j = ; j < tmp[i].size(); ++j) {
  79. int v[] = {tmp[i][j].a,tmp[i][j].b}, w = tmp[i][j].t;
  80. for(int _=;_<;++_){
  81. int u = v[_];
  82. if(p[u].empty() || p[u].back().second < i) {
  83. p[u].push_back(make_pair(++tot,i));
  84. int s = p[u].size();
  85. if(s > ) Add(p[u][s-].first,p[u][s-].first,p[u][s-].second-p[u][s-].second);
  86. }
  87. }
  88. Add(p[v[]].back().first,p[v[]].back().first,w);
  89. }
  90. }
  91. Dij(tot,);
  92. LL ans = INF;
  93. for(int i = ; i < p[n].size(); ++i)
  94. ans = min( ans, dist[p[n][i].first]);
  95. printf("%lld\n",ans);
  96. }
  97. return ;
  98. }

湖南省第十二届大学生计算机程序设计竞赛 F 地铁 多源多汇最短路的更多相关文章

  1. 2016年湖南省第十二届大学生计算机程序设计竞赛---Parenthesis(线段树求区间最值)

    原题链接 http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1809 Description Bobo has a balanced parenthes ...

  2. 2016年湖南省第十二届大学生计算机程序设计竞赛Problem A 2016 找规律归类

    Problem A: 2016 Time Limit: 5 Sec  Memory Limit: 128 MB Description  给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) ...

  3. 湖南省第十二届大学生计算机程序设计竞赛 B 有向无环图 拓扑DP

    1804: 有向无环图 Time Limit: 5 Sec  Memory Limit: 128 MBSubmit: 187  Solved: 80[Submit][Status][Web Board ...

  4. 湖南省第十二届大学生计算机程序设计竞赛 G Parenthesis

    1809: Parenthesis Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n and q ...

  5. 湖南省第十二届大学生计算机程序设计竞赛 A 2016

    1803: 2016 Description  给出正整数 n 和 m,统计满足以下条件的正整数对 (a,b) 的数量:   1. 1≤a≤n,1≤b≤m; 2. a×b 是 2016 的倍数. In ...

  6. 【模拟】CSU 1807 最长上升子序列~ (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1807 题目大意: 给你一个长度为N(N<=105)的数列,数列中的0可以被其他数 ...

  7. 【最短路】【数学】CSU 1806 Toll (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1806 题目大意: N个点M条有向边,给一个时间T(2≤n≤10,1≤m≤n(n-1), ...

  8. 【树状数组】CSU 1811 Tree Intersection (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1811 题目大意: 一棵树,N(2<=N<=105)个节点,每个节点有一种颜 ...

  9. 【数学】CSU 1810 Reverse (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1810 题目大意: 一个长度为N的十进制数,R(i,j)表示将第i位到第j位翻转过来后的 ...

随机推荐

  1. 开源工作流 Bonita BPM (JAVA)

    Bonita BPM 开源工作流 Bonita BPM  (JAVA) http://www.bonitasoft.com/

  2. C# 数据库查询总结

    首先创建了一个SQL Server数据库作为测试的数据库,建立表并填入测试数据 数据库:SQL Server 数据库名:Blog 表名:Test 注:数据库的连接可以使用“dbl”文件测试,具体使用百 ...

  3. effective c++ resources

    http://www.cnblogs.com/littlethank/archive/2011/12/15/2288787.html http://www.cnblogs.com/liao-xiao- ...

  4. 【转】.so兼容32位和64位

    本文转自:http://blog.csdn.net/fwt336/article/details/51700300 安卓的兼容性是一个很令人头疼的问题,这几天又遇到了,还好还是解决了. 我遇到的问题是 ...

  5. Unity3D研究院之Prefab里面的Prefab关联问题(转)

    转自http://www.xuanyusong.com/archives/3042 最近在做UI部分中遇到了这样的问题,就是Prefab里面预制了Prefab.可是在Unity里面一旦Prefab预制 ...

  6. python迭代器与iter()函数实例教程

    python迭代器与iter()函数实例教程 发布时间:2014-07-16编辑:脚本学堂 本文介绍了python迭代器与iter()函数的用法,Python 的迭代无缝地支持序列对象,而且它还允许程 ...

  7. Delphi XE5 android 获取网络状态

    unit Androidapi.JNI.Network; interface function IsConnected: Boolean; function IsWiFiConnected: Bool ...

  8. perl 从文件里读出变量无法使用解决办法

    最近在写一个perl函数,把test case 放到配置文件里,读出来然后使用system运行. 我的本意是: 配置文件conf ping -c $count $ip #在主程序中定义$ip和$cou ...

  9. Java for LeetCode 209 Minimum Size Subarray Sum

    Given an array of n positive integers and a positive integer s, find the minimal length of a subarra ...

  10. SAP打印出库单需求

    *&---------------------------------------------------------------------* *& Report  Z_SD_CKD ...