#include <bits/stdc++.h>
using namespace std;
const int N = 300005;
map<pair<int, int>, pair<int, int> > M;
priority_queue<pair<long long, int>, vector<pair<long long, int> >,
               greater<pair<long long, int> > >
    Q;
vector<pair<int, int> > adjList[N];
pair<long long, int> dist[N];
int parent[N];
vector<int> ans;
int main() {
  int n, m, u, v, src;
  long long w, d;
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    cin >> u >> v >> w;
    M[{u, v}] = {i, w};
    M[{v, u}] = {i, w};
    adjList[u].push_back({v, w});
    adjList[v].push_back({u, w});
  }
  cin >> src;
  Q.push({0, src});
  for (int i = 1; i <= n; i++)
    dist[i].first = LLONG_MAX, dist[i].second = INT_MAX;
  dist[src].first = dist[src].second = 0;
  while (!Q.empty()) {
    u = Q.top().second, d = Q.top().first;
    Q.pop();
    if (d > dist[u].first) continue;
    for (int i = 0; i < adjList[u].size(); i++) {
      v = adjList[u][i].first, w = adjList[u][i].second;
      if (d + w < dist[v].first) {
        dist[v].first = d + w;
        dist[v].second = w;
        parent[v] = u;
        Q.push({dist[v].first, v});
      } else if (d + w == dist[v].first && dist[v].second > w) {
        dist[v].second = w;
        parent[v] = u;
        Q.push({dist[v].first, v});
      }
    }
  }
  d = 0;
  for (int i = 1; i <= n; i++) {
    if (i == src) continue;
    ans.push_back(M[{parent[i], i}].first);
    d += M[{parent[i], i}].second;
  }
  cout << d << endl;
  for (int i = 0; i < ans.size(); i++) cout << ans[i] << " ";
  cout << endl;
  return 0;
}
