E - Road Reduction
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的更多相关文章
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- POJ 3204 Ikki's Story I - Road Reconstruction
Ikki's Story I - Road Reconstruction Time Limit: 2000MS Memory Limit: 131072K Total Submissions: 7 ...
- Lattice Reduction (LLL) 算法C代码实现
废话不多说,大名鼎鼎的Lenstra-Lenstra-Lovasz(LLL) 算法.实现参考论文:Factoring Polynomials with Rational Coefficients, 作 ...
- 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 ...
- dp or 贪心 --- hdu : Road Trip
Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...
- Stanford机器学习笔记-10. 降维(Dimensionality Reduction)
10. Dimensionality Reduction Content 10. Dimensionality Reduction 10.1 Motivation 10.1.1 Motivation ...
- HDU 1598 find the most comfortable road(最小生成树之Kruskal)
题目链接: 传送门 find the most comfortable road Time Limit: 1000MS Memory Limit: 32768 K Description XX ...
- CUDA中并行规约(Parallel Reduction)的优化
转自: http://hackecho.com/2013/04/cuda-parallel-reduction/ Parallel Reduction是NVIDIA-CUDA自带的例子,也几乎是所有C ...
- 三分 --- CSU 1548: Design road
Design road Problem's Link: http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1548 Mean: 目的:从(0,0)到 ...
随机推荐
- c# sqlsugar,hisql,freesql orm框架全方位性能测试对比 sqlserver 性能测试
在2022年1月份本人做过一次sqlsugar,hisql,freesql三个框架的性能测试,上次主要是测的sqlserver下的常规插入(非bulkcopy的方式数据插入),hisql与目前比较流行 ...
- RPA应用场景-信用卡交易争议后续流程
RPA应用场景-信用卡交易争议后续流程 场景概述 信用卡交易争议后续流程 所涉系统名称 客服系统,邮件 人工操作(时间/次) 4小时 所涉人工数量20操作频率 不定时 场景流程 1.RPA自动接收客户 ...
- NC16649 [NOIP2005]校门外的树
NC16649 [NOIP2005]校门外的树 题目 题目描述 某校大门外长度为 \(L\) 的马路上有一排树,每两棵相邻的树之间的间隔都是 \(1\) 米.我们可以把马路看成一个数轴,马路的一端在数 ...
- gslb(global server load balance)技术的一点理解
gslb(global server load balance)技术的一点理解 前言 对于比较大的互联网公司来说,用户可能遍及海内外,此时,为了提升用户体验,公司一般会在离用户较近的地方建立机房,来服 ...
- CentOS7下bash升级
[1.查看系统版本][root@web ~]# uname -aLinux web 3.10.0-1160.el7.x86_64 #1 SMP Mon Oct 19 16:18:59 UTC 2020 ...
- CADisplayLink、NSTimer循环引用解决方案
前言:CADisplayLink.NSTimer 循环引用问题 CADisplayLink.NSTimer会对Target产生强引用,如果target又对他们产生强引用,那么就会引发循环引用. @ ...
- Calendar类介绍_获取对象的方式和Calendar类的常用成员方法
java.util.Calendar类:日历类 Calendar类是一个抽象类,里边提供了很多操作日历字段的方法(YEAR.MONTH.DAY_OF_MONTH.HOUR ) Calendar类无法直 ...
- 关于Thymeleaf无法取值问题
SpringBoot2.7以前的版本在获取model中数据的时候不需要注释,2.7以后的版本需要加注释,它无法直接取存在model中的数据,不加注释的时候会爆红但是可以正常使用,这个注释的含义就是指定 ...
- 常用类-Instant、DateTimeFormatter类的使用
一.Instant 我们所处的时间点是在东八区,Java中Instant所计算出来的时间是按本初子午线的时间来算的,与我们的时间相差8个小时,也就是说当我的北京时间是上午九点时,本初子午线的时间是凌晨 ...
- mybatis collection解析以及和association的区别
1.collection标签 说到mybatis的collection标签,我们肯定不陌生,可以通过它解决一对多的映射问题,举个例子一个用户对应多个系统权限,通过对用户表和权限表的关联查询我们可以得到 ...