赤裸裸的最短路,需要注意下枚举过程就好了。直接贴上别人的代码,发现他的代码挺符合我的风格,以后就这样写了。

#include <bits/stdc++.h>

using namespace std;

struct node{
    int y,z,id;
    node(){}
    node(int y,int z,int id):y(y),z(z),id(id){}
};
vector<node> e[]; int n,m,u;
int f[];
int ed[];
long long dis[];
vector<int> ans;
const long long inf = (long long)*; int main(){
    cin >>n >>m;
    for (int i=;i<=m;i++){
        int x,y,z;
        cin >> x>>y>>z;
        ed[i]=z;
        e[x].push_back(node(y,z,i));
        e[y].push_back(node(x,z,i));
    }
    cin >> u;
    queue<int> q;
    q.push(u);
    for (int i=;i<=n;i++) dis[i]=inf;
    //for (int i=1;i<=n;i++) cout<<dis[i]<<endl;
    dis[u]=;
    while (!q.empty()){
        int now = q.front();
        q.pop();
        for (int i=;i<e[now].size();i++){
            int v = e[now][i].y;
            if (dis[now]+e[now][i].z<dis[v] || dis[now]+e[now][i].z==dis[v] && e[now][i].z<ed[f[v]]){
                f[v]=e[now][i].id;
                dis[v]=dis[now]+e[now][i].z;
                q.push(v);
            }
        }
    }
    long long cnt=;
    for (int i=;i<=n;i++)
        if (f[i]!=) cnt+=ed[f[i]],ans.push_back(f[i]);
    sort(ans.begin(),ans.end());
    cout << cnt<<endl;
    for(int i=;i<ans.size();i++){
        if (i>) cout<<" ";
        cout << ans[i];
    }
    return ;

}

codeforces 303 div2 E的更多相关文章

  1. Codeforces #180 div2 C Parity Game

    // Codeforces #180 div2 C Parity Game // // 这个问题的意思被摄物体没有解释 // // 这个主题是如此的狠一点(对我来说,),不多说了这 // // 解决问 ...

  2. Codeforces #541 (Div2) - E. String Multiplication(动态规划)

    Problem   Codeforces #541 (Div2) - E. String Multiplication Time Limit: 2000 mSec Problem Descriptio ...

  3. Codeforces #541 (Div2) - F. Asya And Kittens(并查集+链表)

    Problem   Codeforces #541 (Div2) - F. Asya And Kittens Time Limit: 2000 mSec Problem Description Inp ...

  4. Codeforces #541 (Div2) - D. Gourmet choice(拓扑排序+并查集)

    Problem   Codeforces #541 (Div2) - D. Gourmet choice Time Limit: 2000 mSec Problem Description Input ...

  5. Codeforces #548 (Div2) - D.Steps to One(概率dp+数论)

    Problem   Codeforces #548 (Div2) - D.Steps to One Time Limit: 2000 mSec Problem Description Input Th ...

  6. 【Codeforces #312 div2 A】Lala Land and Apple Trees

    # [Codeforces #312 div2 A]Lala Land and Apple Trees 首先,此题的大意是在一条坐标轴上,有\(n\)个点,每个点的权值为\(a_{i}\),第一次从原 ...

  7. Codeforces #263 div2 解题报告

    比赛链接:http://codeforces.com/contest/462 这次比赛的时候,刚刚注冊的时候非常想好好的做一下,可是网上喝了个小酒之后.也就迷迷糊糊地看了题目,做了几题.一觉醒来发现r ...

  8. codeforces #round363 div2.C-Vacations (DP)

    题目链接:http://codeforces.com/contest/699/problem/C dp[i][j]表示第i天做事情j所得到最小的假期,j=0,1,2. #include<bits ...

  9. codeforces round367 div2.C (DP)

    题目链接:http://codeforces.com/contest/706/problem/C #include<bits/stdc++.h> using namespace std; ...

随机推荐

  1. 释放资源的一般范式——更锋利的C#代码小记

    public class Photo : IDisposable { //在Finalize函数中调用内部的Dispose方法 ~Photo() { //被自动回收时仅释放托管资源,不释放非托管资源 ...

  2. lock和synchronized的同步区别与选择

    1. lock是一个接口,而synchronized是java的一个关键字,synchronized是内置的语言实现:(具体实现上的区别在<Java虚拟机>中有讲解底层的CAS不同,以前有 ...

  3. 【HEVC帧间预测论文】P1.6 A Fast HEVC Inter CU Selection Method Based on Pyramid Motion Divergence

    A Fast HEVC Inter CU Selection Method Based on Pyramid Motion Divergence <HEVC标准介绍.HEVC帧间预测论文笔记&g ...

  4. sql查询作业执行时间

    SELECT  j.name                        AS Job_Name        ,        h.step_id                     AS S ...

  5. 获取父页面的dom元素

    $("li.jericho_tabs", window.top.document); 上面的代码意思是获取父页面的li元素,class为jericho_tabs的所有元素.

  6. 事件捕获 & 事件冒泡

    <body> <div id="div1"> <div id="div2"> <div id="div3&q ...

  7. libs/tools.js stringToDate dateToString 日期字符串转换函数

    libs/tools.js stringToDate dateToString 日期字符串转换函数 import { stringToDate } from '@/libs/tools.js'   e ...

  8. aapt环境变量配置

    D:\android-sdk_r24.4.1-windows\android-sdk-windows\build-tools\28.0.2 将aapt路径添加到path中, 打开cmd 输入aapt

  9. jxcel - 好用的Excel与Java对象转换工具

    更多精彩博文,欢迎访问我的个人博客 Jxcel简介 Jxcel是一个支持Java对象与Excel(目前仅xlsx.xls)互相转换的工具包. 项目地址:https://github.com/jptan ...

  10. python:第一章

    完成同一个任务,C语言要写1000行代码,Java只需要写100行,而Python可能只要20行. 代码少的代价是运行速度慢,C程序运行1秒钟,Java程序可能需要2秒,而Python程序可能就需要1 ...