#include <bits/stdc++.h>
using namespace std;
const long long NMAX = 300000 + 5;
const long long inf = 922337203685477580LL;
vector<vector<pair<long long, long long> > > g(NMAX);
vector<vector<int> > id(NMAX);
long long dis[NMAX];
long long last[NMAX];
long long a[NMAX];
int main() {
  int n, m;
  cin >> n >> m;
  long long ans = 0;
  for (int i = 0; i < m; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    g[a].push_back({b, c});
    g[b].push_back({a, c});
    id[a].push_back(i);
    id[b].push_back(i);
  }
  int s;
  cin >> s;
  for (int i = 1; i <= n; i++) dis[i] = inf;
  dis[s] = 0;
  for (int i = 1; i <= n; i++) last[i] = inf;
  last[s] = 0;
  set<pair<int, int> > q;
  q.insert({0, s});
  while (!q.empty()) {
    int w = q.begin()->second;
    q.erase(q.begin());
    for (int i = 0; i < g[w].size(); i++) {
      int to = g[w][i].first;
      int len = g[w][i].second;
      if (dis[w] + len < dis[to] ||
          (dis[w] + len <= dis[to] && len < last[to])) {
        q.erase(make_pair(dis[to], to));
        dis[to] = dis[w] + len;
        last[to] = len;
        a[to] = id[w][i] + 1;
        q.insert(make_pair(dis[to], to));
      }
    }
  }
  for (int i = 1; i <= n; i++) ans += last[i];
  cout << ans << endl;
  for (int i = 1; i <= n; i++) {
    if (i != s) {
      cout << a[i] << " ";
    }
  }
  cout << endl;
  return 0;
}
