当前位置:网站首页>L3-007 天梯地图 (30 分)(条件dij
L3-007 天梯地图 (30 分)(条件dij
2022-04-22 09:16:00 【lcxdz】
#include<bits/stdc++.h>
using namespace std;
const int N=510;
int st,ed;
struct node{
int to,length,time;
}t[N];
vector<node>v[N];
vector<int>anstime,anslength;
typedef pair<int,int> pii;
int dist[N],vis[N],path[N],sum[N];
void dijtime(){
priority_queue<pii,vector<pii>,greater<pii>>q;
q.push({
0,st});
memset(dist,0x3f,sizeof dist);
memset(path,-1,sizeof path);
memset(vis,0,sizeof vis);
memset(sum,0,sizeof sum);
dist[st]=0;
while(q.size()){
auto p=q.top();
q.pop();
if(vis[p.second])continue;
vis[p.second]=1;
for(auto it:v[p.second]){
if(dist[it.to]>dist[p.second]+it.time){
// cout<<it.to<<"\n";
dist[it.to]=dist[p.second]+it.time;
path[it.to]=p.second;
sum[it.to]=sum[p.second]+it.length;
q.push({
dist[it.to],it.to});
}
else if(dist[it.to]==dist[p.second]+it.time){
if(sum[it.to]>sum[p.second]+it.length){
sum[it.to]=sum[p.second]+it.length;
path[it.to]=p.second;
q.push({
dist[it.to],it.to});
}
}
}
}
}
void dij(){
priority_queue<pii,vector<pii>,greater<pii>>q;
q.push({
0,st});
memset(dist,0x3f,sizeof dist);
memset(path,-1,sizeof path);
memset(vis,0,sizeof vis);
memset(sum,0,sizeof sum);
dist[st]=0;
while(q.size()){
auto p=q.top();
q.pop();
if(vis[p.second])continue;
vis[p.second]=1;
for(auto it:v[p.second]){
if(dist[it.to]>dist[p.second]+it.length){
// cout<<it.to<<"\n";
dist[it.to]=dist[p.second]+it.length;
path[it.to]=p.second;
sum[it.to]=sum[p.second]+1;
q.push({
dist[it.to],it.to});
}
else if(dist[it.to]==dist[p.second]+it.length){
if(sum[it.to]>sum[p.second]+1){
sum[it.to]=sum[p.second]+1;
path[it.to]=p.second;
q.push({
dist[it.to],it.to});
}
}
}
}
}
int main(){
int n,m;
cin>>n>>m;
while(m--){
int a,b,way,length,time;
cin>>a>>b>>way>>length>>time;
v[a].push_back({
b,length,time});
if(way==0){
v[b].push_back({
a,length,time});
}
}
cin>>st>>ed;
dijtime();
int TIME=dist[ed];
int over=ed;
anstime.push_back(ed);
while(over!=st){
anstime.push_back(path[over]);
over=path[over];
}
dij();
// for(int i=anstime.size()-1;i>=0;i--){
// if(i==anstime.size()-1)cout<<": ";
// else cout<<" => ";
// cout<<anstime[i];
// }
int DIS=dist[ed];
over=ed;
anslength.push_back(ed);
while(over!=st){
anslength.push_back(path[over]);
over=path[over];
}
if(anslength==anstime){
printf("Time = %d; Distance = %d",TIME,DIS);
for(int i=anstime.size()-1;i>=0;i--){
if(i==anstime.size()-1)cout<<": ";
else cout<<" => ";
cout<<anstime[i];
}
}
else {
cout<<"Time = "<<TIME;
for(int i=anstime.size()-1;i>=0;i--){
if(i==anstime.size()-1)cout<<": ";
else cout<<" => ";
cout<<anstime[i];
}
cout<<"\nDistance = "<<DIS;
for(int i=anslength.size()-1;i>=0;i--){
if(i==anslength.size()-1)cout<<": ";
else cout<<" => ";
cout<<anslength[i];
}
}
return 0;
}
版权声明
本文为[lcxdz]所创,转载请带上原文链接,感谢
https://minelois.blog.csdn.net/article/details/124332188
边栏推荐
- 量化投资学习——介绍orderflow
- LeetCode 349. 两个数组的交集(简单、数组)day12
- Flink流处理引擎系统学习(二)
- 手動搭建hyperledger fabric v2.x 生產網絡(四)創建通道,鏈碼的生命周期
- Separate linked list (create two empty linked lists)
- Using docker to build LNMP + redis development environment suitable for thinkphp5
- Numpy notes (vstack, random.permutation, empty_like, empty, diff, random.choice, random.random, ISIN)
- Manually build hyperledger fabric v2 X production network (IV) create channel and chain code life cycle
- Cmake使用基础知识一之基础语法
- Six methods can make you gain more in NFT trading
猜你喜欢

安装Navicat 15 详解及相关问题详解

R语言进阶|广义向量和属性解析

LeetCode 447. 回旋镖的数量 (排列组合问题)

mysql用source命令导入sql出现报错 觉得应该是编码问题 不知道该怎么解决

ERP 集成对公司系统完善的重要性

Understand the first snapshot read and the second snapshot read of the same transaction in MySQL mvcc - Notes
Unicorn Bio筹集320万美元资金 将用于培养肉的原型设备变成商业产品

2022G3锅炉水处理上岗证题目及答案

The request client is not a secure context and the resource is in more-private address ...

基于麒麟SP10服务器版的Kubernetes集群安装
随机推荐
push()与pop()的使用
The request client is not a secure context and the resource is in more-private address ...
PHP & Laravel & 掌握 api 生成 token 的几种方式以及一些注意事项(坑)
EventBridge 集成云服务实践
云原生架构下的微服务选型和演进
Redis 内存回收策略
软件测试工程师简历要怎么写,才能让 HR 看到?
mysql C语言连接
Open3D点云处理
从键盘中随意输入一串字符,统计并输出该字符串中各字符(数字、大写字母、小写字母、标点符号等)各自出现的次数。用面向对像的思想实现。
MySQL-on duplicate key upate/case when语法使用
APP优化及积分榜进阶下篇【MUI+Flask+MongoDB】
sqlserver 数据传输到 mysql
Using stack to realize queue (double stack, input stack and output stack)
pip install shutil 报错
Neo4j: merge [create if it doesn't exist, modifiable if it already exists]
Stream API 优化代码
LeetCode40-组合总数II
【大话云原生】微服务篇-五星级酒店的服务方式
pytorch模型加载跑测试集和训练过程中跑测试集结果不一致的问题?