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

#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. JVM 内存机制理解【转自http://www.cnblogs.com/dingyingsi/p/3760447.html】

    我们知道,计算机CPU和内存的交互是最频繁的,内存是我们的高速缓存区,用户磁盘和CPU的交互,而CPU运转速度越来越快,磁盘远远跟不上CPU的读写速度,才设计了内存,用户缓冲用户IO等待导致CPU的等 ...

  2. oracle 数据导入、导出

    导入导出 --数据导出备份和导入 ------注意 导出和导入 必须是CMD 命令行下操作,而不是SQL编辑器中 --1.导出表 . --exp:导出关键字 ,userid:用户权限 ,file:保存 ...

  3. .net excel 导入 导出

    哎,好好的代码今天说来个实验,结果用的是office15 气死人了,网上最高office14.dll 文章转自2012年 QQ群:13615607 MR.Young protected void Bt ...

  4. AJPFX总结抽象类和接口的区别

    /*                 * 抽象类和接口的区别                 *                 1.成员的区别                         *   ...

  5. [ CQOI 2018 ] 异或序列

    \(\\\) Description 给出一个长为 \(n\) 的数列 \(A\) 和 \(k\),多次询问: 对于一个区间 \([L_i,R_i]\),问区间内有多少个不为空的子段异或和为 \(k\ ...

  6. css:段落文本两端对齐

    效果图: Css:

  7. ios 从相册视频中获取视频截图

    //给image添加个分类 +(UIImage *)getImage:(NSURL: *)videoURL { AVURLAsset *asset = [[AVURLAsset alloc] init ...

  8. Oracle逻辑备份与恢复(Data Pump)

    1. 备份的类型 按照备份方式的不同,可以把备份分为两类: 1.1 逻辑备份:指通过逻辑导出对数据进行备份.将数据库中的用户对象导出到一个二进制文件中,逻辑备份使用导入导出工具:EXPDP/IMPDP ...

  9. java实现汉诺塔算法

    package com.ywx.count; import java.util.Scanner; /** * @author Vashon * date:20150410 * * 题目:汉诺塔算法(本 ...

  10. SAS Fuctions

    1. monotonic(), 单调递增函数.返回一列变量的序列等,类似于_N_ . 2. index v indexw: INDEX Function Searches a character ex ...