int Dijkstra(int START,int END) { priority_queue<City,vector<City>,CMP> QUE; vector<int> Path(MAXN,INF); Path[START]=0; City temp; temp.set(START,0); QUE.push(temp); while(!QUE.empty()) { int Ups=QUE.top().LCity; if(Ups==END) return QUE.top().Cost; QUE.pop(); for(unsigned i=0;i<Graph[Ups].size();i++){ int tCity=Graph[Ups][i].LCity; int tCost=Graph[Ups][i].Cost; if(tCost+Path[Ups]<Path[tCity]){ Path[tCity]=tCost+Path[Ups]; temp.set(tCity,Path[tCity]); QUE.push(temp); } } } return -1; }
int main() { int m; while(scanf("%d%d",&n,&m),n||m){ Graph.clear(); Graph.resize(n+1); for(int i=0;i<m;i++){ int c1,c2,cost; scanf("%d%d%d",&c1,&c2,&cost); City temp; temp.set(c2,cost); Graph[c1].push_back(temp); temp.set(c1,cost); Graph[c2].push_back(temp); } printf("%d\n",Dijkstra(1,n)); } }
bool Cities::create_city(const std::string &_str, Countries &_countries, City &_city){ auto country_sm_name = 0; auto city_name = 1; auto latitude = 5; auto longitude = 6; std::vector<std::string> tokens; StringUtils::split(_str, tokens, ','); if (tokens.size() >= (longitude + 1)){ auto country_id = _countries.add_country(tokens[country_sm_name], tokens[country_sm_name]); _city.set(tokens[city_name], boost::lexical_cast<float>(tokens[latitude]), boost::lexical_cast<float>(tokens[longitude]), country_id); return true; } return false; }