Paths and Trees

time limit per test3 seconds

memory limit per test256 megabytes

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 V is the set of vertices, E is the set of edges). The shortest-path tree from vertex u is such graph G1 = (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 G1 are 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.





题目大概意思就是给定 n 个点, m 条边的无向图和一个点 u,找出若干条边组成一个子图,要求这个子图中 u 到其他点的最短距离与在原图中的相等,并且要求子图所有边的权重和最小,求出最小值。

显然要先跑一次最短路。。。

然后你想一下对于一个点,只要有一条边从一个近一点的点能够转移过来构成他的最短路就够了。。。所以就贪心就好了,找一个最短的边保证可以就好了。。。


#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
struct lpl{
int to, dis, num;
}lin;
struct ld{
int num;
long long dis;
}node[maxn];
vector<lpl> point[maxn];
int n, m, s;
long long ans, dis[maxn];
bool vis[maxn];
queue<int> q; inline void putit()
{
scanf("%d%d", &n, &m);
for(int a, b, i = 1; i <= m; ++i){
scanf("%d%d%d", &a, &b, &lin.dis); lin.num = i;
lin.to = b; point[a].push_back(lin);
lin.to = a; point[b].push_back(lin);
}
scanf("%d", &s);
} inline void spfa()
{
int now, qwe; memset(dis, 0x3f, sizeof(dis)); dis[s] = 0; q.push(s);
while(!q.empty()){
now = q.front(); q.pop(); vis[now] = false;
for(int i = point[now].size() - 1; i >= 0; --i){
qwe = point[now][i].to;
if(dis[qwe] > dis[now] + point[now][i].dis){
dis[qwe] = dis[now] + point[now][i].dis;
if(!vis[qwe]){vis[qwe] = true; q.push(qwe);}
}
}
}
} inline bool cmp(ld A, ld B){return A.dis < B.dis;} inline void workk()
{
for(int i = 1; i <= n; ++i){node[i].num = i; node[i].dis = dis[i];}
sort(node + 1, node + n + 1, cmp);
int t, now, qwe, num;
for(int i = 1; i <= n; ++i){
t = node[i].num; if(t == s) continue;
qwe = 2e9;
for(int j = point[t].size() - 1; j >= 0; --j){
now = point[t][j].to;
if(dis[now] + point[t][j].dis != dis[t]) continue;
if(point[t][j].dis < qwe){
qwe = point[t][j].dis; num = point[t][j].num;
}
}
ans += qwe; vis[num] = true;
}
} inline void print()
{
cout << ans << endl;
for(int i = 1; i <= m; ++i)
if(vis[i]) printf("%d ", i);
} int main()
{
putit();
spfa();
workk();
print();
return 0;
}

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

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

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

  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 545E] Paths and Trees

    [题目链接] https://codeforces.com/contest/545/problem/E [算法] 首先求 u 到所有结点的最短路 记录每个节点最短路径上的最后一条边         答 ...

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

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

  8. 「日常训练」Paths and Trees(Codeforces Round 301 Div.2 E)

    题意与分析 题意是这样的,定义一个从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 现在求一棵最短生成树,输出总边权和与选取边的编号. 我们首先要明白这样一个结论:对一个图求 ...

  9. Codeforces 545E. Paths and Trees[最短路+贪心]

    [题目大意] 题目将从某点出发的所有最短路方案中,选择边权和最小的最短路方案,称为最短生成树. 题目要求一颗最短生成树,输出总边权和与选取边的编号.[题意分析] 比如下面的数据: 5 5 1 2 2 ...

随机推荐

  1. PHP-不涉及第三个变量交换两个变量的值

    不涉及第三个变量交换两个变量的值 方法1:使用加减法 <?php $a = 1; $b = 2; $a = $a+$b; $b = $a-$b; $a = $a-$b; printf(" ...

  2. 记一次redis读取超时的排查过程(SADD惹的祸)

    问题背景 在业务使用redis过程中,出现了read timeout 的异常. 问题排查 直接原因 运维查询redis慢查询日志,发现在异常时间节点,有redis慢查询日志,执行sadd 命令花费了1 ...

  3. python断言语句assert

    断言语句的格式 assert test, [msg] test是一个表达式,表达式求值为Fals时引发AssertionError异常,msg是可选的异常消息. def test_assert(a): ...

  4. windows java jdk安装

    安装 下载地址:https://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 勾选协议,选择 ...

  5. Uva10491 Cows and Cars 【迁移自洛谷博客】

    题目大意 假设有a头牛,b辆车(门的总数为a+b),你先选一个门,然后你最终选择前主持人会替你打开C扇有牛的门(不会打开你已经选择的门),问你要不要换门,输出"总是换门"的策略下, ...

  6. 如何使用Excel绘制甘特图

    摘自:http://www.mifengtd.cn/articles/how-to-create-a-gantt-chart-in-excel.html 再造<优秀的时间管理和项目管理工具> ...

  7. paper 167:GPU的使用Theano之tutorial

    Theano之使用GPU 英文版本:http://deeplearning.net/software/theano/tutorial/using_gpu.html          using the ...

  8. LOJ 3094 「BJOI2019」删数——角标偏移的线段树

    题目:https://loj.ac/problem/3094 弱化版是 AGC017C . 用线段树维护那个题里的序列即可. 对应关系大概是: 真实值的范围是 [ 1-m , n+m ] :考虑设偏移 ...

  9. 【靶场练习_sqli-labs】SQLi-LABS Page-1(Basic Challenges)

    GET篇 Less-1:  1.用order by得出待查表里有三个字段 http://192.168.40.165/sqli-labs-master/Less-1/?id=1' order by 3 ...

  10. Houdini学习笔记——【案例二】消散文字制作

    [案例二]Houdini消散文字制作 一.Overview     文字通过时间轴中frame变化而碎裂从两边开始向着中间消散并向镜头移动. 效果 二.Sop(Surface OPerators or ...