当前位置:网站首页>牛客:飞行路线(分层图+最短路)
牛客:飞行路线(分层图+最短路)
2022-06-25 06:43:00 【mfy的1号小迷弟】
牛客:飞行路线(分层图+最短路)
题意:
一个带边权的图,求从s到t的最短距离,可以忽略k条边的权值。
2 < = n < = 10000 , 1 < = m < = 50000 , 0 < = k < = 10 2<=n<=10000,1<=m<=50000,0<=k<=10 2<=n<=10000,1<=m<=50000,0<=k<=10
思路:
因为k很小,可以想不 到分层图,把原图看成k层,每层完全一样,再建新边把每层连起来,对于第一层的从x到y,把x每层对应的点和下一层的y连一条权值为0的边,把y每层对应的点和下一层的x连一条权值为0的边。这样从某层的x到下一层的y就表示用了一次k
再跑最短路
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
typedef pair<int,int>pii;
const int maxn=3e6+5;
int s,n,m,t,k,d,dis[maxn],vis[maxn],head[maxn],w[maxn],to[maxn],nex[maxn];
void add(int x,int y,int z){
nex[++d]=head[x];
to[d]=y;
w[d]=z;
head[x]=d;
}
void dij(){
memset(dis,INF,sizeof(dis));
dis[s]=0;
priority_queue<pii,vector<pii>,greater<pii>>p;
p.push({
0,s});
while(!p.empty()){
int x=p.top().second;
p.pop();
if(vis[x])continue;
vis[x]=1;
for(int i=head[x];i;i=nex[i]){
int y=to[i];
if(dis[y]>dis[x]+w[i]){
dis[y]=dis[x]+w[i];
p.push({
dis[y],y});
}
}
}
}
int main(){
scanf("%d%d%d%d%d",&n,&m,&k,&s,&t);
for(int i=1,x,y,z;i<=m;i++){
scanf("%d%d%d",&x,&y,&z);
add(x,y,z),add(y,x,z);
for(int j=1;j<=k;j++){
add(x+j*n,y+j*n,z);
add(y+j*n,x+j*n,z);
add(x+(j-1)*n,y+j*n,0);
add(y+(j-1)*n,x+j*n,0);
}
}
dij();
int ans=INF;
for(int i=0;i<=k;i++){
ans=min(ans,dis[i*n+t]);
}
printf("%d\n",ans);
}
边栏推荐
- C WinForm panel custom picture and text
- [daily training] 207 Class Schedule Card
- C#中如何调整图像大小
- C control refresh
- Knowledge sharing 𞓜 conventional laminated structure of six layer PCB
- Requirements for Power PCB circuit board design 2021-11-09
- The method of judging whether triode can amplify AC signal
- SCM Project Training
- 判断用户是否是第一次进入某个页面
- 基于RBAC 的SAAS系统权限设计
猜你喜欢

Technology blog | how to communicate using SSE

Anaconda based module installation and precautions

传统的IO存在什么问题?为什么引入零拷贝的?

Invalid Navicat scheduled task

Importer des données dans MATLAB

50 pieces of professional knowledge of Product Manager (IV) - from problem to ability improvement: amdgf model tool

差点被这波Handler 面试连环炮带走~

用函数的递归来解决几道有趣的题

Can bus working condition and signal quality "physical examination"

Manufacturing process of PCB 2021-10-11
随机推荐
OpenCV每日函数 结构分析和形状描述符(8) fitLine函数 拟合直线
RMQ区间最大值下标查询,区间最值
神经网络与深度学习-3- 机器学习简单示例-PyTorch
Can transparent cloud gateway caniot and candtu record can messages and send and receive can data remotely
力扣76题,最小覆盖字串
Invalid Navicat scheduled task
剑指 Offer II 027. 回文链表
饮食干预减轻癌症治疗相关症状和毒性
深度学习系列48:DeepFaker
50. pow (x, n) - fast power
FFT【模板】
Atlas conflict Remote Code Execution Vulnerability (cve-2022-26134 vulnerability analysis and protection
C get the version number of exe - file version and assembly version
Analysis and utilization of Microsoft Office Word remote command execution vulnerability (cve-2022-30190)
1464. maximum product of two elements in an array
57. 插入区间
环网冗余式CAN/光纤转换器的CAN光端机在消防火灾联网报警系统中的应用
Analysis of kinsing dual platform mining family virus
基于Anaconda的模块安装与注意事项
线程+线程问题记录