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 G 1 = (V, E 1) that is a tree with the set of edges E 1 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 G 1 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 — u i, v i, w i — the numbers of vertices connected by an edge and the weight of the edge ( u i ≠ v i, 1 ≤ w i ≤ 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.
<코드>
#include <vector>
#include <queue>
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
class tpl {
public:
int w, b, en;
};
vector<tpl> Edges[300005];
bool visit[300005];
long long L[300005];
int pre[300005];
int wei[300005];
int en[300005];
int ans[300005];
long long ansL;
class pkt
{
public:
long long l;
int w, a, b, en;
bool operator < (const pkt& k) const
{
return l > k.l;
}
};
priority_queue<pkt> Q;
void dijkstra(int s)
{
pkt p, q;
int i;
p.l = 0;
p.w = 0;
p.a = 0;
p.b = s;
Q.push(p);
while (!Q.empty())
{
p = Q.top();
Q.pop();
if (visit[p.b] == false) {
if (wei[p.b] > p.w) {
wei[p.b] = p.w;
pre[p.b] = p.a;
en[p.b] = p.en;
}
}
else if (L[p.b] == p.l){
if (wei[p.b] > p.w) {
wei[p.b] = p.w;
pre[p.b] = p.a;
en[p.b] = p.en;
}
}
if (visit[p.b]) continue;
visit[p.b] = true;
L[p.b] = p.l;
pre[p.b] = p.a;
wei[p.b] = p.w;
en[p.b] = p.en;
for (i = 0; i < Edges[p.b].size(); i++) {
q.l = L[p.b] + Edges[p.b][i].w;
q.w = Edges[p.b][i].w;
q.a = p.b;
q.b = Edges[p.b][i].b;
q.en = Edges[p.b][i].en;
Q.push(q);
}
}
}
int main()
{
int n, m, r, s, w, u;
int i, j;
pkt p, q;
tpl t;
scanf("%d %d", &n, &m);
for (i = 1; i <= m; i++) {
scanf("%d %d %d", &r, &s, &w);
t.b = s;
t.w = w;
t.en = i;
Edges[r].push_back(t);
t.b = r;
t.w = w;
t.en = i;
Edges[s].push_back(t);
}
for (i = 1; i <= n; i++) wei[i] = L[i] = 10E12;
scanf("%d", &u);
dijkstra(u);
for (i = 1; i <= n; i++)
{
if (i != u) ansL += wei[i];
}
printf("%lld\n", ansL);
for (i = 1; i <= n; i++)
{
if (i != u) printf("%d ", en[i]);
}
return 0;
}
https://codeforces.com/problemset/problem/545/E
'🧩PS > 🥇Hard' 카테고리의 다른 글
[C/C++] 백준 9663번 - N-Queen (DFS, 백트래킹) (0) | 2020.10.06 |
---|---|
[C/C++] 백준 1181번 - 단어 정렬 (std::sort, std::unique, std::erase) (0) | 2020.09.12 |
[C/C++] Codeforce 20C - Dijkstra? (0) | 2020.06.27 |
[C/C++] Codeforce 1336A - Linova and Kingdom (0) | 2020.06.09 |
[C/C++] Codeforce 1324F - Maximum White Subtree (0) | 2020.06.09 |