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. IIS版本与Windows Server版本对应关系

    IIS 6.0随着Windows XP Professional 64位和Windows Server 2003发布. IIS 7.0随着Windows Vista和Windows Server 20 ...

  2. QT与DoNet中单例模式的简单实现

    由于使用场景的不同,单例模式的写法也有所区别. 目前接触到的,大多数都是多线程,大量数据处理,还要考虑灵活性,对原有类结构改动最小等因素,所以写法更是多种多样. QT个人较常用的一种写法:(两个文件: ...

  3. bat-设置oracle服务

    1.停止oracle所有服务 并将服务设置为手动启动 @echo off echo oracle服务--------停止 net stop OracleVssWriterORCL net stop O ...

  4. vmware修改虚拟机网卡mac地址

    选中"虚拟机" 右键 "设置",然后选中"网络适配器",然后点击"高级",设置"MAC地址"

  5. 毕设着急了吧?Python股票数据分析,制作动态柱状图

    写在前面的一些屁话: 雪球成立于 2010 年,是北京雪球信息科技有限公司旗下推出的投资者社区.雪球一直致力于为中国投资者提供跨市场(沪深.香港.美国),跨品种(股票.基金.债券等)的数据查询.资讯获 ...

  6. warning: setlocale: LC_ALL: cannot change locale (en_US.UTF-8): No such file or directory

    1)apt-get clean && apt-get update && apt-get install -y locales 2)locale-gen en_US.U ...

  7. Docker安装及基础命令

    一.docker安装 1.安装docker #关闭防火墙和selinux systemctl stop firewalld.service setenforce 0  ​ #安装依赖包 yum ins ...

  8. shell脚本三剑客:grep、sed、awk

    shell脚本三剑客:grep  sed  awk grep语法: grep [OPTIONS] PATTERN [FILE] 常用选项 -c                       统计匹配到的 ...

  9. VS Code + GitHub

    来到博客园学着别人美化了一下自己的博客页面,蛮好看的,然后右上角有一个"Fork me on GitHub".之前就因为好奇而注册过GitHub,但一直不会使,现在正式开始编程学习 ...

  10. 加强版:合并果子[NOIP2004]

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