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

相关文章

MyBatisPlus如何优化千万级数据的CRUD

《MyBatisPlus如何优化千万级数据的CRUD》最近负责的一个项目,数据库表量级破千万,每次执行CRUD都像走钢丝,稍有不慎就引起数据库报警,本文就结合这个项目的实战经验,聊聊MyBatisPl... 目录背景一、MyBATis Plus 简介二、千万级数据的挑战三、优化 CRUD 的关键策略1. 查

如何在Mac上彻底删除Edge账户? 手动卸载Edge浏览器并清理残留文件技巧

《如何在Mac上彻底删除Edge账户?手动卸载Edge浏览器并清理残留文件技巧》Mac上的Edge账户里存了不少网站密码和个人信息,结果同事一不小心打开了,简直尴尬到爆炸,想要卸载edge浏览器并清... 如果你遇到 Microsoft Edge 浏览器运行迟缓、频繁崩溃或网页加载异常等问题,可以尝试多种方

SpringBoot中HTTP连接池的配置与优化

《SpringBoot中HTTP连接池的配置与优化》这篇文章主要为大家详细介绍了SpringBoot中HTTP连接池的配置与优化的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、HTTP连接池的核心价值二、Spring Boot集成方案方案1:Apache HttpCl

PyTorch高级特性与性能优化方式

《PyTorch高级特性与性能优化方式》:本文主要介绍PyTorch高级特性与性能优化方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、自动化机制1.自动微分机制2.动态计算图二、性能优化1.内存管理2.GPU加速3.多GPU训练三、分布式训练1.分布式数据

MySQL中like模糊查询的优化方案

《MySQL中like模糊查询的优化方案》在MySQL中,like模糊查询是一种常用的查询方式,但在某些情况下可能会导致性能问题,本文将介绍八种优化MySQL中like模糊查询的方法,需要的朋友可以参... 目录1. 避免以通配符开头的查询2. 使用全文索引(Full-text Index)3. 使用前缀索

C#实现高性能Excel百万数据导出优化实战指南

《C#实现高性能Excel百万数据导出优化实战指南》在日常工作中,Excel数据导出是一个常见的需求,然而,当数据量较大时,性能和内存问题往往会成为限制导出效率的瓶颈,下面我们看看C#如何结合EPPl... 目录一、技术方案核心对比二、各方案选型建议三、性能对比数据四、核心代码实现1. MiniExcel

Python中edge-tts实现便捷语音合成

《Python中edge-tts实现便捷语音合成》edge-tts是一个功能强大的Python库,支持多种语言和声音选项,本文主要介绍了Python中edge-tts实现便捷语音合成,具有一定的参考价... 目录安装与环境设置文本转语音查找音色更改语音参数生成音频与字幕总结edge-tts 是一个功能强大的

MySQL索引的优化之LIKE模糊查询功能实现

《MySQL索引的优化之LIKE模糊查询功能实现》:本文主要介绍MySQL索引的优化之LIKE模糊查询功能实现,本文通过示例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前缀匹配优化二、后缀匹配优化三、中间匹配优化四、覆盖索引优化五、减少查询范围六、避免通配符开头七、使用外部搜索引擎八、分

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

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

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

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