#include <bits/stdc++.h>
const int MAXN = 300010;
const long long INF = ~0uLL >> 1;
using namespace std;
set<long long> si;
long long d[MAXN];
int p[MAXN];
bool vis[MAXN];
int n, m;
struct HeapNode {
  long long d;
  int u;
  bool operator<(const HeapNode& rhs) const { return d > rhs.d; }
};
struct Edge {
  int to, next;
  long long w;
  int id;
} edge[MAXN << 1];
int head[MAXN], tot;
void init() {
  tot = 0;
  memset(head, 0xff, sizeof(head));
}
void addedge(int u, int v, long long w, int id) {
  edge[tot].to = v;
  edge[tot].w = w;
  edge[tot].id = id;
  edge[tot].next = head[u];
  head[u] = tot++;
}
void dijkstra(int s) {
  priority_queue<HeapNode> q;
  for (int i = 1; i <= n; ++i) d[i] = INF;
  d[s] = 0;
  memset(vis, false, sizeof(vis));
  memset(p, 0xff, sizeof(p));
  q.push((HeapNode){0, s});
  while (!q.empty()) {
    HeapNode x = q.top();
    q.pop();
    int u = x.u;
    if (vis[u]) continue;
    vis[u] = 1;
    for (int i = head[u]; i != -1; i = edge[i].next) {
      int v = edge[i].to;
      long long w = edge[i].w;
      if (d[v] > d[u] + w) {
        d[v] = d[u] + w;
        p[v] = i;
        q.push((HeapNode){d[v], v});
      } else if (d[v] == d[u] + w && edge[p[v]].w > w) {
        p[v] = i;
      }
    }
  }
}
void print(int s) {
  long long ans = 0;
  for (int i = 1; i <= n; ++i) {
    if (p[i] >= 0) ans += edge[p[i]].w;
  }
  cout << ans << endl;
  for (int i = 1; i <= n; ++i) {
    if (p[i] >= 0) si.insert(edge[p[i]].id);
  }
  for (__typeof((si).begin()) i = (si).begin(); i != (si).end(); ++i) {
    printf("%d ", *i);
  }
  puts("");
}
int main() {
  int u, v, s;
  long long w;
  scanf("%d%d", &n, &m);
  init();
  for (int i = 1; i <= m; ++i) {
    scanf("%d%d%I64d", &u, &v, &w);
    addedge(u, v, w, i);
    addedge(v, u, w, i);
  }
  scanf("%d", &s);
  dijkstra(s);
  print(s);
  return 0;
}
