CSU 1808 地铁
题意:
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 所需要花费的最小时间。
分析:
很明显的最短路,但是有两个做法.
一是按每个站点有几个地铁经过然后拆为多个点。然后两次添加边,第一次是添加不同站点之间的花费,第二次是是相同站点不同地铁线路的话费。
二是把边当做状态dist[i]表示走过i这条边打到edge[i].v的最少花费,这样的话就2∗m条边
spfa会TLE,所以要用dijkstra+heap
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
方法一:
- #include<iostream>
- #include<cstring>
- #include<cstdio>
- #include<vector>
- #include<map>
- #include<queue>
- #include<algorithm>
- using namespace std;
- const int maxn =;
- const int INF = 0x3f3f3f3f;
- int n,m;
- struct Edge{
- int to,next;
- int w;
- }edge[maxn*];
- int head[maxn],tot;
- void init(){
- memset(head,-,sizeof(head));
- tot=;
- }
- void addedge(int u,int v,int w){
- edge[tot].to=v;
- edge[tot].next = head[u];
- edge[tot].w =w;
- head[u]=tot++;
- }
- vector<int>num[maxn];//存贮第i个站点跟哪几个地铁相连
- map<int,int>mp[maxn];//存贮第i个站点跟几个地铁相连
- int dis[maxn];
- int cnt;
- struct node{
- int now;
- int c;
- node(int _now = ,int _c=):now(_now),c(_c){}
- bool operator <(const node &r)const
- {
- return c>r.c;
- }
- };
- void DJ(){
- priority_queue<node> que;
- while(!que.empty()) que.pop();
- for(int i=;i<cnt;++i) dis[i]=INF;
- for(int i=;i<num[].size();++i){
- int st;
- st = mp[][num[][i]];
- dis[st]=;
- que.push(node(st,));
- }
- node temp;
- while(!que.empty()){
- temp = que.top();
- que.pop();
- int u = temp.now;
- int cost = temp.c;
- if(cost>dis[u])
- continue;
- for(int i=head[u];~i;i=edge[i].next){
- int v = edge[i].to;
- int w = edge[i].w;
- if(dis[v]>cost+w){
- dis[v]= cost + w;
- que.push(node(v,dis[v]));
- }
- }
- }
- }
- int main(){
- int u,v,w,x;
- while(scanf("%d%d",&n,&m)!=EOF){
- init();
- cnt=;
- for(int i=;i<=n;i++){
- num[i].clear();
- mp[i].clear();
- }
- for(int i=;i<m;++i){
- scanf("%d%d%d%d",&u,&v,&x,&w);
- if(!mp[u][x]){
- mp[u][x]=cnt++;
- num[u].push_back(x);
- }
- u=mp[u][x];
- if(!mp[v][x]){
- mp[v][x]=cnt++;
- num[v].push_back(x);
- }
- v=mp[v][x];
- addedge(u,v,w);
- addedge(v,u,w);
- }
- for(int i=;i<=n;i++){
- sort(num[i].begin(),num[i].end());
- for(int j=;j<num[i].size()-;++j){
- u=mp[i][num[i][j]];
- v=mp[i][num[i][j+]];
- w=num[i][j+]-num[i][j]; //同一站点不同线路的拆点之间的差值
- addedge(u,v,w);
- addedge(v,u,w);
- }
- }
- DJ();
- int ans=INF;
- for(int i=;i<num[n].size();i++){
- u=mp[n][num[n][i]];
- ans=min(ans,dis[u]);
- }
- printf("%d\n",ans);
- }
- return ;
- }
方法二:
- #include <map>
- #include <set>
- #include <stack>
- #include <queue>
- #include <cmath>
- #include <string>
- #include <vector>
- #include <cstdio>
- #include <cctype>
- #include <cstring>
- #include <sstream>
- #include <cstdlib>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- #define MAX 100005
- #define MAXN 1000005
- #define LL long long
- inline void RI(int &x) {
- char c;
- while((c=getchar())<'' || c>'');
- x=c-'';
- while((c=getchar())>='' && c<='') x=(x<<)+(x<<)+c-'';
- }
- struct Edge{
- int v,next,num,c;
- }edge[MAX*];
- struct Node{
- int id,val;
- bool operator<(const Node &a)const{
- return val>a.val;
- }
- }x;
- int head[MAX];
- LL dis[MAX*];
- int vis[MAX*];
- int tot;
- void add_edge(int a,int b,int c,int d){
- edge[tot]=(Edge){b,head[a],c,d};
- head[a]=tot++;
- edge[tot]=(Edge){a,head[b],c,d};
- head[b]=tot++;
- }
- LL dijkstra(int s,int t){
- priority_queue<Node> q;
- for(int i=;i<tot;i++){
- dis[i]=1e18;
- vis[i]=;
- }
- for(int i=head[s];i!=-;i=edge[i].next){
- x=(Node){i,edge[i].c};
- dis[i]=edge[i].c;
- q.push(x);
- }
- LL ans=1e18;
- while(!q.empty()){
- x=q.top();
- q.pop();
- int p=x.id;
- if(vis[p]) continue;
- vis[p]=;
- int u=edge[p].v;
- if(u==t) ans=min(ans,dis[p]);
- for(int i=head[u];i!=-;i=edge[i].next){
- int v=edge[i].v;
- if(!vis[i]&&dis[i]>dis[p]+edge[i].c+abs(edge[i].num-edge[p].num)){
- dis[i]=dis[p]+edge[i].c+abs(edge[i].num-edge[p].num);
- q.push((Node){i,dis[i]});
- }
- }
- }
- return ans;
- }
- int main() {
- int n,m;
- while(cin>>n>>m){
- tot=;
- for(int i=;i<=n;i++) head[i]=-;
- for(int i=;i<m;i++){
- int a,b,c,d;
- RI(a);RI(b);RI(c);RI(d);
- add_edge(a,b,c,d);
- }
- cout<<dijkstra(,n)<<endl;
- }
- return ;
- }
dis[i]表示走过i这条边打到edge[i].v的最少花费
这样的话就2∗m条边
把边看作点来做最短路,但是这题的边数未知
所以spfa会TLE,所以要用dijkstra+heap
CSU 1808 地铁的更多相关文章
- CSU 1808: 地铁 最短路
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 1808: 地铁 Time Limit: 5 SecMemory Limit: ...
- 【最短路】【STL】CSU 1808 地铁 (2016湖南省第十二届大学生计算机程序设计竞赛)
题目链接: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1808 题目大意: N个点M条无向边(N,M<=105),每条边属于某一条地铁Ci ...
- CSU 1808 - 地铁 - [最短路变形]
题目链接:http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 Time limit: 5000 ms Memory limit: 13107 ...
- CSU 1808 地铁(最短路变形)
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题意: Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站, ...
- CSU 1808 地铁 (Dijkstra)
Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci ...
- CSU 1808:地铁(Dijkstra)
http://acm.csu.edu.cn/csuoj/problemset/problem?pid=1808 题意:…… 思路:和之前的天梯赛的一题一样,但是简单点. 没办法直接用点去算.把边看成点 ...
- CSUOJ 1808 地铁
Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,-,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i 段地铁属于 ci ...
- 湖南省第十二届大学生计算机程序设计竞赛 F 地铁 多源多汇最短路
1808: 地铁 Description Bobo 居住在大城市 ICPCCamp. ICPCCamp 有 n 个地铁站,用 1,2,…,n 编号. m 段双向的地铁线路连接 n 个地铁站,其中第 i ...
- CSU 1809 Parenthesis(RMQ-ST+思考)
1809: Parenthesis Submit Description Bobo has a balanced parenthesis sequence P=p1 p2…pn of length n ...
随机推荐
- Web Api Session开启会话支持
1.WebApi中默认是没有开启Session会话支持的.需要在Global中重写Init方法来指定会话需要支持的类型 //代码如下 public override voi ...
- poj2378 树形DP
C - 树形dp Crawling in process... Crawling failed Time Limit:1000MS Memory Limit:65536KB 64bit ...
- hdu5338 ZZX and Permutations(贪心、线段树)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud ZZX and Permutations Time Limit: 6000/300 ...
- hdfs-over-ftp安装与配置
hdfs-over-ftp是一个开源,简单易用的实现了对HDFS系统的下载和上传功能的小工具.可以作为管理工具来使用,快捷方便. 1 安装jdk(1.6以上版本)并配置环境变量分别执行java -ve ...
- window.onload,<body onload="function()">, document.onreadystatechange, httpRequest.onreadystatechang
部分内容参考:http://www.aspbc.com/tech/showtech.asp?id=1256 在开发的过程中,经常使用window.onload和body onload两种,很少使用do ...
- ueditor 百度编辑器 自定义图片上传路径和格式化上传文件名
今天项目中需要自定义图片上传的保存路径,并且不需要按照日期自动创建存储文件夹和文件名,我的ueditor版本是1.3.6.下面记录一下我配置成功的方法,如果有什么不对的地方欢迎指出,共同学习: 1:我 ...
- AFNetworking 使用 核心代码
///////////////////////////以前用法///////////////////////////////////////////////////////////// //使用AFH ...
- IOS 项目问题总结
把自己项目中遇到的问题总结一下,供大家参考,希望大家多多提出意见!! 在Xcode 6.2中遇到Your build settings specify a provisioning profile w ...
- Here are some of my ideas .
1:Learning english is very important ,its the very useful for my major studying and my future develo ...
- POJ 3709 K-Anonymous Sequence (单调队列优化)
题意:给定一个不下降数列,一个K,将数列分成若干段,每段的数字个数不小于K,每段的代价是这段内每个数字减去这段中最小数字之和.求一种分法使得总代价最小? 思路:F[i]表示到i的最小代价.f[i]=m ...