题目链接:

http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808

1808: 地铁

Time Limit: 5 Sec
Memory Limit: 128 MB
#### 问题描述
> 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 所需要花费的最小时间。
#### 输入
> 输入包含不超过 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 的地铁线路(不一定直达)。
#### 输出
> 对于每组数据,输出一个整数表示要求的值。
#### 样例
> **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

题解

一个地铁站有多少条线路,就把它拆成多少个点,然后对地铁线路编号相邻的两个点建边,权值为编号差值的绝对值,(由于权值的计算方式比较特殊,我们没有必要连成完全图,只需要连一条链就可以了。)图建完后跑最短路径。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII; const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0); //start---------------------------------------------------------------------- const int maxn=4e5+10; int n,m,tot; struct Node{
int id,u,v,w;
}nds[maxn]; struct Edge{
int u,v,w;
Edge(int u,int v,int w):u(u),v(v),w(w){}
}; struct HeapNode{
LL d; int u;
HeapNode(LL d,int u):d(d),u(u){}
bool operator < (const HeapNode& rhs) const {
return d>rhs.d;
}
}; struct Dijkstra{
int n,m;
vector<Edge> egs;
vector<int> G[maxn];
bool done[maxn];
LL d[maxn]; void init(int n){
this->n=n;
rep(i,0,n) G[i].clear();
egs.clear();
} void addEdge(int u,int v,int d){
egs.pb(Edge(u,v,d));
m=egs.sz();
G[u].pb(m-1);
} void dijkstra(VI &lis){
priority_queue<HeapNode> Q;
rep(i,0,n) d[i]=INFL;
rep(i,0,lis.sz()){
int v=lis[i];
d[v]=0;
Q.push(HeapNode(0,v));
}
clr(done,0);
while(!Q.empty()){
HeapNode x=Q.top(); Q.pop();
int u=x.u;
if(done[u]) continue;
done[u]=true;
rep(i,0,G[u].sz()){
Edge& e=egs[G[u][i]];
if(d[e.v]>d[u]+e.w){
d[e.v]=d[u]+e.w;
Q.push(HeapNode(d[e.v],e.v));
}
}
}
} }dij; map<pair<int,int>,int> mp; VI G[maxn]; void init(){
mp.clear();
rep(i,0,n+10) G[i].clear();
tot=0;
} int main() {
while(scf("%d%d",&n,&m)==2){
init();
rep(i,0,m){
scf("%d%d%d%d",&nds[i].u,&nds[i].v,&nds[i].id,&nds[i].w);
G[nds[i].u].pb(nds[i].id);
G[nds[i].v].pb(nds[i].id);
}
for(int i=1;i<=n;i++){
sort(all(G[i]));
G[i].erase(unique(all(G[i])),G[i].end());
rep(j,0,G[i].sz()){
int v=G[i][j];
mp[mkp(i,v)]=tot++;
}
}
dij.init(tot); rep(i,0,m){
int u=mp[mkp(nds[i].u,nds[i].id)];
int v=mp[mkp(nds[i].v,nds[i].id)];
dij.addEdge(u,v,nds[i].w);
dij.addEdge(v,u,nds[i].w);
} for(int i=1;i<=n;i++){
rep(j,0,G[i].sz()-1){
int u=mp[mkp(i,G[i][j])];
int v=mp[mkp(i,G[i][j+1])];
int w=G[i][j+1]-G[i][j];
dij.addEdge(u,v,w);
dij.addEdge(v,u,w);
}
} VI lis;
rep(i,0,G[1].sz()){
int v=mp[mkp(1,G[1][i])];
lis.pb(v);
} dij.dijkstra(lis); LL ans=INFL;
rep(i,0,G[n].sz()){
int v=mp[mkp(n,G[n][i])];
ans=min(ans,dij.d[v]);
} prf("%lld\n",ans);
}
return 0;
} //end-----------------------------------------------------------------------

1808: 地铁

CSU 1808: 地铁 最短路的更多相关文章

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

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

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

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

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

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

  4. CSU 1808 地铁

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

  5. CSU 1808 地铁 (Dijkstra)

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

  6. CSU 1808:地铁(Dijkstra)

    http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题意:…… 思路:和之前的天梯赛的一题一样,但是简单点. 没办法直接用点去算.把边看成点 ...

  7. CSUOJ 1808 地铁

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

  8. 湖南省第十二届大学生计算机程序设计竞赛 F 地铁 多源多汇最短路

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

  9. 北京地铁出行线路规划系统项目总结(Java+Flask+Vue实现)

    北京地铁出行线路规划系统项目总结 GitHub仓库地址:https://github.com/KeadinZhou/SE-Subway Demo地址:http://10.66.2.161:8080/ ...

随机推荐

  1. PHP 查找二维数组中是否有指定字符串的字段

    Array ( ] => Array ( [content] => 您提交了订单,请等待系统确认 :: [operator] => 客户 ) ] => Array ( [con ...

  2. Python爬虫系列 - 初探:爬取旅游评论

    Python爬虫目前是基于requests包,下面是该包的文档,查一些资料还是比较方便. http://docs.python-requests.org/en/master/ POST发送内容格式 爬 ...

  3. 嵌入式Linux系统移植——uboot常用命令

    flash的一般分区: 其它数据 环境变量 可执行程序.如bootloader print(可缩写为:pri):打印查看uboot这个软件中集成的环境变量setenv.saveenv:设置.保存环境变 ...

  4. 原生js三级联动

    <!DOCTYPE html> <html lang="en"> <head> <title> 三级联动 </title> ...

  5. spark执行命令 监控执行命令

    #!/bin/bash #/usr/hdp/current/flume-server/bin/flume-ng agent -c conf/ -f /usr/hdp/current/flume-ser ...

  6. 【EXCEL】指定の項目の内容一覧を表示

    上記の図の左の部分みたいな長いデータがあって. その中からただほしいのは右側部分ように一部分のみほしい時. 普段はVLOOKUP使用フィルターをかけて探すなどの方法をしようしていますが. ここで.簡単 ...

  7. 20155231 实验二 Java面向对象程序设计

    20155231 java实验一 Java开发环境的熟悉 实验要求 没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程: 完成实验.撰写实验 ...

  8. 20155310《Java程序设计》实验五(网络编程与安全)实验报告

    20155310<Java程序设计>实验五(网络编程与安全)实验报告 一.实验内容及步骤 •任务一: 编写MyBC.java实现中缀表达式转后缀表达式的功能 编写MyDC.java实现从上 ...

  9. 20155336 2016-2017-2 《Java程序设计》第四周学习总结

    20155336 2016-2017-2 <Java程序设计>第四周学习总结 教材学习内容总结 第六章 继承:面向对象中,为避免多个类间重复定义共同行为.(简单说就是将相同的程序代码提升为 ...

  10. Struts2知识点总结

    2 Struts2 1 Struts2 简化Servlet编写 2 Struts2使用步骤 1 添加第三方包 2 在web.xml当中配置核心过滤器 <filter> <filter ...