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 ...
随机推荐
- Hibernate_day03讲义_使用Hibernate完成多对多的关系映射并操作
- GOF---Java开发中的23种设计模式详解
表点 设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式 ...
- [转]Struts2多个文件上传
转载至:http://blog.csdn.net/hanxiaoshuang123/article/details/7342091 Struts2多个文件上传多个文件上传分为List集合和数组,下面我 ...
- grid网格的流动grid-auto-flow属性
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- php前端传过来的json数据丢失 (max_input_vars)
开发向我反馈,前端业务页面提交数据用js将要传输的数据用json dump后,发给服务器,服务器在loads后发现数据是不全的. 这个问题困扰开发人员和运维人员.首先调整php.ini文件的上传文件数 ...
- Install VMware Workstation as a Service
Under default conditions, VMware Workstation does not support the ability to run virtual machines as ...
- osgExp只能将3dmax中的动画导出为路径动画osg::AnimationPath,而不能导出osgAnimation::Animation。osg播放骨骼动画应该使用FBX格式
通过实际的模型测试,导出为.osg文本格式,搜索animation,只能搜索到AnimationPathCallback,而搜索不到osgAnimation相关类 在OSGExp1.5.0源代码中搜索 ...
- 计算直线与WGS84椭球的交点
/************************************************************************/ /*线段与WGS84椭球求交 x^2/a^2+y^ ...
- JavaWeb学习总结(十六)Cookie保存中文内容
Cookie的值保存中文内容,可以使用Java.net.URLDecoder进行解码. 示例: <%@page import="java.net.URLDecoder"%&g ...
- jQuery Easing 动画效果扩展--使用Easing插件,让你的动画更具美感。
jQuery Easing 是一款比较老的jQuery插件,在很多网站都有应用,尤其是在一些页面滚动.幻灯片切换等场景应用比较多.它非常小巧,且有多种动画方案供选择,使用简单,而且免费. 引入Eas ...