Codeforces Paths and Trees

本文介绍了一种算法,该算法用于解决在一个给定的无向图中找到一个子图的问题,该子图能保持从指定顶点出发到达其他所有顶点的最短路径不变,同时使得子图的总边权最小。

Paths and Trees

time limit per test3 seconds

memory limit per test256 megabytes

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.

Examples

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 条边的无向图和一个点 u,找出若干条边组成一个子图,要求这个子图中 u 到其他点的最短距离与在原图中的相等,并且要求子图所有边的权重和最小,求出最小值。
显然要先跑一次最短路。。。
然后你想一下对于一个点,只要有一条边从一个近一点的点能够转移过来构成他的最短路就够了。。。所以就贪心就好了,找一个最短的边保证可以就好了。。。


#include<bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 5;
struct lpl{
    int to, dis, num;
}lin;
struct ld{
    int num;
    long long dis;
}node[maxn];
vector<lpl> point[maxn]; 
int n, m, s;
long long ans, dis[maxn];
bool vis[maxn];
queue<int> q;

inline void putit()
{
    scanf("%d%d", &n, &m);
    for(int a, b, i = 1; i <= m; ++i){
        scanf("%d%d%d", &a, &b, &lin.dis); lin.num = i;
        lin.to = b; point[a].push_back(lin);
        lin.to = a; point[b].push_back(lin);
    }
    scanf("%d", &s);
}

inline void spfa()
{
    int now, qwe; memset(dis, 0x3f, sizeof(dis)); dis[s] = 0; q.push(s);
    while(!q.empty()){
        now = q.front(); q.pop(); vis[now] = false;
        for(int i = point[now].size() - 1; i >= 0; --i){
            qwe = point[now][i].to;
            if(dis[qwe] > dis[now] + point[now][i].dis){
                dis[qwe] = dis[now] + point[now][i].dis;
                if(!vis[qwe]){vis[qwe] = true; q.push(qwe);}
            }
        }       
    }
}

inline bool cmp(ld A, ld B){return A.dis < B.dis;}

inline void workk()
{
    for(int i = 1; i <= n; ++i){node[i].num = i; node[i].dis = dis[i];}
    sort(node + 1, node + n + 1, cmp);
    int t, now, qwe, num;
    for(int i = 1; i <= n; ++i){
        t = node[i].num; if(t == s) continue;
        qwe = 2e9;
        for(int j = point[t].size() - 1; j >= 0; --j){
            now = point[t][j].to;
            if(dis[now] + point[t][j].dis != dis[t]) continue;
            if(point[t][j].dis < qwe){
                qwe = point[t][j].dis; num = point[t][j].num; 
            }
        }
        ans += qwe; vis[num] = true;
    }
}

inline void print()
{
    cout << ans << endl;
    for(int i = 1; i <= m; ++i)
        if(vis[i]) printf("%d ", i);
}

int main()
{
    putit();
    spfa();
    workk();
    print();
    return 0;
}

转载于:https://www.cnblogs.com/LLppdd/p/9229724.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值