题目大意:给出n个点,m条无向边,每条边有长度。求一棵树,要求树上的每个点到源点距离最小的前提下,使得树上的边的长度和最小。输出树上边的总长度,以及树上的边的序号(按输入顺序 1...m).

思路 :单源最短路径 + 贪心 .用Dijkstra 或spfa 算法 求每个点到源点的最短路径,并在记录当前用来更新每个点最短距离的那条边(即pre[i]记录点i所对应的变)。

    若当前这条边能使得某个点到源点的距离变小则更新该点到源点的距离,并记录更新该点的边; 若当前这条边更新某节点后,使得该点到源点的距离不变(即:d[e[t].to] = d[e[t].from] + e[t].length ) 则比较这条边与当前该点所记录的边的长度大小,若这条边比改点所记录的边长度要小(即e[pre[e[t].to]].length > e[t].length),则更新改点所记录的边(pre[e[t].to] = t)。

代码 :

  1. #include<stdio.h>
    #include<iostream>
    #include<string.h>
    #include<stdlib.h>
    #include<queue>
    #define INF 0x7fffffffffffffff
    #define pii pair<long long,long long>
    using namespace std;
    long long s,n,m,pre[300002],tag[600002],u[600002],v[600002],next[600002],first[300002];
    long long sum,d[300002],w[600002];
    priority_queue<pii,vector<pii>,greater<pii> > Q;
    bool vis[300002]; void input(){
    long long i,j,a,b,c;
    for(i=1;i<=n;i++)
    first[i] = -1;
    for(i=1;i<=m;i++){
    scanf("%I64d %I64d %I64d",&u[i],&v[i],&w[i]);
    u[i+m] = v[i] ;
    v[i+m] = u[i] ;
    w[i+m] = w[i] ;
    next[i] = first[u[i]];
    first[u[i]] = i ;
    next[i+m] = first[v[i]];
    first[v[i]] = i+m ;
    }
    scanf("%I64d",&s);
    } void Dijkstra(){
    long long i,j;
    memset(vis,0,sizeof(vis));
    memset(tag,0,sizeof(tag));
    for(i=1;i<=n;i++)
    pre[i] = -1 ;
    for(i=1;i<=n;i++)
    d[i] = INF;
    d[s] = 0;
    Q.push(make_pair(d[s],s));
    while(!Q.empty()){
    pii t = Q.top();
    Q.pop();
    long long x = t.second;
    if(vis[x])
    continue;
    vis[x] = true;
    for(long long e=first[x]; e!=-1; e=next[e]){
    if(d[v[e]] > d[x] + w[e] && d[x] < INF){
    pre[v[e]] = e;
    d[v[e]] = d[x] + w[e] ;
    Q.push(make_pair(d[v[e]],v[e]));}elseif(d[v[e]]== d[x]+ w[e]&& d[x]< INF && w[pre[v[e]]]> w[e]){
    pre[v[e]]= e;
    Q.push(make_pair(d[v[e]],v[e]));}}}}int main(){longlong i,j;while(scanf("%I64d%I64d",&n,&m)==2){
    input();
    sum =0;Dijkstra();for(i=1;i<=n;i++){
    sum += w[pre[i]];
    tag[pre[i]]=1;} printf("%I64d\n",sum);longlong count =0;for(i=1;i<=2*m;i++)if(tag[i]){if(i > m){if(count !=0)
    printf(" %I64d",i-m);else
    printf("%I64d",i-m);}elseif(i<=m){if(count !=0)
    printf(" %I64d",i);else
    printf("%I64d",i);}
    count ++;}
    printf("\n");}return0;}

CF 545E Paths and Trees的更多相关文章

  1. Codeforces 545E. Paths and Trees 最短路

    E. Paths and Trees time limit per test: 3 seconds memory limit per test: 256 megabytes input: standa ...

  2. 545E. Paths and Trees

    题目链接 题意:给定一个无向图和一个点u,找出若干条边组成一个子图,要求这个子图中u到其他个点的最短距离与在原图中的相等,并且要求子图所有边的权重之和最小,求出最小值并输出子图的边号. 思路:先求一遍 ...

  3. Codeforces 545E. Paths and Trees[最短路+贪心]

    [题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...

  4. [Codeforces 545E] Paths and Trees

    [题目链接] https://codeforces.com/contest/545/problem/E [算法] 首先求 u 到所有结点的最短路 记录每个节点最短路径上的最后一条边         答 ...

  5. codeforces 545E E. Paths and Trees(单源最短路+总权重最小)

    E. Paths and Trees time limit per test:3 seconds memory limit per test:256 megabytes input:standard ...

  6. Codeforces Round #303 (Div. 2) E. Paths and Trees 最短路+贪心

    题目链接: 题目 E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes inputs ...

  7. Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路

    E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  8. Codeforces Round #303 (Div. 2) E. Paths and Trees Dijkstra堆优化+贪心(!!!)

    E. Paths and Trees time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  9. Codeforces Paths and Trees

    Paths and Trees time limit per test3 seconds memory limit per test256 megabytes Little girl Susie ac ...

随机推荐

  1. POJ2828---线段树与逆序数&&DUTOJ1210---逆序对构造排列

    来看这样一道问题:http://acm.dlut.edu.cn/problem.php?id=1210 题目大意:对于一个1-n的排列,a1,a2,a3,a4...an我们把满足i < j,ai ...

  2. ShellSort Shell排序

    希尔排序(Shell Sort)又称为“缩小增量排序”.是1959年由D.L.Shell提出来的.该方法的基本思想是:先将整个待排元素序列分割成若干个子序列(由相隔某个“增量”的元素组成的)分别进行直 ...

  3. 一台机器同时运行多个appium实例

    测试需要同时在多个android设备上运行,就需要启动多个appium 第一台是运行微信: DesiredCapabilities capabilities = new DesiredCapabili ...

  4. Win32多线程编程(1) — 基础概念篇

      内核对象的基本概念 Windows系统是非开源的,它提供给我们的接口是用户模式的,即User-Mode API.当我们调用某个API时,需要从用户模式切换到内核模式的I/O System Serv ...

  5. Div与table的区别

    1:速度和加载方式方面的区别 div 和 table 的差异不是速度,而是加载方式,速度只能是指网络速度,如果速度足够快,是没有差异的: div 的加载方式是即读即加载,遇到 <div> ...

  6. avalon学习笔记一 列表及条件过滤

    好长时间都没有更新博客了,不是因为没有学习新的东西,而是到了新的单位每天玩命加班实在是太累了!经过一年的努力吧,终于可以轻松一下了.废话少说,直接干货吧! 由于是学习阶段,就直接拿了公司的二级页面做了 ...

  7. Property与Attribute的区别

    Property属于面向对象的范畴----属性 Attribute则是编程语言文法层面的东西----特征          Property属于面向对象的范畴.在使用面向对象编程的时候,常常需要对客观 ...

  8. Markdown 学习笔记: Basics

    Markdown 学习笔记: Basics 原文:Basics. 了解Markdown格式化句法的要点 本页对如何使用Markdown提供了一个简单的概述.在"句法"页中对Mark ...

  9. Arcgis Engine - 脱离ToolBarControl控件的命令和工具

    可以手动实现脱离ToolBarControl控件的命令和工具 //打开文件. private void file_tsmItem_Click(object sender, EventArgs e) { ...

  10. 常用SQL语句学习整理

    增 insert into table_name (column_name1,column_name2) values (value1,value2) 删 delete from table_name ...