#include <bits/stdc++.h>
using namespace std;
string vow = "aeiou";
int month[] = {-1, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int dxhorse[] = {-2, -2, -1, -1, 1, 1, 2, 2};
const int dyhorse[] = {1, -1, 2, -2, 2, -2, 1, -1};
const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, 1, 0, -1};
const long double pie = 3.1415926535897932384626;
const long long mod = 1e9 + 7;
const int N = 3e5 + 10;
vector<pair<int, int> > g[N];
long long dis[N];
int weight[N];
int n, m, src;
void read() {
  cin >> n >> m;
  for (int i = 1; i <= m; i++) {
    int a, b, w;
    cin >> a >> b >> w;
    g[a].push_back({i, b});
    g[b].push_back({i, a});
    weight[i] = w;
  }
  cin >> src;
}
struct comp {
  bool operator()(const pair<long long, pair<int, int> > &a,
                  const pair<long long, pair<int, int> > &b) {
    if (a.first < b.first) return false;
    if (a.first > b.first) return true;
    if (a.first == b.first) {
      if (weight[a.second.first] < weight[b.second.first]) return false;
      return true;
    }
  }
};
vector<int> ans;
long long tot_cost = 0;
bool vis[N];
void dijk(int node) {
  priority_queue<pair<long long, pair<int, int> >,
                 vector<pair<long long, pair<int, int> > >, comp>
      q;
  const long long inf = 1e15;
  for (int i = 1; i <= n; i++) dis[i] = inf;
  dis[node] = 0;
  q.push({0, {0, node}});
  while (q.empty() == false) {
    auto temp = q.top();
    q.pop();
    node = temp.second.second;
    if (vis[node]) continue;
    ;
    vis[node] = 1;
    int edge_number = temp.second.first;
    long long d = temp.first;
    if (edge_number) {
      ans.push_back(edge_number);
      tot_cost += weight[edge_number];
    }
    for (auto &temp : g[node]) {
      int i = temp.second;
      if (vis[i]) continue;
      ;
      long long relax_dis = d + weight[temp.first];
      if (relax_dis <= dis[i]) {
        dis[i] = relax_dis;
        q.push({relax_dis, {temp.first, i}});
      }
    }
  }
}
void solve(int test_case) {
  read();
  dijk(src);
  cout << tot_cost;
  cout << "\n";
  ;
  for (auto &i : ans) cout << i << " ";
  cout << "\n";
  ;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int t = 1;
  for (int i = 1; i <= t; i++) solve(i);
  return 0;
}
