Untitled
user_5668965
c_cpp
a year ago
1.9 kB
12
Indexable
#include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> typedef long long ll; #define pb push_back #define ff first #define ss second const int N=3e5+7; const int mod=1e9+7; const double eps=1e-6; const double pi= 3.14159265358979323846; using namespace std; using namespace __gnu_pbds; typedef tree<int, null_type, less_equal<int>, rb_tree_tag, tree_order_statistics_node_update> op_set; int32_t main() { ios_base::sync_with_stdio(false); cin.tie(0); int n,m; cin >> n >> m; vector<vector< array<long long,3> >> g(n); vector<long long> wts(m); for(int i=0;i<m;i++) { int u,v,wt; cin >> u >> v >> wt; u--; v--; g[u].pb( {v,wt,i} ); g[v].pb( {u,wt,i} ); wts[ i ]=wt; } int sr; cin >> sr; sr--; vector<long long> dist(n,1e18); vector<int> useful(n,-1); set< pair<long long,int> > dij; dist[sr]=0; dij.insert( {0,sr} ); while(dij.size()!=0) { auto val=*dij.begin(); dij.erase(val); int node=val.second; for(auto &info : g[node] ) { int child=info[0]; long long wt=info[1]; long long edno=info[2]; if(dist[child]>=dist[node]+wt) { dij.erase( {dist[child],child} ); dist[child]=dist[node]+wt; dij.insert( {dist[child],child} ); useful[child]=edno; } } } long long ans=0; for(auto ele : useful) { if(ele!=-1) ans=ans+wts[ele]; } cout << ans << "\n"; for(auto ele : useful) { if(ele!=-1) cout << ele+1 << " "; } } /* I have a hopeless crush on someone who I don't even have a chance on */
Editor is loading...
Leave a Comment