#include <bits/stdc++.h>
using namespace std;
vector<pair<long long, long long> > g[300005];
long long dist[300005];
long long marker[300005];
map<pair<long long, long long>, long long> Index, Cost;
bool done[300005];
vector<pair<long long, long long> > nodes;
long long sum;
vector<long long> ans;
long long inf = 1e18;
long long n, m;
void dijk(long long src) {
  for (long long i = 0; i < n; i++) dist[i] = inf;
  dist[src] = 0;
  priority_queue<pair<long long, int> > pq;
  pq.push(make_pair(0, src));
  while (!pq.empty()) {
    long long node = pq.top().second;
    long long cost = -pq.top().first;
    pq.pop();
    if (cost > dist[node]) continue;
    for (int i = 0; i < int(g[node].size()); i++) {
      long long child = g[node][i].first;
      long long price = g[node][i].second;
      if (dist[child] <= price + cost) continue;
      dist[child] = price + cost;
      pq.push(make_pair(-dist[child], child));
    }
  }
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  for (long long i = 0; i < m; i++) {
    long long u, v, w;
    cin >> u >> v >> w;
    --u;
    --v;
    g[u].push_back(make_pair(v, w));
    g[v].push_back(make_pair(u, w));
    Index[make_pair(u, v)] = i;
    Index[make_pair(v, u)] = i;
    Cost[make_pair(u, v)] = w;
    Cost[make_pair(v, u)] = w;
  }
  long long src;
  cin >> src;
  --src;
  dijk(src);
  for (long long i = 0; i < n; i++) {
    nodes.push_back(make_pair(dist[i], i));
    marker[i] = -1;
  }
  marker[src] = src;
  sort(nodes.begin(), nodes.end());
  for (long long i = 0; i < int(nodes.size()); i++) {
    long long node = nodes[i].second;
    done[node] = true;
    long long parent = marker[node];
    if (node != parent) {
      ans.push_back(Index[make_pair(node, parent)]);
      sum += Cost[make_pair(node, parent)];
    }
    for (long long i = 0; i < int(g[node].size()); i++) {
      long long child = g[node][i].first;
      long long cost = g[node][i].second;
      if (done[child] || dist[node] + cost != dist[child]) continue;
      long long pastMarker = marker[child];
      long long curMarker = node;
      if (pastMarker == -1 || dist[curMarker] >= dist[pastMarker])
        marker[child] = curMarker;
    }
  }
  cout << sum << endl;
  for (long long i = 0; i < int(ans.size()); i++) cout << ans[i] + 1 << " ";
  cout << endl;
  return 0;
}
