Codeforces Round #303 (Div. 2).E题目链接
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.
刚读这道题的时候,读错了题意,然后就WA了on test4,(连续WA了)之后再看了遍,发现事情并不简单(搞事!!!)
这道题,并不是一开始我认为的那样(假题),是这样的:N个点,M条边就不多说了,我们要求从一个点S,到其余所有点的最短路径的同时,要求用的路径和是最少的。
首先,我们按照题目的要求那样,去维护最短路,并且维护是由哪条边过来的(最后一定是棵生成树),然后如果遇到更短的,就直接更新;如果是相等的话,那么就是去比较前面的所指向它的路径,哪条更短一些,我们选取更短的哪那条。
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define lowbit(x) ( x&(-x) )
#define pi 3.141592653589793
#define e 2.718281828459045
#define INF 0x3f3f3f3f3f3f3f3f
#define HalF (l + r)>>1
#define lsn rt<<1
#define rsn rt<<1|1
#define Lson lsn, l, mid
#define Rson rsn, mid+1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
#define myself rt, l, r
#define MP(a, b) make_pair(a, b)
#define MP3(a, b, c) MP(MP(a, b), c)
using namespace std;
typedef unsigned long long ull;
typedef long long ll;
const int maxN = 3e5 + 7, maxE = 6e5 + 7;
int N, M, head[maxN], cnt, pre[maxN], S;
struct Eddge
{
int nex, to, val, id, u;
Eddge(int a=-1, int b=0, int c=0, int d=0, int f=0):nex(a), to(b), val(c), id(d), u(f) {}
}edge[maxE];
inline void addEddge(int u, int v, int w, int id)
{
edge[cnt] = Eddge(head[u], v, w, id, u);
head[u] = cnt++;
}
inline void _add(int u, int v, int w, int id) { addEddge(u, v, w, id); addEddge(v, u, w, id); }
ll dist[maxN];
struct node
{
int id; ll dis;
node(int a=0, ll b=0):id(a), dis(b) {}
friend bool operator < (node e1, node e2) { return e1.dis > e2.dis; }
};
priority_queue<node> Q;
inline void dijkstra()
{
for(int i=1; i<=N; i++)
{
dist[i] = INF;
pre[i] = -1;
}
dist[S] = 0;
while(!Q.empty()) Q.pop();
Q.push(node(S, 0));
while(!Q.empty())
{
node now = Q.top(); Q.pop();
int u = now.id;
for(int i=head[u], v; ~i; i=edge[i].nex)
{
v = edge[i].to; ll w = edge[i].val;
if(dist[v] > dist[u] + w)
{
dist[v] = dist[u] + w;
pre[v] = i;
Q.push(node(v, dist[v]));
}
else if(dist[v] == dist[u] + w && edge[pre[v]].val > edge[i].val)
{
pre[v] = i;
}
}
}
}
inline void init()
{
cnt = 0;
memset(head, -1, sizeof(head));
}
int main()
{
scanf("%d%d", &N, &M);
init();
for(int i=1, u, v, w; i<=M; i++)
{
scanf("%d%d%d", &u, &v, &w);
_add(u, v, w, i);
}
scanf("%d", &S);
dijkstra();
ll ans = 0;
for(int i=1; i<=N; i++) if(i != S) ans += edge[pre[i]].val;
printf("%lld\n", ans);
for(int i=1; i<=N; i++)
{
if(i == S) continue;
printf("%d ", edge[pre[i]].id);
}
printf("\n");
return 0;
}