1. 程式人生 > >Codeforces Round #303 (Div. 2) E 最短路迪傑斯特拉(小根堆實現)

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;
}