Dang_Quan_10_Tin

Untitled

Mar 2nd, 2020
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.28 KB | None | 0 0
  1. struct dat{
  2.     string s;
  3.     int val;
  4.     string out;
  5.     dat() {}
  6.     dat(string& a, int b, string p){
  7.         this->s = a;
  8.         this->val = b;
  9.         this->out = p + ";" + this->s;
  10.     }
  11.     bool operator <(const dat& a)const{
  12.         return val > a.val
  13.             || (val == a.val && out > a.out);
  14.     }
  15. };
  16.  
  17. inline void chuanhoa(string& s){
  18.     for(int i = 0; i < s.size(); ++i){
  19.         if(i == 0 || s[i - 1] == ' '){
  20.             if('a' <= s[i] && s[i] <= 'z') s[i] += 'A' - 'a';
  21.         }
  22.         else if('A' <= s[i] && s[i] <= 'Z') s[i] += 'a' - 'A';
  23.     }
  24. }
  25.  
  26. std::string travelPath(std::string x, std::string y, std::vector<std::vector<std::string>> paths)
  27. {
  28.     map<string, vector< pair<string, int> > > ke;
  29.     for(auto i : paths){
  30.         chuanhoa(i[0]);
  31.         chuanhoa(i[1]);
  32.         ke[i[0]].push_back({i[1], stoi(i[2])});
  33.         ke[i[1]].push_back({i[0], stoi(i[2])});
  34.     }
  35.     priority_queue<dat, vector<dat> > open;
  36.     open.push(dat(x, 0, ""));
  37.     while(open.size()){
  38.         auto c = open.top();
  39.         open.pop();
  40.         if(c.s == y){
  41.             return c.out.substr(1, c.out.size() - 1);
  42.             continue;
  43.         }
  44.         for(auto i : ke[c.s]){
  45.             open.push(dat(i.first, c.val + i.second, c.out));
  46.         }
  47.     }
  48. }
Advertisement
Add Comment
Please, Sign In to add comment