题目大意:给出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. 漫谈MySQL primaryKey

    主键没有着明确的概念定义,其是索引的一种,并且是唯一性索引的一种,且必须定义为“PRIMARY KEY”,是只可意会不可言传的东西.下面让我用通俗,甚至有些低俗的语言为您简单介绍一下MySQL的主键. ...

  2. (转)iOS Wow体验 - 第六章 - 交互模型与创新的产品概念(1)

    本文是<iOS Wow Factor:Apps and UX Design Techniques for iPhone and iPad>第六章译文精选,其余章节将陆续放出.上一篇:Wow ...

  3. ZOJ 3430 Detect the Virus 【AC自动机+解码】

    解码的那些事儿,不多说. 注意解码后的结果各种情况都有,用整数数组存储,char数组会超char类型的范围(这个事最蛋疼的啊)建立自动机的时候不能用0来判断结束. #include <cstdi ...

  4. 浪潮服务器通过ipmitool获取mac地址

    一.GPU服务器 #配置两个主板集成千兆四个外插PCI万兆网卡# 板载网卡可以使用命令获取到:RAW 0X30 0X21 就可以读取到第一块网卡的MAC,就是以下返回值的后6位. 0c,c4,7a,5 ...

  5. 安装sql server提示挂起报错

    在安装sql server时出现“以前的某个程序安装已在安装计算机上创建挂起的文件操作.运行安装程序之前必须重新启动计算机”错误.无法进行下去. 参考有关资料后,以下步骤基本可以解决: 1)添加/删除 ...

  6. WebApi2官网学习记录---异常处理

    HttpResponseException 当WebAPI的控制器抛出一个未捕获的异常时,默认情况下,大多数异常被转为status code为500的http response即服务端错误. Http ...

  7. performSelector的方法

    在此我对performSelector系列方法进行了总结 1. - (id)performSelector:(SEL)aSelector; - (id)performSelector:(SEL)aSe ...

  8. vector的用法总结

    Reference Constructors vector Constructs a vector of a specific size or with elements of a specific ...

  9. (原)torch7中添加新的层

    转载请注明出处: http://www.cnblogs.com/darkknightzh/p/6069627.html 参考网址: http://torch.ch/docs/developer-doc ...

  10. ubuntu中安装openssh-server失败(首先ubuntu不能远程root登录)

      ubuntu 安装openssh-server时,报依赖错误   解决方法: 更新软件 sudo apt-get update   如果报校验和不符错误:(此错误为部分网址被墙)如图 解决方法如下 ...