#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 10000;
const long long mod = 1e9 + 7;
int n, m;
vector<int> G[maxn];
int edge[maxn][2];
long long dp[maxn];
int par[maxn];
void Dijkstra(int src) {
  memset(dp, 63, sizeof(dp));
  priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                 greater<pair<long long, int>>>
      pq;
  dp[src] = 0LL;
  pq.push({0LL, src});
  while (!pq.empty()) {
    auto u = pq.top();
    pq.pop();
    if (u.first > dp[u.second]) continue;
    for (auto g : G[u.second]) {
      int ver = edge[g][0] ^ u.second;
      int wei = edge[g][1];
      if (dp[ver] > dp[u.second] + wei) {
        dp[ver] = dp[u.second] + wei;
        par[ver] = g;
        pq.push({dp[ver], ver});
      } else if (dp[ver] == dp[u.second] + wei && edge[par[ver]][1] > wei) {
        par[ver] = g;
      }
    }
  }
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int u, v, w;
    cin >> u >> v >> w;
    edge[i][0] = u ^ v;
    edge[i][1] = w;
    G[u].push_back(i);
    G[v].push_back(i);
  }
  int start;
  cin >> start;
  Dijkstra(start);
  long long sum = 0;
  for (int i = 1; i <= n; i++) {
    if (i == start) continue;
    sum += edge[par[i]][1];
  }
  cout << sum << endl;
  for (int i = 1; i <= n; i++) {
    if (i == start) continue;
    cout << par[i] << " ";
  }
}
