csu1808
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的更多相关文章
- 【CSU1808】地铁
ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟(即从 ...
- CSU1808 地铁 —— dijkstra变形
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题解:由于中转线路需要花费一定的时间,所以一般的以顶点为研究对象的dijkst ...
- 2017年暑假ACM集训日志
20170710: hdu1074,hdu1087,hdu1114,hdu1159,hdu1160,hdu1171,hdu1176,hdu1010,hdu1203 20170711: hdu1231, ...
随机推荐
- 【Adaptive Boosting】林轩田机器学习技法
首先用一个形象的例子来说明AdaBoost的过程: 1. 每次产生一个弱的分类器,把本轮错的样本增加权重丢入下一轮 2. 下一轮对上一轮分错的样本再加重学习,获得另一个弱分类器 经过T轮之后,学得了T ...
- katalon系列二:selenium IDE的替代者——Katalon Recorder
Katalon Recorder是和selenium IDE一样的一个浏览器插件,可以录制web上的操作并回放,但我个人感觉Katalon Recorder更好用.大家可以直接在chrome商店下载安 ...
- Python学习-前台开发-ajax操作
概述 对于WEB应用程序:用户浏览器发送请求,服务器接收并处理请求,然后返回结果,往往返回就是字符串(HTML),浏览器将字符串(HTML)渲染并显示浏览器上. 1.传统的Web应用 一个简单操作需要 ...
- Python全栈工程师
ParisGabriel Python 入门基础 print(“hello world”)变量 : 存储信息的,日后被调用.修改操作常量: 固定不变的量,字母大写命名规则:1. 字母数 ...
- 设计模式之模板方法模式 templateMethod
代码实现 public abstract class BankTemplateMethod { //具体方法 public void takeNumber(){ System.out.println( ...
- tomcat启动后服务访问404
. 解决办法: 在tomcat文件中有个work文件夹.其中,tomcat属于admin用户,work属于 admin用户 ,启动服务由admin用户启动. 但是发现work文件下的目录权限属于 ...
- 原始套接字--arp相关
arp请求示例 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <un ...
- code forces Codeforces Round #487 (Div. 2) C
C. A Mist of Florescence time limit per test 1 second memory limit per test 256 megabytes input stan ...
- java爬虫--使用正则表达式获取网页中的email
package com.enation.newtest; import java.io.*; import java.util.regex.*; import java.net.*; public c ...
- 使用 redux 监听插件的使用
首先需要在chrome浏览器当中下载redux插件 接着在你的项目当中加上**window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX ...