CSU 1808 - 地铁 - [最短路变形]
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808
Time limit: 5000 ms Memory limit: 131072 kB
Bobo 居住在大城市 ICPCCamp。
Input
Output
对于每组数据,输出一个整数表示要求的值。
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
题解:
如果只记录到某个节点x的最短路长度d[x],并且记录对应于d[x],是坐哪号线来到节点x的,这样显然是错误的。
原因比如这样的样例:
3 3
1 2 1 2
1 2 3 3
2 3 3 5
可以看出,d[x]要扩展到d[x][c],即这题的状态有两个量决定:到了节点x,最后乘坐的是c号线;
那么,如果我们把节点x用若干条边Edge(u1→x)…Edge(uk→x)来代替,那么我们就相当于把d[x]要扩展到d[x][c]了;
所以我们可以直接把边当成点,对边做最短路。
(这题对边做最短路,如果用spfa的话会TLE,要使用堆优化dijkstra)
AC代码:
#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> Pair; const LL INF=1e18;
const int maxn=1e5+;
const int maxm=2e5+; //无向边拆成两条有向边 int n,m; struct Edge{
int u,v,c;
int next;
LL t;
};
Edge E[maxm];
int head[maxn],ne;
void init()
{
ne=;
memset(head,-,sizeof(head));
}
void addedge(int u,int v,int c,LL t)
{
E[ne].u=u, E[ne].v=v, E[ne].c=c, E[ne].t=t;
E[ne].next=head[u];
head[u]=ne++;
} LL ans;
LL d[maxm];
bool vis[maxm];
void dijkstra(int st)
{
priority_queue< Pair, vector<Pair>, greater<Pair> > Q; memset(vis,,sizeof(vis));
for(int i=;i<ne;i++) d[i]=INF;
ans=INF; for(int i=head[st];i!=-;i=E[i].next)
{
d[i]=E[i].t;
Q.push(Pair(d[i],i));
}
while(!Q.empty())
{
int x=Q.top().second; Q.pop(); if(vis[x]) continue;
vis[x]=;
if(E[x].v==n) ans=min(ans,d[x]); for(int y=head[E[x].v];y!=-;y=E[y].next)
{
if(vis[y]) continue;
if(d[y]>d[x]+E[y].t+abs(E[y].c-E[x].c))
{
d[y]=d[x]+E[y].t+abs(E[y].c-E[x].c);
Q.push(Pair(d[y],y));
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init();
for(int i=;i<=m;i++)
{
int u,v,c; LL t;
scanf("%d%d%d%lld",&u,&v,&c,&t);
addedge(u,v,c,t);
addedge(v,u,c,t);
} dijkstra();
printf("%lld\n",ans);
}
}
#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> Pair; const LL INF=1e18;
const int maxn=1e5+;
const int maxm=2e5+; //无向边拆成两条有向边 int n,m; struct Edge{
int u,v,c;
LL t;
};
vector<Edge> E;
vector<int> G[maxn];
void init(int l,int r)
{
E.clear();
for(int i=l;i<=r;i++) G[i].clear();
}
void addedge(int u,int v,int c,LL t)
{
E.push_back((Edge){u,v,c,t});
G[u].push_back(E.size()-);
} LL ans;
LL d[maxm];
bool vis[maxm];
void dijkstra(int st)
{
priority_queue< Pair, vector<Pair>, greater<Pair> > Q; memset(vis,,sizeof(vis));
for(int i=;i<E.size();i++) d[i]=INF;
ans=INF; for(int i=;i<G[st].size();i++)
{
int x=G[st][i];
d[x]=E[x].t;
Q.push(Pair(d[x],x));
}
while(!Q.empty())
{
int x=Q.top().second; Q.pop(); if(vis[x]) continue;
vis[x]=;
if(E[x].v==n) ans=min(ans,d[x]); for(int i=;i<G[E[x].v].size();i++)
{
int y=G[E[x].v][i];
if(vis[y]) continue;
if(d[y]>d[x]+E[y].t+abs(E[y].c-E[x].c))
{
d[y]=d[x]+E[y].t+abs(E[y].c-E[x].c);
Q.push(Pair(d[y],y));
}
}
}
}
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
init(,n);
for(int i=;i<=m;i++)
{
int u,v,c; LL t;
scanf("%d%d%d%lld",&u,&v,&c,&t);
addedge(u,v,c,t);
addedge(v,u,c,t);
} dijkstra();
printf("%lld\n",ans);
}
}
注:两份代码的区别是分别用链式前向星和vector邻接表存图。
CSU 1808 - 地铁 - [最短路变形]的更多相关文章
- CSU 1808: 地铁 最短路
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 1808: 地铁 Time Limit: 5 SecMemory Limit: ...
- CSU 1808 地铁(最短路变形)
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题意: Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站, ...
- 【最短路】【STL】CSU 1808 地铁 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci ...
- CSU 1808 地铁
题意: ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci 号线,位于站 ai,bi 之间,往返均需要花费 ti 分钟 ...
- CSU 1808 地铁 (Dijkstra)
Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci ...
- POJ-2253.Frogger.(求每条路径中最大值的最小值,最短路变形)
做到了这个题,感觉网上的博客是真的水,只有kuangbin大神一句话就点醒了我,所以我写这篇博客是为了让最短路的入门者尽快脱坑...... 本题思路:本题是最短路的变形,要求出最短路中的最大跳跃距离, ...
- POJ 3635 - Full Tank? - [最短路变形][手写二叉堆优化Dijkstra][配对堆优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 题意题解等均参考:POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]. 一些口胡: ...
- POJ 3635 - Full Tank? - [最短路变形][优先队列优化Dijkstra]
题目链接:http://poj.org/problem?id=3635 Description After going through the receipts from your car trip ...
- POJ-1797Heavy Transportation,最短路变形,用dijkstra稍加修改就可以了;
Heavy Transportation Time Limit: 3000MS Memory Limit: 30000K Description Background Hugo ...
随机推荐
- [Model] LeNet-5 by Keras
典型的卷积神经网络. 数据的预处理 Keras傻瓜式读取数据:自动下载,自动解压,自动加载. # X_train: array([[[[ 0., 0., 0., ..., 0., 0., 0.], [ ...
- c# 通过.net自带的chart控件绘制饼图pie chart
c# 通过.net自带的chart控件绘制饼图pie chart 需要实现的目标是: 1.将数据绑定到pie的后台数据中,自动生成饼图. 2.生成的饼图有详细文字的说明. 具体的实现步骤: > ...
- Binary Numbers
时空限制 Time Limit:1000ms Resident Memory Limit:1024KB Output Limit:1024B 题目内容 Given a positive integer ...
- css背景图等比例缩放,盒子随背景图等比例缩放
很多时候我们给网站了一个大banner,但是随着屏幕的变化,背景会变形,我们知道background-size可以实现背景图等比例缩放,但是,我们想让下面的盒子根据缩放后背景图的高度,也能自动向上挤. ...
- debug的一点总结
程序员常常需要和bug打交道,一般来说调试bug的时间要多于编写程序的时间. bug可以简单的分为两大类: 语法上的bug 逻辑上的bug 语法上的bug就是指编译器能够识别的,例如常见的缺少分号和括 ...
- 【GIS】Cesium1.49编译
1.npm install 2.npm install --save-dev gulp 3.gulp default 4.npm run build 5.npm start 遇到问题 1.gulp不好 ...
- Splash autoload() 方法
autoload() 方法可以设置每个页面访问时自动加载的对象,比如自动加载 JavaScript 代码,自动加载 Ajax 代码等等 注意此方法只负责加载 JavaScript/Ajax 代码,不执 ...
- 使用 Splash
Splash 简介与安装 Splash Lua 脚本 Splash 对象属性 Splash 对象方法 Splash API 调用 Splash 负载均衡
- 带有ZLIB_LIBRARY_DEBUG的FindZLIB.cmake文件
CMake自带的FindZLIB.cmake只有ZLIB_LIBRARY,而没有ZLIB_LIBRARY_DEBUG 将下面的代码保存成FindZLIB.cmake,替换掉D:\Program Fil ...
- Elasticseach的评分机制
lucene 的评分机制 elasticsearch是基于lucene的,所以他的评分机制也是基于lucene的.评分就是我们搜索的短语和索引中每篇文档的相关度打分. 如果没有干预评分算法的时候,每次 ...