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

相关文章

从原理到实战解析Java Stream 的并行流性能优化

《从原理到实战解析JavaStream的并行流性能优化》本文给大家介绍JavaStream的并行流性能优化:从原理到实战的全攻略,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的... 目录一、并行流的核心原理与适用场景二、性能优化的核心策略1. 合理设置并行度:打破默认阈值2. 避免装箱

Python实战之SEO优化自动化工具开发指南

《Python实战之SEO优化自动化工具开发指南》在数字化营销时代,搜索引擎优化(SEO)已成为网站获取流量的重要手段,本文将带您使用Python开发一套完整的SEO自动化工具,需要的可以了解下... 目录前言项目概述技术栈选择核心模块实现1. 关键词研究模块2. 网站技术seo检测模块3. 内容优化分析模

Java实现复杂查询优化的7个技巧小结

《Java实现复杂查询优化的7个技巧小结》在Java项目中,复杂查询是开发者面临的“硬骨头”,本文将通过7个实战技巧,结合代码示例和性能对比,手把手教你如何让复杂查询变得优雅,大家可以根据需求进行选择... 目录一、复杂查询的痛点:为何你的代码“又臭又长”1.1冗余变量与中间状态1.2重复查询与性能陷阱1.

Python内存优化的实战技巧分享

《Python内存优化的实战技巧分享》Python作为一门解释型语言,虽然在开发效率上有着显著优势,但在执行效率方面往往被诟病,然而,通过合理的内存优化策略,我们可以让Python程序的运行速度提升3... 目录前言python内存管理机制引用计数机制垃圾回收机制内存泄漏的常见原因1. 循环引用2. 全局变

Python多线程应用中的卡死问题优化方案指南

《Python多线程应用中的卡死问题优化方案指南》在利用Python语言开发某查询软件时,遇到了点击搜索按钮后软件卡死的问题,本文将简单分析一下出现的原因以及对应的优化方案,希望对大家有所帮助... 目录问题描述优化方案1. 网络请求优化2. 多线程架构优化3. 全局异常处理4. 配置管理优化优化效果1.

MySQL中优化CPU使用的详细指南

《MySQL中优化CPU使用的详细指南》优化MySQL的CPU使用可以显著提高数据库的性能和响应时间,本文为大家整理了一些优化CPU使用的方法,大家可以根据需要进行选择... 目录一、优化查询和索引1.1 优化查询语句1.2 创建和优化索引1.3 避免全表扫描二、调整mysql配置参数2.1 调整线程数2.

深入解析Java NIO在高并发场景下的性能优化实践指南

《深入解析JavaNIO在高并发场景下的性能优化实践指南》随着互联网业务不断演进,对高并发、低延时网络服务的需求日益增长,本文将深入解析JavaNIO在高并发场景下的性能优化方法,希望对大家有所帮助... 目录简介一、技术背景与应用场景二、核心原理深入分析2.1 Selector多路复用2.2 Buffer

SpringBoot利用树形结构优化查询速度

《SpringBoot利用树形结构优化查询速度》这篇文章主要为大家详细介绍了SpringBoot利用树形结构优化查询速度,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录一个真实的性能灾难传统方案为什么这么慢N+1查询灾难性能测试数据对比核心解决方案:一次查询 + O(n)算法解决

小白也能轻松上手! 路由器设置优化指南

《小白也能轻松上手!路由器设置优化指南》在日常生活中,我们常常会遇到WiFi网速慢的问题,这主要受到三个方面的影响,首要原因是WiFi产品的配置优化不合理,其次是硬件性能的不足,以及宽带线路本身的质... 在数字化时代,网络已成为生活必需品,追剧、游戏、办公、学习都离不开稳定高速的网络。但很多人面对新路由器

MySQL深分页进行性能优化的常见方法

《MySQL深分页进行性能优化的常见方法》在Web应用中,分页查询是数据库操作中的常见需求,然而,在面对大型数据集时,深分页(deeppagination)却成为了性能优化的一个挑战,在本文中,我们将... 目录引言:深分页,真的只是“翻页慢”那么简单吗?一、背景介绍二、深分页的性能问题三、业务场景分析四、