#include <bits/stdc++.h>
using namespace std;
const int max_n = 1e6 + 10;
const long long inf = 1e18;
vector<pair<int, int>> adj[max_n];
set<pair<long long, int>> q;
long long d[max_n];
int b[max_n];
set<int> ans;
map<pair<int, int>, int> mp;
map<pair<int, int>, int> e;
map<int, long long> z;
long long p;
int main() {
  int n, m;
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    int u, v, c;
    cin >> u >> v >> c;
    u--;
    v--;
    adj[u].push_back({c, v});
    mp[make_pair(u, v)] = c;
    mp[make_pair(v, u)] = c;
    e[make_pair(u, v)] = i + 1;
    e[make_pair(v, u)] = i + 1;
    z[i + 1] = c;
    z[i + 1] = c;
    adj[v].push_back({c, u});
  }
  int h;
  cin >> h;
  h--;
  for (int i = 0; i < n; i++)
    if (i != h) d[i] = inf;
  q.insert({0, h});
  while (q.size()) {
    int v = (*q.begin()).second;
    q.erase(q.begin());
    for (auto j : adj[v]) {
      if (d[j.second] > d[v] + j.first) {
        ans.erase(e[{b[j.second], j.second}]);
        b[j.second] = v;
        q.erase({d[j.second], j.second});
        q.insert({d[j.second] = d[v] + j.first, j.second});
        ans.insert(e[{j.second, v}]);
      } else if (d[j.second] == d[v] + j.first &&
                 mp[{b[j.second], j.second}] > j.first) {
        ans.erase(e[{b[j.second], j.second}]);
        b[j.second] = v;
        q.erase({d[j.second], j.second});
        q.insert({d[j.second] = d[v] + j.first, j.second});
        ans.insert(e[{j.second, v}]);
      }
    }
  }
  for (auto i : ans) p += z[i];
  cout << p << endl;
  for (auto i : ans) cout << i << " ";
  return 0;
}
