#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 10;
int n, m, vertex_start;
vector<pair<int, pair<int, int>>> graph[3 * N];
long long dist[3 * N];
priority_queue<pair<long long, int>, vector<pair<long long, int>>,
               greater<pair<long long, int>>>
    q;
pair<int, int> ans[3 * N];
int main() {
  ios_base::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int from, to, weight;
    cin >> from >> to >> weight;
    graph[from].push_back({to, {weight, i}});
    graph[to].push_back({from, {weight, i}});
  }
  for (int i = 1; i <= n; i++) {
    dist[i] = 1e18;
  }
  cin >> vertex_start;
  dist[vertex_start] = 0;
  q.push({0, vertex_start});
  bool usd[N * 3] = {};
  while (!q.empty()) {
    pair<long long, int> cur = q.top();
    int v = cur.second;
    long long value = cur.first;
    q.pop();
    if (usd[v]) {
      continue;
    }
    if (value > dist[v]) {
      continue;
    }
    usd[v] = true;
    for (int i = 0; i < graph[v].size(); i++) {
      int child = graph[v][i].first;
      int weight = graph[v][i].second.first;
      int id = graph[v][i].second.second;
      long long res = dist[v] + 1LL * weight;
      if (dist[child] >= res) {
        dist[child] = res;
        q.push({res, child});
        ans[child] = {id, weight};
      }
    }
  }
  long long edge_sum = 0;
  for (int i = 1; i <= n; i++) {
    if (i != vertex_start) {
      edge_sum = edge_sum + 1LL * ans[i].second;
    }
  }
  cout << edge_sum << endl;
  for (int i = 1; i <= n; i++) {
    if (i != vertex_start) {
      cout << ans[i].first << " ";
    }
  }
}
