E - Road Reduction (atcoder.jp)

题意:一棵树n个点,m条路, di表示1-i的距离,问怎么选择边可以使得d2+...dn最短。

题解: 很明显,就是直接套最短路板子,判断每一个点的最短路,题目里没有负权值,直接dijkstra就可以了。边如何记录,与每个点相连的那个最短路的边,每次更新出来储存,然后直接输出即可。

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const int N=5e5+5;
ll vis[N],pre[N];
ll dis[N],cnt,head[N];
ll ans[N];
struct ss{
ll next,to,w,id;
}e[N];
void add(ll x,ll y,ll w,ll id){
e[++cnt].to=y;
e[cnt].w=w;
e[cnt].id=id;
e[cnt].next=head[x];
head[x]=cnt;
}
void dij(){
priority_queue<pll,vector<pll>,greater<pll> >q;
memset(dis,0x3f,sizeof(dis));
memset(vis,0,sizeof(vis));
q.push({0,1});
dis[1]=0;
while(!q.empty()){
ll v=q.top().first;
ll u=q.top().second;
q.pop();
if(vis[u]) continue;
vis[u]=1;
for(ll i=head[u];i;i=e[i].next){
ll j=e[i].to;
if(dis[j]>v+e[i].w){
dis[j]=dis[u]+e[i].w;
q.push({dis[j],j});
ans[j]=e[i].id;
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0);
ll n,m;cin>>n>>m;
for(ll i=1;i<=m;i++){
ll x,y,w;cin>>x>>y>>w;
add(x,y,w,i);
add(y,x,w,i);
}
dij();
for(int i=2;i<=n;i++){
cout<<ans[i]<<" ";
}
}

E - Road Reduction的更多相关文章

  1. 【Codeforces 738C】Road to Cinema

    http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...

  2. POJ 3204 Ikki's Story I - Road Reconstruction

    Ikki's Story I - Road Reconstruction Time Limit: 2000MS   Memory Limit: 131072K Total Submissions: 7 ...

  3. Lattice Reduction (LLL) 算法C代码实现

    废话不多说,大名鼎鼎的Lenstra-Lenstra-Lovasz(LLL) 算法.实现参考论文:Factoring Polynomials with Rational Coefficients, 作 ...

  4. Codeforces #380 div2 C(729C) Road to Cinema

    C. Road to Cinema time limit per test 1 second memory limit per test 256 megabytes input standard in ...

  5. dp or 贪心 --- hdu : Road Trip

    Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...

  6. Stanford机器学习笔记-10. 降维(Dimensionality Reduction)

    10. Dimensionality Reduction Content  10. Dimensionality Reduction 10.1 Motivation 10.1.1 Motivation ...

  7. HDU 1598 find the most comfortable road(最小生成树之Kruskal)

    题目链接: 传送门 find the most comfortable road Time Limit: 1000MS     Memory Limit: 32768 K Description XX ...

  8. CUDA中并行规约(Parallel Reduction)的优化

    转自: http://hackecho.com/2013/04/cuda-parallel-reduction/ Parallel Reduction是NVIDIA-CUDA自带的例子,也几乎是所有C ...

  9. 三分 --- CSU 1548: Design road

    Design road Problem's Link:   http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到 ...

随机推荐

  1. 数字化转型之数字资产知识库(springboot+es+vue+neo4j)

    前言 在数字化高度普及的时代,企事业机关单位在日常工作中会产生大量的文档,例如医院制度汇编,企业知识共享库等.针对这些文档性的东西,手工纸质化去管理是非常消耗工作量的,并且纸质化查阅难,易损耗,所以电 ...

  2. Java学习-第一阶段-第一节:Java概述

    JAVA概述 Java版本 原网址(https://www.oracle.com/java/technologies/java-se-support-roadmap.html) Oracle 将仅将某 ...

  3. Redis布隆过滤器和布谷鸟过滤器

    一.过滤器使用场景:比如有如下几个需求:1.原本有10亿个号码,现在又来了10万个号码,要快速准确判断这10万个号码是否在10亿个号码库中? 解决办法一:将10亿个号码存入数据库中,进行数据库查询,准 ...

  4. 讲给测试人员的docker知识

    docker对测试来说有什么用 docker类似于Windows系统的虚拟机,对于测试来说docker意味着一种新的测试环境部署方式,由于其镜像分层的设置,我们可以在一台物理机上同过docker的方式 ...

  5. Identity Server 4资源拥有者密码认证控制访问API

    基于上一篇文章中的代码进行继续延伸,只需要小小的改动即可,不明白的地方可以先看看本人上一篇文章及源码: Identity Server 4客户端认证控制访问API 一.QuickStartIdenti ...

  6. Rails_via牛客网

    题目 链接:https://ac.nowcoder.com/acm/contest/28537/D 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32768K,其他语言 ...

  7. efcore在Saas系统下多租户零脚本分表分库读写分离解决方案

    efcore在Saas系统下多租户零脚本分表分库读写分离解决方案 ## 介绍 本文ShardinfCore版本x.6.0.20+ 本期主角: - [`ShardingCore`](https://gi ...

  8. MD5,Des,RSA加密解密

    一.加密和解密 下面先熟悉几个概念 1>对称加密:加密的key和解密的key是同一个 但是如何确保密钥安全地进行传递?秘钥的安全是一个问题 2>非对称加密:加密点的key和解密的key不是 ...

  9. 匿名对象和private关键字

    匿名内部类 没有名次的对象我们成为匿名对象 new Dog(); new Demo02().change(new Student("...")); 匿名对象的特点 如果对象只会被使 ...

  10. Linux ssh协议

    基础知识 ssh:secure shell protocol,安全的远程登录 作用:是建立在应用层基础上的安全协议,实现数据传输过程中数据的加密,代替telent协议 使用tcp协议,端口号为22 s ...