小雨坐地铁--[最短路分层建图+虚点]

news/2024/6/28 18:00:54

也是第一次接触这种分层建图的最短路
思路:由题目我们可以知道某些站点是可以连接好几条地铁线路的,那么对于每条地铁线路我们可以把他当成一幅图来算。当然图是个无向图,所以要加两次边。

add(i*n+x,i*n+pre,b);  //乘i的话就是说把他建在第i层,这个pre是记录上一个点的位置。
add(i*n+pre,i*n+x,b);

处理换乘时的操作
利用到我们刚刚所说的虚点

add(x,i*n+x,a);  //从虚点到地铁线上需要花费,花费坐这条线的价格//也就是题中(坐i号线需要花费a[i]的价格)
add(i*n+x,x,0);   //从地铁线上到虚点花费为0

根据题目中样例建成的图就是(有点丑,别介意)
在这里插入图片描述

接下来的过程就跟最短路模板题一样了
代码:

#pragma GCC optimize(3,"Ofast","inline")
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <math.h>
#include <string>
#include <list>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#define maxn  1000010
const int MaxN = 0x3f3f3f3f;
const int MinN = 0xc0c0c00c;
typedef long long ll;
const int mod = 1000007;
using namespace std;
struct wazxy{int to,w,next;
}edge[maxn];
struct node{int n,dis;node(int a,int b){n=a,dis=b;}bool operator <(const node & a)const{return dis>a.dis;}
};
int dis[maxn];
int head[maxn];
int cnt=0,n,m,s,t;
bool visited[maxn];
void init(){memset(head,-1,sizeof(head));memset(visited,false,sizeof(visited));memset(dis,MaxN,sizeof(dis));
}
void add(int u,int v,int w){edge[cnt].w=w;edge[cnt].to=v;edge[cnt].next=head[u];head[u]=cnt++;
}
void dij(int x){dis[x]=0;priority_queue<node> q;q.push(node(x,0));while(!q.empty()){node temp=q.top();q.pop();if(visited[temp.n]) continue;visited[temp.n]=true;for(int i=head[temp.n];~i;i=edge[i].next){int v=edge[i].to;int w=edge[i].w;if(visited[v]) continue;if(dis[v]>temp.dis+w){dis[v]=temp.dis+w;q.push(node(v,dis[v]));}}}
}
int main()
{cin>>n>>m>>s>>t;init();for(int i=1;i<=m;i++){int a,b,c,x;scanf("%d%d%d",&a,&b,&c);int pre=-1;while(c--){cin>>x;if(pre!=-1){add(i*n+x,i*n+pre,b);  //乘i的话就是说把他建在第i层add(i*n+pre,i*n+x,b);}add(x,i*n+x,a);add(i*n+x,x,0);pre=x;}}dij(s);if(dis[t]==MaxN) cout<<"-1"<<endl;else cout<<dis[t]<<endl;return 0;
}

http://lihuaxi.xjx100.cn/news/285942.html

相关文章

Google经典面试题解析

作者 | Alex Golec译者 | 弯月责编 | 屠敏出品 | CSDN&#xff08;ID&#xff1a;CSDNnews&#xff09;在深入问题之前&#xff0c;有一个令人振奋的消息&#xff1a;我离开了Google&#xff01;我激动地宣布&#xff0c;我已经加入了Reddit&#xff0c;并在纽约市担任项目经理…

树形dp ---- gym101655 D - Delta Quadrant 树上连通块思维换根 + 树形dp

题目链接 题目大意&#xff1a; 给你一颗NNN个节点的树&#xff0c;树上每条边有边权就是遍历的时间&#xff0c;你可以从任意节点出发遍历N−kN-kN−k个点并且回到出发点&#xff0c;问你最短的时间是多少&#xff1f; k∈[0,min(N,20)],N∈[1,1e4]k\in[0,min(N,20)],N\in[1,…

任正非:要感谢特朗普,他一吓唬,治好了华为人的富裕病,都努力工作了

华为“心声社区”前几日公布了任正非接受北欧媒体采访的纪要。据纪要显示&#xff0c;前几日任正非接受采访&#xff0c;参与采访的记者来自瑞典国家电视台、丹麦广播公司等多家媒体。任正非在采访中就华为建筑的欧式设计、美国总统特朗普、5G和人工智能等问题作答。任正非(图源…

寒假要完成的任务

这个寒假&#xff0c;我要做的很多&#xff0c;一下是我做了一小的总结&#xff0c;便于提醒自己。 我要做的有这个几个&#xff1a;1.蓝桥杯&#xff0c;每天2道题2.学习Heberate ,spring &#xff0c;三大框架3.做项目&#xff0c;学习android4.去完成计算机网络三、四级&…

《中国人工智能ABC人才发展报告》发布,算法和应用类人才短缺

近日&#xff0c;百度云联手中国传媒大学、BOSS 直聘和百度指数发布了《中国人工智能 ABC 人才发展报告&#xff08;2018版&#xff09;》&#xff08;以下简称“报告”&#xff09;和百度云智学院2019 年人才认证体系。报告指出&#xff0c;从 2018 年的人才供需状况来看&…

Codeforces Round #640 (Div. 4)(ABCDEG题解)

文章目录A. Sum of Round NumbersB - Same Parity SummandsC - K-th Not Divisible by nD - Alice, Bob and CandiesE - Special ElementsF. Binary String ReconstructionG - Special PermutationA. Sum of Round Numbers 题解&#xff1a;把他一个整数拆分&#xff0c;模拟一…

第1关:实现一个顺序存储的线性表

#if !defined(Seqlist__CIELSI_989SE_AJIEZN_728JULC__INCLUDED_) #define Seqlist__CIELSI_989SE_AJIEZN_728JULC__INCLUDED_ typedef int T; // 数据元素的数据类型 struct SeqList{T* data; // 数据元素的开始地址int len; // 当前长度int max; // 线性表的最大长度 };SeqL…

一文带你看懂Springboot核心功能及优缺点

点击上方[视学算法]→右上角[...]→[设为星标⭐]SpringBoot核心功能1、独立运行Spring项目Spring boot 可以以jar包形式独立运行&#xff0c;运行一个Spring Boot项目只需要通过java -jar xx.jar来运行。2、内嵌servlet容器Spring Boot可以选择内嵌Tomcat、jetty或者Undertow,这…