http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808

题意:……

思路:和之前的天梯赛的一题一样,但是简单点。

没办法直接用点去算。把边看成点去做,规定dis[i]为走完第i条边之后即达到edge[i].v这个点的时候需要的花费。

点数为2*m。如果用普通的Dijkstra和SPFA会超时,所以用优先队列优化的Dijkstra。

 #include <bits/stdc++.h>
using namespace std;
#define N 100010
typedef long long LL;
const LL INF = 1000000000000000000LL;
struct Edge {
int u, v, nxt, w, c;
} edge[N*];
struct Node {
LL d; int id;
bool operator < (const Node &rhs) const {
return d > rhs.d;
}
};
int head[N], tot, n, m;
bool vis[N*];
LL dis[N*]; void Add(int u, int v, int w, int id) {
edge[tot] = (Edge) { u, v, head[u], w, id}; head[u] = tot++;
edge[tot] = (Edge) { v, u, head[v], w, id}; head[v] = tot++;
} LL Dijkstra() {
for(int i = ; i < tot; i++) dis[i] = INF;
memset(vis, , sizeof(vis));
LL ans = INF;
priority_queue<Node> que;
while(!que.empty()) que.pop(); for(int i = head[]; ~i; i = edge[i].nxt)
que.push((Node) {edge[i].w, i}), dis[i] = edge[i].w;
while(!que.empty()) {
Node now = que.top(); que.pop();
int pree = now.id; LL pred = now.d;
if(vis[pree]) continue; vis[pree] = ;
int u = edge[pree].v;
if(u == n && ans > pred) ans = pred;
for(int i = head[u]; ~i; i = edge[i].nxt) {
int nowe = i;
LL nowd = dis[pree] + edge[nowe].w + abs(edge[nowe].c - edge[pree].c);
if(nowd < dis[nowe] && !vis[nowe]) {
dis[nowe] = nowd;
que.push((Node) { nowd, nowe });
}
}
}
return ans;
} int main() {
while(~scanf("%d%d", &n, &m)) {
memset(head, -, sizeof(head)); tot = ;
for(int i = ; i <= m; i++) {
int u, v, c, w;
scanf("%d%d%d%d", &u, &v, &c, &w);
Add(u, v, w, c);
}
printf("%lld\n", Dijkstra());
}
return ;
}
/*
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
*/

CSU 1808:地铁(Dijkstra)的更多相关文章

  1. CSU 1808: 地铁 最短路

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 1808: 地铁 Time Limit: 5 SecMemory Limit: ...

  2. CSU 1808 地铁 (Dijkstra)

    Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci ...

  3. CSU 1808 - 地铁 - [最短路变形]

    题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 Time limit: 5000 ms Memory limit: 13107 ...

  4. CSU 1808 地铁(最短路变形)

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题意: Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站, ...

  5. 【最短路】【STL】CSU 1808 地铁 (2016湖南省第十二届大学生计算机程序设计竞赛)

    题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci ...

  6. CSU 1808 地铁

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

  7. CSU1808 地铁 —— dijkstra变形

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

  8. 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛

    传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...

  9. CSUOJ 1808 地铁

    Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci ...

随机推荐

  1. Ubuntu 官方推荐源列表

    如何使用Ubuntu Night Ubuntu Night(  http://ubuntu9.com ) 的Top mirror功能根据当前的网络情况和源健康状况不断地进行更新当前可用的源的信息,包括 ...

  2. Install Oracle 12c R2 on CentOS 7 silently

    准备工作 VMware 虚拟机 CentOS 7 17.08 系统安装包镜像 Oracle 12c R2 软件安装包 配置 yum 库并安装如下包 binutils-2.23.52.0.1-12.el ...

  3. sql分组统计多列值

    select BQDM,sum(case when HFBZ='0' then 1 ELSE 0 end) bxschf,sum(case when HFBZ='1' then 1 ELSE 0 en ...

  4. JS如何为iframe添加onclick事件

    如果页面上有iframe时,鼠标点击在iframe内时,包含iframe的document是不响应任何事件的, 例如: $("#iframe1").click(function() ...

  5. WPF 用代码调用dynamic resource动态更改背景 - CSDN博客

    原文:WPF 用代码调用dynamic resource动态更改背景 - CSDN博客 一般dynamic resoource通常在XAML里调用,如下范例: <Button Click=&qu ...

  6. 用友u8各版本在输出的时候报错提示:外部数据库驱动程序(1)中的意外错误

    从10月12日起很多U8用户反馈,在各版本U8中输出报表时软件报错,报错内容“外部数据库驱动程序(1)中的意外错误”,经初步分析有以下解决方案:1.卸载微软的补丁:(1)如果是PC操作系统(一般是客户 ...

  7. 【Python】wifi开关测试

    #!/usr/bin/python # -*- coding: UTF-8 -*- import os import time def find_device(): os.system('adb ki ...

  8. HTTP的请求方法一共有9种,有OPTIONS, HEAD, GET, POST等等(消息头有图,十分清楚)

    请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源]     GET方法用来请求已被URI识别的资源.指定的资源经服务器端解析后 ...

  9. VS2013环境里安装QT插件-“X86”与目标计算机类型“x64”冲突

    在VS2013环境里搭载QT老是出现模块计算机类型“X86”与目标计算机类型“x64”冲突 2.解决方案2.1 项目右键,属性>配置管理选择>x64,没有的话新建:2.2  项目右键,属性 ...

  10. C# 委托参数方法实例

    /// <summary> /// 方法委托 /// </summary> /// <param name="dwEnrollNum">< ...