#include <bits/stdc++.h>
using namespace std;
const int maxn = 3e5 + 100;
const long long inf = 0x3f3f3f3f3f3f3f3f;
int n, m;
struct Edge {
  int u, v, w;
} edges[maxn];
vector<pair<pair<int, int>, int>> g[maxn];
long long dist[maxn], ans = 0;
bool mark[maxn];
int edge_pai[maxn];
void dijkstra(int s) {
  for (int i = (1); i < (n + 1); i++) dist[i] = inf;
  dist[s] = 0ll;
  priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                 greater<pair<long long, int>>>
      q;
  q.push({0ll, s});
  while (!q.empty()) {
    int u = q.top().second;
    q.pop();
    if (mark[u]) continue;
    mark[u] = 1;
    for (auto pp : g[u]) {
      int v = pp.first.first;
      int w = pp.first.second;
      int id = pp.second;
      if (dist[v] == dist[u] + w && edges[edge_pai[v]].w > w) {
        ans += w - edges[edge_pai[v]].w;
        edge_pai[v] = id;
      }
      if (dist[v] > dist[u] + w) {
        ans += w - (edges[edge_pai[v]].w ? edges[edge_pai[v]].w : 0);
        edge_pai[v] = id;
        dist[v] = dist[u] + w;
        q.push({dist[u] + w, v});
      }
    }
  }
}
int main() {
  scanf("%d %d", &n, &m);
  for (int i = (1); i < (m + 1); i++) {
    int u, v, w;
    scanf("%d %d %d", &u, &v, &w);
    g[u].push_back({{v, w}, i});
    g[v].push_back({{u, w}, i});
    edges[i] = {u, v, w};
  }
  int s;
  scanf("%d", &s);
  dijkstra(s);
  printf("%lld\n", ans);
  for (int i = (1); i < (n + 1); i++)
    if (i != s) printf("%d%c", edge_pai[i], " \n"[i == n]);
  if (s == n) puts("");
}
