Codeforces Round #303 (Div. 2) E. Paths and Trees
2015-05-22 21:11
253 查看
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·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 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
output
input
output
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_s
struct 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
;
long long weight;
int x
, h
;
long long dist
;
bool vis
;
int ans
;
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;
else
return 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;
}
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·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 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 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.
题目要求出从源结点到每个结点都是最短路的最小生成树,先用最短路算法求最短路,再保证,是最小边权优先就可以了!用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_s
struct 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
;
long long weight;
int x
, h
;
long long dist
;
bool vis
;
int ans
;
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;
else
return 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 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 Round #303 (Div. 2) E. Paths and Trees 最短路+贪心
- Codeforces Round #303 (Div. 2)E. Paths and Trees 最短路
- 【Codeforces Round #420 (Div. 2) B】Okabe and Banana Trees
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D Bear and Two Paths
- CodeForces 545E Paths and Trees(最短路)
- Codeforces Round #420 (Div. 2) B. Okabe and Banana Trees
- CodeForces #303E Div.2 Paths and Trees(最短路+拓扑排序)
- 【Codeforces Round 331 (Div 2)D】【DP 记忆化搜索 期望DP区间性质 好题】Wilbur and Trees 砍树随机从右从左概率左倒右倒的期望底面覆盖长度
- Codeforces Round #169 (Div. 2) E.Little Girl and Problem on Trees(成段更新)
- Little Girl and Problem on Trees - CodeForces #169 (Div. 2) E 树状数组
- Codeforces Round #312 (Div. 2)-A. Lala Land and Apple Trees-水题-模拟
- Codeforces Round #312 (Div. 2) A. Lala Land and Apple Trees
- Codeforces Round #169 (Div. 2) E. Little Girl and Problem on Trees(给出一棵树,对距离某个点d范围之内的所有点+x,查询某个点的值)
- Codeforces Round #351 (VK Cup 2016 Round 3, Div. 2 Edition) D. Bear and Two Paths
- Codeforces#303-E - Paths and Treesg-最短路+最小生成树
- E. Paths and Trees (CF 303 div2)最短路