题目大意:给出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. WPF 控件之ComboBox绑定[2]

    最近感觉新的方法Binding comboBox用起来很好用. 记录一下: <ComboBox Grid.Row=" x:Name="cboFamilyName" ...

  2. Firefox历史版本下载

    http://ftp.mozilla.org/pub/firefox/releases/ http://ftp.mozilla.org/pub/firefox/releases/47.0.1/

  3. python3-day5(模块)

    1.获取路径import os,sys #获取全部路径 print(os.path.abspath(__file__)) #获取目录 print(os.path.dirname(os.path.abs ...

  4. kafka-分布式消息系统

    消息中间件MessageQuene 解耦且可扩展:业务复杂度的提升带来的也是耦合度的提高,消息队列在处理过程中间插入了一个隐含的.基于数据的接口层,两边的处理过程都要实现这一接口.这允许你独立的扩展或 ...

  5. ASP.NET 实现PDF文件下载

    本文介绍了一种在ASP.NET中下载文件的方法. 方法一:可能是最简单的.最短的方式: Response.ContentType = "application/pdf"; Resp ...

  6. RPG JS跨平台测试

    RPG JS虽然说是跨平台的,但是在具体的测试中效果并不理想. 以官方提供的Demo为例 问题一 手机的屏幕太小,导致画面上的人物都很小,连点击都很不准确.在9寸的平板上才可以看得比较清楚. 问题二 ...

  7. MySQL具体解释(7)-----------MySQL线程池总结(一)

    线程池是Mysql5.6的一个核心功能.对于server应用而言,不管是web应用服务还是DB服务,高并发请求始终是一个绕不开的话题.当有大量请求并发訪问时,一定伴随着资源的不断创建和释放.导致资源利 ...

  8. 一张图搞懂 Javascript 中的原型链、prototype、__proto__的关系 转载加自己的总结

    1. JavaScript内置对象 所谓的内置对象 指的是:JavaScript本身就自己有的对象 可以直接拿来就用.例如Array String 等等.JavaScript一共有12内置对象    ...

  9. FileZilla 安装配置参考

    http://www.admin10000.com/document/72.html 解决 NAT issue https://wiki.filezilla-project.org/Network_C ...

  10. jquery选择指定元素之外的所有元素

    最近的项目中有这么一个需求,点击一排图片中的任意一张后底部弹出一个对话框,要求点击任意地方隐藏对话框 这个时候用not()显然是不现实的,用closest()可以实现差不多的功能 <!DOCTY ...