#include <bits/stdc++.h>
using namespace std;
const long long M = 1e15;
int main(int argc, char const* argv[]) {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<pair<int, int> > adjacency_list[n];
  int edges[m];
  long long distance[n];
  int previous[n], last_cost[n];
  bool visited[n];
  for (int i = 0; i < n; i += 1) {
    distance[i] = M;
    previous[i] = -1;
    visited[i] = 0;
  }
  for (int i = 0; i < m; i += 1) {
    int a, b;
    scanf("%d %d %d", &a, &b, edges + i);
    a--;
    b--;
    adjacency_list[a].push_back(make_pair(b, i));
    adjacency_list[b].push_back(make_pair(a, i));
  }
  int source;
  scanf("%d", &source);
  source--;
  distance[source] = 0;
  previous[source] = -1;
  set<pair<long long, pair<int, int> > > que;
  que.insert(make_pair(0, make_pair(0, source)));
  long long ans = 0;
  while (!que.empty()) {
    pair<long long, pair<int, int> > present = *(que.begin());
    que.erase(que.begin());
    int u = present.second.second;
    long long dist = present.first;
    assert(u < n);
    if (visited[u]) {
      continue;
    }
    visited[u] = 1;
    for (int i = 0; i < adjacency_list[u].size(); i += 1) {
      int v = adjacency_list[u][i].first;
      int k = adjacency_list[u][i].second;
      assert(v < n);
      assert(k < m);
      que.erase(make_pair(distance[v], make_pair(last_cost[v], v)));
      if (distance[v] > edges[k] + dist ||
          (distance[v] == edges[k] + dist && edges[k] < last_cost[v])) {
        distance[v] = edges[k] + dist;
        previous[v] = k;
        last_cost[v] = edges[k];
      }
      que.insert(make_pair(distance[v], make_pair(last_cost[v], v)));
    }
    ans += present.second.first;
  }
  cout << ans << endl;
  for (int i = 0; i < n; i += 1) {
    if (i != source) {
      printf("%d ", previous[i] + 1);
    }
  }
  printf("\n");
  return 0;
}
