codeforces 545E E. Paths and Trees(单源最短路+总权重最小)

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 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.

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.

题意:给n个点m条边,两个点间无重边,然后是m行的ui,vi,wi代表起始点和边的权重(第几个代表第几条边)然后在给起始点u,问选哪些边,可以满足从起始点u到达其它点距离最短,且使整张图的权重最小;

输出:所有边的总权重 以及选的边(递增顺序,若答案多组,输出任意一组)

思路:spfa,就是在更新每个点的最短路是,顺便记录和更新连到这个点的最短边;

 1 #include <stdio.h>
 2 #include <string.h>
 3 #include <stdlib.h>
 4 #include <math.h>
 5 #include <iostream>
 6 #include <algorithm>
 7 #include <climits>
 8 #include <queue>
 9 #define ll long long
10 #define INF  0x3f3f3f3f
11 
12 using namespace std;
13 const int MAXN = 1000500;
14 ll total,head[MAXN],dis[MAXN],used[MAXN];
15 struct nodes
16 {
17     ll e,l,next,th;
18 } edge[MAXN];
19 struct nodes2
20 {
21     ll s,dis,p;
22     bool operator <(nodes2 q) const
23     {
24         return dis > q.dis;
25     }
26 } cur;
27 void add(ll x,ll y,ll l,ll th)
28 {
29     edge[total].e = y;
30     edge[total].l = l;
31     edge[total].th = th;
32     edge[total].next = head[x];
33     head[x] = total++;
34 }
35 void init( )
36 {
37     total = 0;
38     memset(head,-1,sizeof(head));
39     memset(used,-1,sizeof(used));
40     fill(dis,dis+1000050,INF);
41 }
42 
43 void spfa(int x,int n)
44 {
45     ll s,e,subdis,th;
46     priority_queue<nodes2>Q;
47     cur.dis = 0,cur.s = x;
48     dis[x] = 0;
49     Q.push(cur);
50     while(!Q.empty())
51     {
52         cur = Q.top();
53         Q.pop();
54         s = cur.s;
55         for(int i = head[s]; i != -1; i = edge[i].next)
56         {
57             subdis = edge[i].l,e = edge[i].e,th = edge[i].th;
58             if(dis[e] > dis[s] + subdis)
59             {
60                 dis[e] = dis[s] + subdis;
61                 used[e] = th;
62                 cur.s = edge[i].e,cur.dis = dis[e];
63                 Q.push(cur);
64             }
65         }
66     }
67     ll d=0;
68     for(int i = 1; i <= n; i++)
69         d += dis[i];
70     printf("%lld\n",d);
71     sort(used,used+n);
72     for(int i = 1; i< n; i++)
73         if(i != n)
74         printf("%lld ",used[i]);
75     else
76         printf("%lld\n",used[i]);
77 }
78 int main(void)
79 {
80     int n,m;
81     scanf("%d%d",&n,&m);
82     init();
83     for(int i = 1; i <= m; i++)
84     {
85         ll x,y,l;
86         scanf("%lld %lld %lld",&x,&y,&l);
87         add(x,y,l,i);
88         add(y,x,l,i);
89     }
90     int s;
91     scanf("%d",&s);
92     spfa(s,n);
93     return 0;
94 }

 

转载于:https://www.cnblogs.com/henserlinda/p/4744955.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值