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;
}

最新文章

  1. Windows 10 部署Enterprise Solution 5.5
  2. (转)Redis 的 5 个常见使用场景
  3. call,apply,bind
  4. MySql 存储过程及调用方法
  5. Scalaz(36)- Free :实践-Free In Action - 实用体验
  6. SpringHttpInvoker解析1-使用示例
  7. Shell 总结
  8. oprofile使用方法
  9. POJ3274-牛的属性-HASH-ACM
  10. SharePoint REST api
  11. Hibernate 一对多双向关联Demo
  12. request.getParameterValues与request.getParameter的差别
  13. linux上发布网站遇到的问题
  14. json处理三部曲之第一曲:利用json-lib-xxx.jar处理json
  15. python并发编程之多线程一
  16. Android文本框-android学习之旅(十七 )
  17. UI第三方
  18. B+树和B-树的区别
  19. openstack Q版部署-----环境搭建(1)
  20. Codeforces Round #244 (Div. 2) C. Checkposts (tarjan 强连通分量)

热门文章

  1. 【vim环境配置】在centos6.4上配置vim的一些零碎记录
  2. centos6安装openfire(mysql)
  3. Escape From The Earth 逃离地球
  4. android 在自定义的listview(有刷新加载项)列表中,数据过少时不能铺满整个屏幕时,header和footer同时显示问题
  5. linux configuration
  6. HttpRuntime.Cache再学习
  7. Asp.net WebApi添加帮助文档
  8. 【转】Itween 贝塞尔曲线(一)
  9. Spring2集成iBatis2
  10. [ARC068F] Solitaire [DP]