csu1808

题意

n 个点间有 m 条地铁,每条地铁可能属于不同的线路,每条地铁有权值即通过时花费的时间,如果乘坐第 i 条地铁来到地铁站 s,再乘坐第 j 条地铁离开,需要花费额外的时间 \(|c[i] - c[j]|\) 即地铁线路之差。

分析

点本身不具有线路信息,如果直接对点做最短路,无法判断要更新的点是来自于哪个线路。

而边具有唯一的线路信息,可以直接把边当成点,使用链式前向星来构造图,对边做最短路。

code

#include<cstdio>
#include<cmath>
#include<cstring>
#include<iostream>
#include<vector>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
typedef pair<ll, int> P;
const ll INF = 1e15;
const int MAXN = 2e5 + 10;
int head[MAXN];
int cnt;
struct Edge {
int next, to, stp;
ll w;
}edge[MAXN];
void add(int u, int v, int stp, ll w) {
edge[cnt].w = w;
edge[cnt].to = v;
edge[cnt].stp = stp;
edge[cnt].next = head[u];
head[u] = cnt++;
}
int n, m;
vector<Edge> g[MAXN];
ll d[MAXN];
ll ans;
int vis[MAXN];
void dijkstra() {
ans = INF;
priority_queue<P, vector<P>, greater<P> > que;
memset(vis, 0, sizeof vis);
for(int i = 0; i <= cnt; i++) d[i] = INF;
for(int i = head[1]; ~i; i = edge[i].next) {
d[i] = edge[i].w;
que.push(P(edge[i].w, i));
}
while(!que.empty()) {
P p = que.top();
que.pop();
int u = p.second;
vis[u] = 1;
if(edge[u].to == n) {
ans = min(ans, d[u]);
}
for(int i = head[edge[u].to]; ~i; i = edge[i].next) {
if(!vis[i] && d[i] > d[u] + edge[i].w + abs(edge[i].stp - edge[u].stp)) {
d[i] = d[u] + edge[i].w + abs(edge[i].stp - edge[u].stp);
que.push(P(d[i], i));
}
}
}
}
int main() {
while(~scanf("%d%d", &n, &m)) {
memset(head, -1, sizeof head);
cnt = 0;
for(int i = 0; i < m; i++) {
int x, y, z;
ll k;
scanf("%d%d%d%lld", &x, &y, &z, &k);
add(x, y, z, k);
add(y, x, z, k);
}
dijkstra();
printf("%lld\n", ans);
}
return 0;
}

csu1808的更多相关文章

  1. 【CSU1808】地铁

    ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟(即从 ...

  2. CSU1808 地铁 —— dijkstra变形

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题解:由于中转线路需要花费一定的时间,所以一般的以顶点为研究对象的dijkst ...

  3. 2017年暑假ACM集训日志

    20170710: hdu1074,hdu1087,hdu1114,hdu1159,hdu1160,hdu1171,hdu1176,hdu1010,hdu1203 20170711: hdu1231, ...

随机推荐

  1. Mybatis基本用法

    搭建mybatis环境 1, 导入需要的jar包 mybatis-*.*.*.jar ojdbc6.jar 2, 配置mybatis的总配置文件: mybatis-config.xml 配置根标签 & ...

  2. Python 3基础教程8--if else、if elif else

    本文介绍if else语句,不多说,直接看例子. if elif else语句

  3. mac虚拟机上(centos系统)怎样实现共享本机文件

    首先加载vboxadditions,可以从https://download.virtualbox.org/virtualbox/下载,记得一定要跟virtualBox版本对应 然后打开virtualb ...

  4. ironic images

    参考: http://blog.csdn.net/wanghuiict/article/details/52757359 ironic 整个部署流程中有两组映像,分别是 deploy 映像和 user ...

  5. 团队冲刺Alpha(九)

    目录 组员情况 组员1(组长):胡绪佩 组员2:胡青元 组员3:庄卉 组员4:家灿 组员5:凯琳 组员6:翟丹丹 组员7:何家伟 组员8:政演 组员9:黄鸿杰 组员10:刘一好 组员11:何宇恒 展示 ...

  6. nyoj 题目36 最长公共子序列

    最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 咱们就不拐弯抹角了,如题,需要你做的就是写一个程序,得出最长公共子序列.tip:最长公共子序列也称作最 ...

  7. 【bzoj5017】[Snoi2017]炸弹 线段树优化建图+Tarjan+拓扑排序

    题目描述 在一条直线上有 N 个炸弹,每个炸弹的坐标是 Xi,爆炸半径是 Ri,当一个炸弹爆炸时,如果另一个炸弹所在位置 Xj 满足:  Xi−Ri≤Xj≤Xi+Ri,那么,该炸弹也会被引爆.  现在 ...

  8. Sublime Text 2 HTML代码缩进 美化HTML代码

    关于代码格式的美化,之前在win下一直用“alignment”这个插件,它能实现一键对齐和缩进.最近使用mac版的sublime text 2,不知道是什么原因,这个插件疑似失效…… 这对有洁癖的完美 ...

  9. NOIP2017赛前考试注意事项总结

     考前: 考试前把读入优化和库以及对拍文件打好做好准备工作,另外注意放松心态,太紧张了肯定考不好··将自己的注意力集中起来  考场策略: 考试的基本策略是对每于道题先想个20分钟,如果想不出个靠谱的方 ...

  10. libcurl网络连接使用tcp/ip

    CURL *curl; CURLcode res; const char *request = "GETas.xxxxE测试发送"; curl_socket_t sockfd; / ...