Codeforces Round #303 (Div. 2) E 最短路迪杰斯特拉(小根堆实现)



链接:戳这里


E. Paths and Trees
time limit per test3 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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.

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肯定是最小的

贪心dis[v]>dis[u]+w 更新的v肯定是加入的新的要走的路径。

还有就是每次最短路径队首出来的点走过就不用再走了。更新的最短路到此节点就结束。

难后这道题,假设当前是最短路,则记录路径和更新节点的花费。如果最短路径相等,那么存下最小的路径花费。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<vector>
#include <ctime>
#include<queue>
#include<set>
#include<map>
#include<stack>
#include<iomanip>
#include<cmath>
#define mst(ss,b) memset((ss),(b),sizeof(ss))
#define maxn 0x3f3f3f3f
#define MAX 1000100
///#pragma comment(linker, "/STACK:102400000,102400000")
typedef long long ll;
typedef unsigned long long ull;
#define INF (1ll<<60)-1
using namespace std;
int n,m;
struct edge{
    int v,next,w,id;
}e[600100];
int head[300100],tot=0;
void Add(int u,int v,int w,int id){
    e[tot].v=v;
    e[tot].w=w;
    e[tot].next=head[u];
    e[tot].id=id;
    head[u]=tot++;
}
struct node{
    int u;
    ll d;
    node(int u=0,ll d=0):u(u),d(d){}
    bool operator < (const node &a)const{
        return d>a.d;
    }
};
ll dis[300100],vis[300100],cost[300100];
int used[300100];
void Dijkstra(int S){
    priority_queue<node> qu;
    for(int i=1;i<=n;i++) dis[i]=INF;
    dis[S]=0;
    qu.push(node(S,0));
    while(!qu.empty()){
        node now=qu.top();
        qu.pop();
        if(vis[now.u]) continue;
        vis[now.u]=1;
        for(int i=head[now.u];i!=-1;i=e[i].next){
            int v=e[i].v;
            int w=e[i].w;
            int id=e[i].id;
            if(!vis[v] && dis[v]>dis[now.u]+w){
                dis[v]=dis[now.u]+w;
                cost[v]=w;
                used[v]=id;
                qu.push(node(v,dis[v]));
            } else if(!vis[v] && dis[v]==dis[now.u]+w){
                if(cost[v]>w) {
                    cost[v]=w;
                    used[v]=id;
                }
            }
        }
    }
    ll ans=0;
    for(int i=1;i<=n;i++) ans+=cost[i];
    printf("%I64d\n",ans);
    if(ans==0) return ;
    for(int i=1;i<=n;i++) {
        if(i==S) continue;
        printf("%d ",used[i]);
    }
    cout<<endl;
}
int main(){
    mst(head,-1);
    scanf("%d%d",&n,&m);
    for(int i=1;i<=m;i++){
        int u,v,w;
        scanf("%d%d%d",&u,&v,&w);
        Add(u,v,w,i);
        Add(v,u,w,i);
    }
    int S;
    scanf("%d",&S);
    Dijkstra(S);
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值