E. Paths and Trees
time limit per test

3 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Little girl Susie accidentally found her elder brother's notebook. She has many things to do, more important than solving problems, but she found this problem too interesting, so she wanted to know its solution and decided to ask you about it. So, the problem statement is as follows.

Let's assume that we are given a connected weighted undirected graph G = (V, E) (here Vis the set of vertices, E is the set of edges). The shortest-path tree from vertex u is such graphG1 = (V, E1) that is a tree with the set of edges E1 that is the subset of the set of edges of the initial graph E, and the lengths of the shortest paths from u to any vertex to G and to G1are the same.

You are given a connected weighted undirected graph G and vertex u. Your task is to find the shortest-path tree of the given graph from vertex u, the total weight of whose edges is minimum possible.

Input

The first line contains two numbers, n and m (1 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of vertices and edges of the graph, respectively.

Next m lines contain three integers each, representing an edge — ui, vi, wi — the numbers of vertices connected by an edge and the weight of the edge (ui ≠ vi, 1 ≤ wi ≤ 109). It is guaranteed that graph is connected and that there is no more than one edge between any pair of vertices.

The last line of the input contains integer u (1 ≤ u ≤ n) — the number of the start vertex.

Output

In the first line print the minimum total weight of the edges of the tree.

In the next line print the indices of the edges that are included in the tree, separated by spaces. The edges are numbered starting from 1 in the order they follow in the input. You may print the numbers of the edges in any order.

If there are multiple answers, print any of them.

Examples
input
3 3
1 2 1
2 3 1
1 3 2
3
output
2
1 2
input
4 4
1 2 1
2 3 1
3 4 1
4 1 2
4
output
4
2 3 4
Note

In the first sample there are two possible shortest path trees:

  • with edges 1 – 3 and 2 – 3 (the total weight is 3);
  • with edges 1 – 2 and 2 – 3 (the total weight is 2);

And, for example, a tree with edges 1 – 2 and 1 – 3 won't be a shortest path tree for vertex 3, because the distance from vertex 3 to vertex 2 in this tree equals 3, and in the original graph it is 1.

题目:一个带权无向图,求一个新图G’=(V,E’),使得源点s到新图各个点的最短距离等于在原图中的最短距离,输出边权值最小的新图;

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long Ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int inf =0x7f7f7f7f;
const double pi=acos(-1);
const int maxn=300000; int used[maxn+10],cost[maxn+10],flag[maxn+10],vis[maxn+10];
ll dis[maxn+10]; struct Edge{
int to,w,id;
};
vector<Edge> G[maxn+10]; struct node{
int to;
ll dis;
bool operator <(const node a) const{
return this->dis>a.dis;
}
};
int main()
{
int n,m,u,v,w,s;
while(~scanf("%d %d",&n,&m))
{
for(int i=1;i<=m;i++)
{
scanf("%d %d %d",&u,&v,&w);
G[u].push_back((Edge){v,w,i});
G[v].push_back((Edge){u,w,i});
} scanf("%d",&s);
for(int i=1;i<=n;i++)
{
dis[i]=1e16;
cost[i]=1e9+10;
flag[i]=0;
used[i]=0;
}
priority_queue<node> q;
q.push((node){s,0});
dis[s]=0;
while(q.size())
{
node cur=q.top();q.pop();
if(dis[cur.to]<cur.dis) continue;
int u=cur.to;
used[u]=1;
for(int i=0;i<G[u].size();i++)
{
Edge e=G[u][i];
if(used[e.to]) continue;
if(dis[e.to]>dis[u]+e.w)
{
dis[e.to]=dis[u]+e.w;
flag[e.to]=e.id;
cost[e.to]=e.w;
q.push((node){e.to,dis[e.to]});//q要放在更新函数内,否则会爆优先队列
}
else if(dis[e.to]==dis[u]+e.w&&cost[e.to]>e.w)
{
cost[e.to]=e.w;
flag[e.to]=e.id;
q.push((node){e.to,dis[e.to]});
}
}
} ll ans=0;
for(int i=1;i<=n;i++)
if(i!=s) ans+=cost[i];
printf("%lld\n",ans);
for(int i=1;i<=n;i++)
if(i!=s) printf("%d ",flag[i]);
printf("\n");
}
return 0;
}

 分析:神奇的一道题目,

1.需要使用堆优化的Dijkstra求一遍最短路;

2.贪心:在求最短路的过程中,假如起点到其余各点只有一条最短路的话,那么显然就是这些最短路

组成的图,但是假如到达同一个点有多条最短路的话,那么就要进行贪心,比如样例中的2和3号节点都可以

最短路到达1,但是因为2与1直接相连的那条边权值要小,所以就选走2这条路的。

3.其实求最后的起点到每个节点的最短路,就是一棵树

Codeforces Round #303 (Div. 2) E. Paths and Trees Dijkstra堆优化+贪心(!!!)的更多相关文章

  1. 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 ...

  2. 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 ...

  3. 水题 Codeforces Round #303 (Div. 2) D. Queue

    题目传送门 /* 比C还水... */ #include <cstdio> #include <algorithm> #include <cstring> #inc ...

  4. DP Codeforces Round #303 (Div. 2) C. Woodcutters

    题目传送门 /* 题意:每棵树给出坐标和高度,可以往左右倒,也可以不倒 问最多能砍到多少棵树 DP:dp[i][0/1/2] 表示到了第i棵树时,它倒左或右或不动能倒多少棵树 分情况讨论,若符合就取最 ...

  5. 贪心 Codeforces Round #303 (Div. 2) B. Equidistant String

    题目传送门 /* 题意:找到一个字符串p,使得它和s,t的不同的总个数相同 贪心:假设p与s相同,奇偶变换赋值,当是偶数,则有答案 */ #include <cstdio> #includ ...

  6. 水题 Codeforces Round #303 (Div. 2) A. Toy Cars

    题目传送门 /* 题意:5种情况对应对应第i或j辆车翻了没 水题:其实就看对角线的上半边就可以了,vis判断,可惜WA了一次 3: if both cars turned over during th ...

  7. Codeforces Round #303 (Div. 2)

    A.Toy Cars 题意:给出n辆玩具车两两碰撞的结果,找出没有翻车过的玩具车. 思路:简单题.遍历即可. #include<iostream> #include<cstdio&g ...

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

    题意 求一个生成树,使得任意点到源点的最短路等于原图中的最短路.再让这个生成树边权和最小. http://codeforces.com/contest/545/problem/E 思路 先Dijkst ...

  9. Codeforces Round #303 (Div. 2) D. Queue 傻逼题

    C. Woodcutters Time Limit: 20 Sec  Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/545/probl ...

随机推荐

  1. Java中集合关键字的区别

    1. ArrayList.Vector和Stack有什么区别? 1.ArrayList的方法和实现基本上和Vector一样,底层都是数组的实现(简:API基本一样) ​   2.Stack继承了Vec ...

  2. Splunk初识

    目录 网址汇总 注册与下载 安装 使用 中文环境 关于APP Splunk自带的APP 创建自己的APP 添加数据 本地文件添加 通过监视添加数据 自定义列 查询语句 SPL 与 SQL对照 命令查找 ...

  3. ef core 动态拼接 条件

    var sql = new List<string>(); var sqlparams = new List<string>(); ; foreach (var p in ph ...

  4. Java基础(四)

    概述 常用快捷键 数组 概述 数组是一种引用类型.变量只可以存放一个数据,数组则可以存放多个类型统一的数据,可以存放基本类型,也可以存放引用类型. 如果需要存储的数据很多,那么定义多个变量很麻烦: I ...

  5. 安装linux mint后要做20件事

    Linux Mint 17 Qiana Cinnamon Linux Mint 17已经发布,定名为Qiana.Mint是Linux最佳发行版之一,它定位于桌面用户,关注可用性和简洁.它携带了风格迥异 ...

  6. fullpage实现(-)

    在线demo还没弄好,地址先给出来

  7. H5 拍照图片旋转、压缩和上传

    原文地址:github.com/whinc/blog/… 最近接到一个“发表评论”的需求:用户输入评论并且可以拍照或从相册选择图片上传,即支持图文评论.需要同时在 H5 和小程序两端实现,该需求处理图 ...

  8. 在vue的js文件引入自定义js文件

    自定义js var provinces=[] export default provinces vue页面js引入 import riskLeft from "./index.js" ...

  9. roll out项目中原系统自定义程序不能编辑

    1.SE38编辑时报 程序源系统与现阶段系统不一致:Carry out repairs in non-original systems only if urgent 2.SE16N 查看表TADIR, ...

  10. kafka消费之kafka查看topic是否有数据

    通过原生kafka,查看对应的topic是否有数据 下载kafka:登录Apache kafka官方下载 http://kafka.apache.org/downloads.html 配置config ...