1. 程式人生 > >E. Paths and Trees (CF 303 div2)最短路

E. Paths and Trees (CF 303 div2)最短路

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·1050 ≤ 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.

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.

題意:給定一個無向圖和一個點u,找出若干條邊組成一個子圖,要求這個子圖中u到其他個點的最短距離與在原圖中的相等,並且要求子圖所有邊的權重之和最小,求出最小值並輸出子圖的邊號。

思路:先求一遍最短路,從所有到i點的滿足最短路的邊中選一條權最小的邊。

程式碼:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#pragma comment (linker,"/STACK:102400000,102400000")
#define maxn 300100
#define MAXN 2005
#define mod 1000000009
#define INF 0x3f3f3f3f
#define pi acos(-1.0)
#define eps 1e-6
#define lson rt<<1,l,mid
#define rson rt<<1|1,mid+1,r
#define FRE(i,a,b)  for(i = a; i <= b; i++)
#define FREE(i,a,b) for(i = a; i >= b; i--)
#define FRL(i,a,b)  for(i = a; i < b; i++)
#define FRLL(i,a,b) for(i = a; i > b; i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define DBG         pf("Hi\n")
typedef long long ll;
using namespace std;

struct Edge
{
    int u,v,w,id,next;
}edge[maxn<<1];

int num,n,m,u;
int head[maxn];
__int64 dist[maxn];
bool inq[maxn];
vector<int>ans;

void init()
{
    num=0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int w,int id)
{
    edge[num]={u,v,w,id,head[u]};
    head[u]=num++;
    edge[num]={v,u,w,id,head[v]};
    head[v]=num++;
}

void SPFA(int s)
{
    memset(dist,INF,sizeof(dist));
    queue<int>Q;
    Q.push(s);
    dist[s]=0;
    inq[s]=true;
    while (!Q.empty())
    {
        int u=Q.front();Q.pop();
        inq[u]=false;
        for (int i=head[u];i+1;i=edge[i].next)
        {
            int v=edge[i].v;
            if (dist[v]>dist[u]+edge[i].w)
            {
                dist[v]=dist[u]+(__int64)edge[i].w;
                if (!inq[v])
                {
                    inq[v]=true;
                    Q.push(v);
                }
            }
        }
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:/Users/asus1/Desktop/IN.txt","r",stdin);
#endif
    int i,j,x,y,z;
    while (~scanf("%d%d",&n,&m))
    {
        init();
        for (i=1;i<=m;i++)
        {
            scanf("%d%d%d",&x,&y,&z);
            addedge(x,y,z,i);
        }
        sf(u);
        SPFA(u);
        __int64 sum=0;
        ans.clear();
        for (int i=1;i<=n;i++)
        {
            if (i==u) continue;
            int Min=INF;
            int ind;
            for (j=head[i];j+1;j=edge[j].next)
            {
                int v=edge[j].v;
                if (dist[i]==dist[v]+edge[j].w&&Min>edge[j].w)
                {
                    Min=edge[j].w;
                    ind=edge[j].id;
                }
            }
            sum+=(__int64)Min;
            ans.push_back(ind);
        }
        pf("%I64d\n",sum);
        for (int i=0;i<ans.size();i++)
            pf("%d ",ans[i]);
        pf("\n");
    }
    return 0;
}