Codeforces Contest 1076 problem D Edge Deletion —— dijkstra的一些优化

2024-04-07 00:48

本文主要是介绍Codeforces Contest 1076 problem D Edge Deletion —— dijkstra的一些优化,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

You are given an undirected connected weighted graph consisting of n vertices and m edges. Let’s denote the length of the shortest path from vertex 1 to vertex i as di.

You have to erase some edges of the graph so that at most k edges remain. Let’s call a vertex i good if there still exists a path from 1 to i with length di after erasing the edges.

Your goal is to erase the edges in such a way that the number of good vertices is maximized.

Input
The first line contains three integers n, m and k (2≤n≤3⋅105, 1≤m≤3⋅105, n−1≤m, 0≤k≤m) — the number of vertices and edges in the graph, and the maximum number of edges that can be retained in the graph, respectively.

Then m lines follow, each containing three integers x, y, w (1≤x,y≤n, x≠y, 1≤w≤109), denoting an edge connecting vertices x and y and having weight w.

The given graph is connected (any vertex can be reached from any other vertex) and simple (there are no self-loops, and for each unordered pair of vertices there exists at most one edge connecting these vertices).

Output
In the first line print e — the number of edges that should remain in the graph (0≤e≤k).

In the second line print e distinct integers from 1 to m — the indices of edges that should remain in the graph. Edges are numbered in the same order they are given in the input. The number of good vertices should be as large as possible.

Examples
inputCopy
3 3 2
1 2 1
3 2 1
1 3 3
outputCopy
2
1 2
inputCopy
4 5 2
4 1 8
2 4 1
2 1 3
3 4 9
3 1 5
outputCopy
2
3 2

题意:

给你n个点,m条边,你需要删除一些边使得剩下的边的数量小于等于k,并且我们规定,一个点是“好的”是在删边以后1到这个点的最短距离等于删边之前的最短距离,让你求出需要剩下哪些边使得“好的”的路最多。

题解:

dijkstra求出1到所有点的距离,之后从第一个点出发,看看与它相邻的点有哪些是之前就是最短的,然后放到队列里面,同时ans数组push进去这条边的id,对于这题有一些优化,首先最大的优化是这个:
dijkstra的时候不要用vis数组纪录,而是在pop的时候看这一个状态的点过来的最短路是否等于当前的最短路,如果不是就说明它之后有状态比它更优,还有就是不要把自定义结构体放到队列里,用pa会快,不要用map,直接将这条边的id放到结构体里,还有就是查询和dijkstra可以一起做。
分开做的情况:
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pa pair<ll,int>
const int N=3e5+5;
const ll inf=1e17;
struct node
{int to,next,id;ll val;
}e[N*2];
int head[N],cnt;
void add(int x,int y,ll w,int id)
{e[cnt].to=y;e[cnt].next=head[x];e[cnt].val=w;e[cnt].id=id;head[x]=cnt++;
}
ll dis[N];
int vis[N],last[N];
vector<int>ans;
void dijkstra()
{priority_queue<pa>Q;dis[1]=0;Q.push({0,1});while(!Q.empty()){pa u=Q.top();Q.pop();if(-u.first!=dis[u.second])continue;for(int i=head[u.second];~i;i=e[i].next){int v=e[i].to;ll w=e[i].val;if(dis[v]>dis[u.second]+w){dis[v]=dis[u.second]+w;Q.push({-dis[v],v});}}}
}
void check(int x)
{priority_queue<pa>Q;dis[1]=0;Q.push({0,1});vis[1]=1;while(!Q.empty()&&x){pa u=Q.top();Q.pop();for(int i=head[u.second];~i;i=e[i].next){int v=e[i].to;ll w=e[i].val;if(vis[v])continue;if(dis[v]==dis[u.second]+w){Q.push({-dis[v],v});vis[v]=1;ans.push_back(e[i].id);x--;}if(x<=0)break;}}
}
int main()
{//freopen("in.txt","r",stdin);memset(head,-1,sizeof(head));for(int i=1;i<N;i++)dis[i]=inf;int x,y;ll w;int n,m,k;scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=m;i++){scanf("%d%d%lld",&x,&y,&w);add(x,y,w,i),add(y,x,w,i);}dijkstra();check(k);printf("%d\n",ans.size());for(int i=0;i<ans.size();i++)printf("%d%c",ans[i],i==ans.size()-1?'\n':' ');return 0;
}

合起来的情况:就是加一个数组在搜的时候就记录最优解的id
在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define pa pair<ll,int>
const int N=3e5+5;
const ll inf=1e17;
struct node
{int to,next,id;ll val;
}e[N*2];
int head[N],cnt;
void add(int x,int y,ll w,int id)
{e[cnt].to=y;e[cnt].next=head[x];e[cnt].val=w;e[cnt].id=id;head[x]=cnt++;
}
ll dis[N];
int vis[N],last[N];
vector<int>ans;
void dijkstra(int x)
{priority_queue<pa>Q;dis[1]=0;Q.push({0,1});while(!Q.empty()&&x){pa u=Q.top();Q.pop();if(-u.first!=dis[u.second])continue;if(last[u.second])x--,ans.push_back(last[u.second]);for(int i=head[u.second];~i;i=e[i].next){int v=e[i].to;ll w=e[i].val;if(dis[v]>dis[u.second]+w){dis[v]=dis[u.second]+w;Q.push({-dis[v],v});last[v]=e[i].id;}}}
}
int main()
{//freopen("in.txt","r",stdin);memset(head,-1,sizeof(head));for(int i=1;i<N;i++)dis[i]=inf;int x,y;ll w;int n,m,k;scanf("%d%d%d",&n,&m,&k);for(int i=1;i<=m;i++){scanf("%d%d%lld",&x,&y,&w);add(x,y,w,i),add(y,x,w,i);}dijkstra(k);printf("%d\n",ans.size());for(int i=0;i<ans.size();i++)printf("%d%c",ans[i],i==ans.size()-1?'\n':' ');return 0;
}

这篇关于Codeforces Contest 1076 problem D Edge Deletion —— dijkstra的一些优化的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/881257

相关文章

Python通过模块化开发优化代码的技巧分享

《Python通过模块化开发优化代码的技巧分享》模块化开发就是把代码拆成一个个“零件”,该封装封装,该拆分拆分,下面小编就来和大家简单聊聊python如何用模块化开发进行代码优化吧... 目录什么是模块化开发如何拆分代码改进版:拆分成模块让模块更强大:使用 __init__.py你一定会遇到的问题模www.

SpringBoot首笔交易慢问题排查与优化方案

《SpringBoot首笔交易慢问题排查与优化方案》在我们的微服务项目中,遇到这样的问题:应用启动后,第一笔交易响应耗时高达4、5秒,而后续请求均能在毫秒级完成,这不仅触发监控告警,也极大影响了用户体... 目录问题背景排查步骤1. 日志分析2. 性能工具定位优化方案:提前预热各种资源1. Flowable

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

Spring Boot + MyBatis Plus 高效开发实战从入门到进阶优化(推荐)

《SpringBoot+MyBatisPlus高效开发实战从入门到进阶优化(推荐)》本文将详细介绍SpringBoot+MyBatisPlus的完整开发流程,并深入剖析分页查询、批量操作、动... 目录Spring Boot + MyBATis Plus 高效开发实战:从入门到进阶优化1. MyBatis

MyBatis 动态 SQL 优化之标签的实战与技巧(常见用法)

《MyBatis动态SQL优化之标签的实战与技巧(常见用法)》本文通过详细的示例和实际应用场景,介绍了如何有效利用这些标签来优化MyBatis配置,提升开发效率,确保SQL的高效执行和安全性,感... 目录动态SQL详解一、动态SQL的核心概念1.1 什么是动态SQL?1.2 动态SQL的优点1.3 动态S

Python如何使用__slots__实现节省内存和性能优化

《Python如何使用__slots__实现节省内存和性能优化》你有想过,一个小小的__slots__能让你的Python类内存消耗直接减半吗,没错,今天咱们要聊的就是这个让人眼前一亮的技巧,感兴趣的... 目录背景:内存吃得满满的类__slots__:你的内存管理小助手举个大概的例子:看看效果如何?1.

一文详解SpringBoot响应压缩功能的配置与优化

《一文详解SpringBoot响应压缩功能的配置与优化》SpringBoot的响应压缩功能基于智能协商机制,需同时满足很多条件,本文主要为大家详细介绍了SpringBoot响应压缩功能的配置与优化,需... 目录一、核心工作机制1.1 自动协商触发条件1.2 压缩处理流程二、配置方案详解2.1 基础YAML

MySQL中慢SQL优化的不同方式介绍

《MySQL中慢SQL优化的不同方式介绍》慢SQL的优化,主要从两个方面考虑,SQL语句本身的优化,以及数据库设计的优化,下面小编就来给大家介绍一下有哪些方式可以优化慢SQL吧... 目录避免不必要的列分页优化索引优化JOIN 的优化排序优化UNION 优化慢 SQL 的优化,主要从两个方面考虑,SQL 语

MySQL中慢SQL优化方法的完整指南

《MySQL中慢SQL优化方法的完整指南》当数据库响应时间超过500ms时,系统将面临三大灾难链式反应,所以本文将为大家介绍一下MySQL中慢SQL优化的常用方法,有需要的小伙伴可以了解下... 目录一、慢SQL的致命影响二、精准定位问题SQL1. 启用慢查询日志2. 诊断黄金三件套三、六大核心优化方案方案

Redis中高并发读写性能的深度解析与优化

《Redis中高并发读写性能的深度解析与优化》Redis作为一款高性能的内存数据库,广泛应用于缓存、消息队列、实时统计等场景,本文将深入探讨Redis的读写并发能力,感兴趣的小伙伴可以了解下... 目录引言一、Redis 并发能力概述1.1 Redis 的读写性能1.2 影响 Redis 并发能力的因素二、