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. redis持久化之RDB (七)

    一:什么是redis的持久化 Redis 持久化 Redis 提供了不同级别的持久化方式: RDB持久化方式能够在指定的时间间隔能对你的数据进行快照存储. AOF持久化方式记录每次对服务器写的操作,当 ...

  2. SpringBoot的浅浅配置和小整合

    SpringBoot的浅浅配置和小整合 本文如题,就是浅浅记录一下学习的过程中一些过程,比较简单,并没有多少深度.谢谢! SpringBoot创建 从IDEA中新建项目或者模块.注意jdk版本,一般不 ...

  3. Kubebuilder模块

    CRD创建 Group表示CRD所属的组,它可以支持多种不同版本.不同类型的资源构建,Version表示CRD的版本号,Kind表示CRD的类型 kubebuilder create api --gr ...

  4. Java递归实现评论多级回复

    最近工作需要做一个评论功能,除了展示评论之外,还需要展示评论回复,评论的回复的回复,这里就用到了递归实现评论的多级回复. 评论实体 数据库存储字段: id 评论id.parent_id 回复评论id. ...

  5. 说什么也要脱单——Python WEB开发:用Tornado框架制作简易【表白墙】网站

    先来哔哔两句:(https://jq.qq.com/?_wv=1027&k=QgGWqAVF) 今天我们要用Python做Web开发,做一个简单的[表白墙]网站.众所周知表白墙的功能普遍更多的 ...

  6. Python爬虫常用:谷歌浏览器驱动——Chromedriver 插件安装教程

    我们在做爬虫的时候经常要使用谷歌浏览器驱动,今天分享下这个Chromedriver 插件的安装方法. 第一步:打开谷歌浏览器打开设置面板 嫌枯燥的小伙伴可以点击此处找管理员小姐姐领取免费资料 第二步: ...

  7. 5.RDD操作综合实例

    一.词频统计 A. 分步骤实现 1.准备文件 (1)下载小说或长篇新闻稿 (2)上传到hdfs上 2.读文件创建RDD 3.分词 4. ·排除大小写lower(),map() ·标点符号re.spli ...

  8. Linux,Centos系统下配置java Jdk(附下载地址)

    一.下载jdk 官网下载地址:https://www.oracle.com/java/technologies/javase/javase-jdk8-downloads.html 需要登录Oracle ...

  9. Thread和Runnable的区别和匿名内部类方式实现线程的创建

    如果一个类继承Thread,则不适合资源共享.但是如果实现了Runable接口的话,则很容易的实现资源共享. 总结:实现Runnable接口比继承Thread类所具有的优势: 1.适合多个相同的程序代 ...

  10. throw关键字和Objects非空判断_requireNonNull方法

    作用: 可以使用throw关键字在指定的方法中抛出指定的异常 使用格式: throw new xxxException("异常产生的原因") 注意: 1.throw关键字必须写在方 ...