Codeforces 1076D Edge Deletion(最短路树)
题目链接:Edge Deletion
题意:给定一张n个顶点,m条边的带权无向图,已知从顶点1到各个顶点的最短路径为di,现要求保留最多k条边,使得从顶点1到各个顶点的最短距离为di的顶点最多。输出m条边中需要保留的边的编号。
题解:先跑一遍最短路,在松弛操作时,存父子关系和边,在以这些关系建立新图(树),因为在松弛操作时存的关系,所以能保证是最短路径,最后DFS输出k条边即可。
#include <queue>
#include <cstdio>
#include <cstring>
using namespace std; typedef long long ll;
const int N=3e5+; struct qnode{
ll v,w;
qnode(){}
qnode(ll v,ll w):v(v),w(w){}
bool operator < (const qnode& b) const{
return w>b.w;
}
}; struct node{
ll nxt,v,w;
node(){}
node(ll nxt,ll v,ll w):nxt(nxt),v(v),w(w){}
}; ll n,m,k,tot;
node edge[N<<];
ll head[N],d[N];
qnode cur,tmp;
bool vis[N];
priority_queue <qnode> Q;
pair <ll,ll> fa[N];
vector <int> g[N],ans; void add_edge(ll u,ll v,ll w){
edge[tot]=node(head[u],v,w);
head[u]=tot++;
} void init(){
tot=;
memset(head,,sizeof(head));
} void dijkstra(ll s){
for(int i=;i<N;i++) d[i]=1e18;
d[s]=;
Q.push(qnode(s,));
while(!Q.empty()){
cur=Q.top();
Q.pop();
ll u=cur.v;
if(vis[u]) continue;
vis[u]=true;
for(ll i=head[u];i;i=edge[i].nxt){
ll v=edge[i].v;
ll w=edge[i].w;
if(d[u]+w<d[v]){
d[v]=d[u]+w;
fa[v]=make_pair(u,(i+)/);
Q.push(qnode(v,d[v]));
}
}
}
} void dfs(int u){
if(k==) return;
if(u!=){
ans.push_back(fa[u].second);
k--;
}
for(ll v:g[u]) dfs(v);
} int main(){
init();
scanf("%lld%lld%lld",&n,&m,&k);
for(ll i=;i<=m;i++){
ll u,v,w;
scanf("%lld%lld%lld",&u,&v,&w);
add_edge(u,v,w);
add_edge(v,u,w);
}
dijkstra();
for(ll i=;i<=n;i++) g[fa[i].first].push_back(i);
dfs();
printf("%d\n",ans.size());
for(ll u:ans) printf("%lld ",u);
printf("\n");
return ;
}
Codeforces 1076D Edge Deletion(最短路树)的更多相关文章
- CF1076D Edge Deletion 最短路树
问题描述 Codeforces 洛谷(有翻译) 题解 最短路树,是一棵在最短路过程中构建的树. 在\(\mathrm{Dijkstra}\)过程中,如果最终点\(y\)是由点\(x\)转移得到的,则在 ...
- Codeforces 1076D Edge Deletion 【最短路+贪心】
<题目链接> 题目大意: n个点,m条边的无向图,现在需要删除一些边,使得剩下的边数不能超过K条.1点为起点,如果1到 i 点的最短距离与删除边之前的最短距离相同,则称 i 为 " ...
- 1076D Edge Deletion 【最短路】
题目:戳这里 题意:求出1到所有点的最短路径后,把边减到小于等于k条,问保留哪些边可以使仍存在的最短路径最多. 解题思路:这题就是考求最短路的原理.比如dijkstra,用优先队列优化后存在队列中的前 ...
- [Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs)
[Codeforces 1005F]Berland and the Shortest Paths(最短路树+dfs) 题面 题意:给你一个无向图,1为起点,求生成树让起点到其他个点的距离最小,距离最小 ...
- Berland and the Shortest Paths CodeForces - 1005F(最短路树)
最短路树就是用bfs走一遍就可以了 d[v] = d[u] + 1 表示v是u的前驱边 然后遍历每个结点 存下它的前驱边 再用dfs遍历每个结点 依次取每个结点的某个前驱边即可 #include &l ...
- hdu 3409 最短路树+树形dp
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3409 参考博客:http://www.cnblogs.com/woaishizhan/p/318981 ...
- LA4080/UVa1416 Warfare And Logistics 最短路树
题目大意: 求图中两两点对最短距离之和 允许你删除一条边,让你最大化删除这个边之后的图中两两点对最短距离之和. 暴力:每次枚举删除哪条边,以每个点为源点做一次最短路,复杂度\(O(NM^2logN)\ ...
- 51nod 1443 路径和树(最短路树)
题目链接:路径和树 题意:给定无向带权连通图,求从u开始边权和最小的最短路树,输出最小边权和. 题解:构造出最短路树,把存留下来的边权全部加起来.(跑dijkstra的时候松弛加上$ < $变成 ...
- Connections between cities HDU - 2874(最短路树 lca )
题意: 给出n个点m条边的图,c次询问 求询问中两个点间的最短距离. 解析: Floyd会T,所以用到了最短路树..具体思想为: 设k为u和v的最近公共祖先 d[i] 为祖结点到i的最短距离 则di ...
随机推荐
- Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from https://repo.maven.apache.org/maven2 was cached in the local repository, resolution will not be reattempted until the update interval o
pom.xml报错: Failure to transfer org.apache.maven:maven-archiver:pom:2.5 from https://repo.maven.apach ...
- C调用C++, C++调用C方法
1. C 调用 C++封装好后的函数: -> 在C++中有一个函数 int main_cpp(): -> 首先构建头文件, #ifndef CPP_FILE_H #define CPP ...
- lumen 5.6 设置APP_KEY为32位长的随机字符串
在 App\Console\Commands下 添加以下内容的KeyGenerateCommand.php文件 <?php namespace App\Console\Commands; use ...
- css3特殊图形(气泡)
一.气泡 效果: body{ background: #dd5e9d; height: 100%; } .paopao { position: absolute; width: 200px; heig ...
- Linux 的相关操作
切换权限 在linux环境下,用户之前的切换使用 “su - name,若要切换到root下面,则使用sudo su 命令即可. 在linux下安装软件,经常就是装完后不知道装到哪里去了 (201 ...
- dentry path_lookat dput
https://www.ibm.com/developerworks/cn/linux/l-cn-usagecounter/index.html https://blog.csdn.net/young ...
- Java对象clone()的测试
Object中自带native clone()方法. 研究了一下用法. public class DeepCopyTest { public static void main(String[] arg ...
- Android SDK Tools 24.3.2 Build脚本Bug
如下图版本: 在%Android_home%\tools\ant\build.xml中, 在483行附近, 少了aidl,aapt,dx, zipalign四个变量的声明. 加上就OK了. <p ...
- mybatis:数据持久层框架
mybatis是一个持久层的框架,是Apache下的顶级项目. mybatis托管到goolecode下,再后来托管到GitHub下:https://github.com/mybatis/mybati ...
- ADO工具类
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Data; ...