#include <bits/stdc++.h>
using namespace std;
class Compare {
 public:
  bool operator()(pair<long long, int> x, pair<long long, int> y) {
    return x.first > y.first;
  }
};
vector<int> mat[300010], num[300010];
vector<long long> cost[300010];
priority_queue<pair<long long, int>, vector<pair<long long, int> >, Compare>
    que;
int n, m;
long long dis[300010], CC[300010], Fuck[300010];
void dijkstra(int p) {
  dis[p] = 0;
  que.push(make_pair(dis[p], p));
  while (!que.empty()) {
    pair<long long, int> use = que.top();
    p = use.second;
    que.pop();
    if (use.first != dis[p]) continue;
    for (int i = 0; i <= (int)mat[p].size() - 1; i++) {
      int x = mat[p][i];
      if (dis[x] > dis[p] + cost[p][i]) {
        dis[x] = dis[p] + cost[p][i];
        CC[x] = cost[p][i];
        Fuck[x] = num[p][i];
        que.push(make_pair(dis[x], x));
      } else if (dis[x] == dis[p] + cost[p][i] && CC[x] > cost[p][i])
        CC[x] = cost[p][i], Fuck[x] = num[p][i];
    }
  }
  return;
}
void prin() {
  long long c = 0;
  for (int i = 1; i <= n; i++) c += CC[i];
  cout << c << endl;
  for (int i = 1; i <= n; i++)
    if (Fuck[i] != 0) cout << Fuck[i] << ' ';
}
void dijkstra_input() {
  long long x, y, z, u;
  for (int i = 1; i <= m; i++) {
    cin >> x >> y >> z;
    mat[x].push_back(y);
    cost[x].push_back(z);
    num[x].push_back(i);
    mat[y].push_back(x);
    cost[y].push_back(z);
    num[y].push_back(i);
  }
  long long v = 1000000000000000000;
  for (int i = 1; i <= n; i++) dis[i] = v;
  cin >> u;
  dijkstra(u);
  prin();
  return;
}
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> m;
  dijkstra_input();
  return 0;
}
