Codeforces Round #303 (Div. 2) E. Paths and Trees
来源:互联网 发布:黑科技网络验证 编辑:程序博客网 时间:2024/05/17 01:27
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.
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.
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.
3 31 2 12 3 11 3 23
21 2
4 41 2 12 3 13 4 14 1 24
42 3 4
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;}
- Codeforces Round #303 (Div. 2) E. Paths and Trees
- codeforces 545 Round #303 (Div. 2) E Paths and Trees
- Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路
- Codeforces Round #303 (Div. 2) E. Paths and Trees (最短路+变形最小生成树)
- Codeforces Round #303 (Div. 2) - E. Paths and Trees(最短路)
- CodeForces #303E Div.2 Paths and Trees(最短路+拓扑排序)
- CodeForces 545E Paths and Trees
- codeforces 545 E. Paths and Trees
- Codeforces 545E Paths and Trees
- Codeforces Round #169 (Div. 2):E - Little Girl and Problem on Trees
- Codeforces Round #169 (Div. 2) 276E Little Girl and Problem on Trees
- Codeforces Round #169 (Div. 2) E.Little Girl and Problem on Trees(成段更新)
- Codeforces Round #420 (Div. 2) B. Okabe and Banana Trees
- CodeForces 545E Paths and Trees(最短路)
- Codeforces Round #167 (Div. 2) 272E Dima and Horses
- Codeforces Round #206 (Div. 2) E. Vasya and Beautiful Arrays
- Codeforces Round #208 (Div. 2)E. Dima and Kicks
- Codeforces Round #199 (Div. 2) E. Xenia and Tree
- ACCESS中使用存储过程
- .net下BerkeleyDB操作封装C#版(附单元测试)
- OC语法细节
- hdu1162Eddy's picture——最小生成树
- SGI114 Telecasting station
- Codeforces Round #303 (Div. 2) E. Paths and Trees
- qt的关于编译条件的一些定义问题
- Error:Execution failed for task ':app:dexUmengDebug'. > com.android.ide.common.process.ProcessExcept
- Foundation——字符串
- Linux 下修改主机名称
- 【爬虫】爬取百度搜索结果页面
- Ubuntu 14.04.1 for ROS(indigo) by ExBot iso 发行版
- 百度2013年计算机视觉算法研发工程师笔试题
- Foundation——文件操作