#include #include #include #include #include #include #include #include #include #include #define LL long long #define ALL(variable) (variable).begin(), (variable).end() #define GT(variableName, defaultValue, limit) for(int variableName = defaultValue; variableName < (int)limit; variableName++) #define GTE(variableName, defaultValue, limit) for(int variableName = defaultValue; variableName <= (int)limit; variableName++) #define LT(variableName, defaultValue, limit) for(int variableName = defaultValue; variableName > (int)limit; variableName--) #define LTE(variableName, defaultValue, limit) for(int variableName = defaultValue; variableName >= (int)limit; variableName--) #define YES cout << "Yes" << endl; #define NO cout << "No" << endl; using namespace std ; int main(){ int N, M; cin >> N >> M; vector>> route(N, vector>(0)); priority_queue, vector>, greater>> d; //cost, positionN, use0ticket true=use vector cost(N, LLONG_MAX), cost0(N, LLONG_MAX); tuple here; cost[0] = cost0[0] = 0; d.push(make_tuple(0, 0, false)); GT(i, 0, M){ int a, b, c; cin >> a >> b >> c; a--; b--; route[a].push_back(make_pair(b, c)); route[b].push_back(make_pair(a, c)); } while(d.size() >= 1){ here = d.top(); d.pop(); int currentPotision = get<1>(here); if(!(get<2>(here))){ if(cost[get<1>(here)] >= get<0>(here)){ GT(i, 0, route[currentPotision].size()){ if(!get<2>(here)){ if(cost[route[currentPotision][i].first] > get<0>(here) + route[currentPotision][i].second){ cost[route[currentPotision][i].first] = get<0>(here) + route[currentPotision][i].second; d.push(make_tuple(cost[route[currentPotision][i].first], route[currentPotision][i].first, get<2>(here))); } if(cost0[route[currentPotision][i].first] > get<0>(here)){ cost0[route[currentPotision][i].first] = get<0>(here); d.push(make_tuple(cost0[route[currentPotision][i].first], route[currentPotision][i].first, true)); } } } } } else{ if(cost0[get<1>(here)] >= get<0>(here)){ GT(i, 0, route[currentPotision].size()){ if(cost0[route[currentPotision][i].first] > get<0>(here) + route[currentPotision][i].second){ cost0[route[currentPotision][i].first] = get<0>(here) + route[currentPotision][i].second; d.push(make_tuple(cost0[route[currentPotision][i].first], route[currentPotision][i].first, true)); } } } } } GT(i, 0, N){ cout << cost[i] + cost0[i] << endl; } return 0; }