#define _USE_MATH_DEFINES #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; template class EdgeBase { public: int to; T cost; EdgeBase(){}; EdgeBase(int to0, T cost0){to = to0; cost = cost0;} }; typedef EdgeBase Edge; template void shortestPath(const vector > >& edges, int start, vector& dist) { const T INF = numeric_limits::max(); const T EPS = static_cast(1.0e-10); dist.assign(edges.size(), INF); dist[start] = 0; priority_queue, vector >, greater > > q; q.push(make_pair(0, start)); while(!q.empty()){ pair p = q.top(); q.pop(); int v = p.second; if(dist[v] < p.first - EPS) continue; for(unsigned i=0; i e = edges[v][i]; if(dist[v] + e.cost < dist[e.to] - EPS){ dist[e.to] = dist[v] + e.cost; q.push(make_pair(dist[e.to], e.to)); } } } } template void shortestPath(const vector > >& edges, vector >& dist) { dist.resize(edges.size()); for(unsigned i=0; i> n >> m >> init; -- init; vector t(n); for(int i=0; i> t[i]; if(count(t.begin(), t.end(), 0) >= n - 1){ cout << 0 << endl; return 0; } vector > edges(n); for(int i=0; i> a >> b >> c; -- a; -- b; edges[a].push_back(Edge(b, c)); edges[b].push_back(Edge(a, c)); } vector > dist; shortestPath(edges, dist); long long ans = LLONG_MAX; for(int i=0; i 0){ long long tmp = sum - dist[i][j] + dist[init][j]; ans = min(ans, tmp); } } } cout << ans << endl; return 0; }