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

3 3
1 2 1 1
2 3 2 1
1 3 1 1
3 3
1 2 1 1
2 3 2 1
1 3 1 10
3 2
1 2 1 1
2 3 1 1

Sample Output

1
3
2

题解:

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

   换线相当于换层

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

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

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

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

   感谢quailty的题解和代码

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
typedef long long LL;
const LL INF = (1LL<<)-;
const double Pi = acos(-1.0);
const int N = 1e5+, M = 1e6+, mod = 1e9+, inf = (<<)-; LL dist[N<<];
int vis[N<<];
struct Edge
{
int a,b,t;
Edge(int _a=,int _b=,int _t=) : a(_a), b(_b), t(_t) {}
};
struct qnode {
LL v,c;
qnode(LL _v=,LL _c=) : v(_v), c(_c) {}
bool operator < (const qnode &r) const
{
return c > r.c;
}
};
vector< Edge > tmp[N<<];
vector< pair<int,int > > G[N<<];
vector<pair<int ,int > > p[N<<];
void Add(int u,int v,int w) {
G[u].push_back(make_pair(v,w));
G[v].push_back(make_pair(u,w));
}
void init() {
for(int i = ; i < N; ++i) p[i].clear(), tmp[i].clear();
for(int i = ; i < * N; ++i) G[i].clear();
}
void Dij(int n,int u) {
for(int i = ; i <= n; ++i) dist[i] = INF, vis[i] = ;
priority_queue<qnode> q;
for(int i = ; i < p[u].size(); ++i) {
dist[p[u][i].first] = ;
q.push(qnode{p[u][i].first,});
}
while(!q.empty()) {
int k = q.top().v;
q.pop();
if(vis[k]) continue;
vis[k] = ;
for(int i = ; i < G[k].size(); ++i)
{
int v = G[k][i].first;
int c = G[k][i].second;
if(!vis[v] && dist[v] > dist[k] + c) {
dist[v] = dist[k] + c;
q.push(qnode{v,dist[v]});
}
}
}
}
int n,m;
int main() {
while(scanf("%d%d",&n,&m)!=EOF) {
init();
for(int i = ; i <= m; ++i) {
int a,b,c,t;
scanf("%d%d%d%d",&a,&b,&c,&t);
tmp[c].push_back(Edge(a,b,t));
}
int tot = ;
for(int i = ; i < N; ++i) {
for(int j = ; j < tmp[i].size(); ++j) {
int v[] = {tmp[i][j].a,tmp[i][j].b}, w = tmp[i][j].t;
for(int _=;_<;++_){
int u = v[_];
if(p[u].empty() || p[u].back().second < i) {
p[u].push_back(make_pair(++tot,i));
int s = p[u].size();
if(s > ) Add(p[u][s-].first,p[u][s-].first,p[u][s-].second-p[u][s-].second);
}
}
Add(p[v[]].back().first,p[v[]].back().first,w);
}
}
Dij(tot,);
LL ans = INF;
for(int i = ; i < p[n].size(); ++i)
ans = min( ans, dist[p[n][i].first]);
printf("%lld\n",ans);
}
return ;
}

湖南省第十二届大学生计算机程序设计竞赛 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. Ubuntu下安装eclipse

    一.eclipse安装环境JDK的安装 1.下载JDK 从官网下载jdk8 jdk-8u5-linux-x64.tar.gz 2.解压$ tar -zxvf jdk-8u5-linux-x64.tar ...

  2. oracle用户创建及权限设置及表空间

    建立表空间: create tablespace portx_data datafile 'D:\oracle_data\portx.dbf' size 50m autoextend on next ...

  3. FFmpeg frei0r water 滤镜

    FFmpeg frei0r water 滤镜, 在 linux 环境中很流畅,但在 XP 环境中抛出异常 研究一段时间修改了代码,能在 XP 里跑得动. sample.water.avi water. ...

  4. nyoj202_红黑树_中序遍历

    红黑树 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 什么是红黑树呢?顾名思义,跟枣树类似,红黑树是一种叶子是黑色果子是红色的树... 当然,这个是我说的... & ...

  5. SuSE Linux 开启VNC服务

    一.启动VNC服务输入命令 vncserver  二.编辑启动脚步vi /root/.vnc/xstartup 把twm &注释改为#twm & 然后再最下面增加2行startgnom ...

  6. unbutu下搭建FTP服务

    安装 apt-get install vsftpd 启动 service vsftpd start 第一次连接的时候出了点问题,报了一个 login incorrect 530的连接错误 然后百度了一 ...

  7. 【python】SQLAlchemy

    来源:廖雪峰 对比:[python]在python中调用mysql 注意连接数据库方式和数据操作方式! 今天发现了个处理数据库的好东西:SQLAlchemy 一般python处理mysql之类的数据库 ...

  8. 【网络】VPN和代理服务器的区别

    来自:http://www.zhihujingxuan.com/19311.html [scotttony的回答(41票)]: VPN和ssh哪个比较好, 要看你怎么定义是“好”. ssh作为一个创建 ...

  9. 后台弹出JS类

    using System; using System.Collections.Generic; using System.Text; using System.Web; using System.We ...

  10. 【XLL API 函数】xlStack

    查看堆栈区还剩余多少空间 原型 Excel12(xlStack, LPXLOPER12 pxRes, 0); 参数 此函数没有带任何参数 属性值/返回值 返回堆栈区还剩余的字节数 备注 返回最新版本的 ...