Codeforces Round #303 (Div. 2) E. Paths and Trees

来源:互联网 发布:黑科技网络验证 编辑:程序博客网 时间:2024/05/17 01:27
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 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·1050 ≤ 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 1in 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.

Sample test(s)
input
3 31 2 12 3 11 3 23
output
21 2 
input
4 41 2 12 3 13 4 14 1 24
output
42 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.

题目要求出从源结点到每个结点都是最短路的最小生成树,先用最短路算法求最短路,再保证,是最小边权优先就可以了!用dij算法,再用链接表

堆优化即可

#include "stdafx.h"#include "stdio.h"#include "string.h"#include "math.h"#include <algorithm>#include <vector>#include <queue>#include <iostream>using namespace std;#define N 300005#define SCANF scanf_sstruct node {int s, e,w,index;node(int ss,int ee,int ww,int indexx){s = ss;e = ee;w = ww;index = indexx;}};struct qnode{int index, v,w;long long d;qnode(int vv, long long dd, int indexx,int ww){v = vv;d = dd;w = ww;index = indexx;}};vector<node> edges[N];long long weight;int x[N], h[N];long long dist[N];bool vis[N];int ans[N];int anscount = 0;struct cmp{bool operator()(qnode a, qnode b){if (a.d == b.d){if (a.w != b.w)return a.w > b.w;elsereturn a.index > b.index;}return a.d> b.d;}};priority_queue < qnode, vector<qnode>, cmp> q;void dij(){while (!q.empty()){qnode  top = q.top();q.pop();if (!vis[top.v]){vis[top.v] = true;weight += top.w;ans[anscount++] = top.index;for (int i = 0; i < edges[top.v].size(); i++){int e = edges[top.v][i].e;if (dist[e] == -1 ||top.d + edges[top.v][i].w <= dist[e]){dist[e] = top.d + edges[top.v][i].w;q.push(qnode(e, dist[e], edges[top.v][i].index, edges[top.v][i].w));}}}}}int main(){int n,sum,m,s,e,w;while (SCANF("%d%d", &n,&m) != EOF){for (int i = 0; i <= n; i++){edges[i].clear();vis[i] = false;dist[i] = -1;}while (!q.empty()){q.pop();}for (int i = 0; i < m; i++){SCANF("%d%d%d", &s, &e, &w);edges[s].push_back(node(s,e,w,i + 1));edges[e].push_back(node(e,s,w,i + 1));}SCANF("%d",&s);anscount = 0;weight = 0;dist[s] = 0;q.push(qnode(s,0,-1,0));dij();cout << weight << endl;for (int i = 1; i < anscount; i++){if (i == 1)cout << ans[i];else cout<< " " << ans[i] ;}if (anscount > 1)cout << endl;}return 0;}


0 0