#include <bits/stdc++.h>
using namespace std;
const long long INF = 1LL << 50;
const int MAXN = 3 * 100000 + 5;
int n, m;
int id[MAXN];
vector<long long> dist;
vector<pair<long long, int>> adj[MAXN];
void dijkstra(int source) {
  priority_queue<pair<long long, int>, vector<pair<long long, int>>,
                 greater<pair<long long, int>>>
      q;
  dist.assign(n, INF);
  dist[source] = 0;
  q.push(make_pair(0, source));
  while (!q.empty()) {
    long long d = q.top().first;
    int v = q.top().second;
    q.pop();
    if (d <= dist[v]) {
      for (auto p : adj[v]) {
        int u = p.first;
        int w = p.second;
        if (dist[v] + w < dist[u]) {
          dist[u] = dist[v] + w;
          q.push(make_pair(dist[u], u));
        }
      }
    }
  }
}
struct Edge {
  int v, u, w;
} edges[MAXN];
void add_edge(Edge e) {
  adj[e.v].push_back(make_pair(e.u, e.w));
  adj[e.u].push_back(make_pair(e.v, e.w));
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    cin >> edges[i].v >> edges[i].u >> edges[i].w;
    --edges[i].v;
    --edges[i].u;
    add_edge(edges[i]);
  }
  int source;
  cin >> source;
  dijkstra(source - 1);
  fill(id, id + MAXN, -1);
  for (int i = 0; i < m; i++) {
    int v = edges[i].v;
    int u = edges[i].u;
    int w = edges[i].w;
    if (abs(dist[v] - dist[u]) == w) {
      if (dist[v] < dist[u]) {
        swap(v, u);
      }
      if (id[v] == -1 || w < edges[id[v]].w) {
        id[v] = i;
      }
    }
  }
  long long ans = 0;
  vector<int> roads;
  for (int i = 0; i < n; i++) {
    if (id[i] != -1) {
      roads.push_back(id[i] + 1);
      ans += edges[id[i]].w;
    }
  }
  cout << ans << endl;
  for (int r : roads) {
    cout << r << ' ';
  }
  cout << endl;
  return 0;
}
