Advertisement
_rashed

UVA 186

Feb 8th, 2023
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.91 KB | None | 0 0
  1. #define ll long long
  2. #include <bits/stdc++.h>
  3. using namespace std;
  4.  
  5. /*
  6. Ordered set usage:
  7. order_of_key (k) : Number of items strictly smaller than k.
  8. find_by_order(k) : K-th element in a set (counting from zero).
  9. */
  10.  
  11. #include <ext/pb_ds/assoc_container.hpp>
  12. #include <ext/pb_ds/tree_policy.hpp>
  13. using namespace __gnu_pbds;
  14. #define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
  15.  
  16. const int OO = 1e9;
  17. const double EPS = 1e-9;
  18.  
  19. struct edge {
  20.     string from;
  21.     string to;
  22.     string name;
  23.     int cost;
  24.  
  25.     int operator < (const edge &e) const {
  26.         return cost > e.cost;
  27.     }
  28. };
  29.  
  30. int cnt_com(string &s) {
  31.     int ret = 0;
  32.     for(int i = 0; i < s.size(); i++) {
  33.         if(s[i] == ',') {
  34.             ret++;
  35.         }
  36.     }
  37.     return ret;
  38. }
  39.  
  40. int nxt_com(string &s, int idx) {
  41.     for(; idx < s.size(); idx++) {
  42.         if(s[idx] == ',') {
  43.             return idx;
  44.         }
  45.     }
  46.     return -1;
  47. }
  48.  
  49. map<string,vector<edge>> graph;
  50.  
  51. int main()
  52. {
  53.     ios_base::sync_with_stdio(NULL);
  54.     cin.tie(0);
  55.     //freopen("out.txt","w",stdout);
  56.     string in;
  57.     while(getline(cin,in)) {
  58.         if(cnt_com(in) == 3) {
  59.             edge curr;
  60.             int e1 = nxt_com(in,0);
  61.             curr.from = in.substr(0,e1);
  62.             int e2 = nxt_com(in,e1+1);
  63.             curr.to = in.substr(e1+1,e2-e1-1);
  64.             int e3 = nxt_com(in,e2+1);
  65.             curr.name = in.substr(e2+1, e3-e2-1);
  66.             int cost = stoi(in.substr(e3+1,in.size()-e3-1));
  67.             curr.cost = cost;
  68.             graph[curr.from].push_back(curr);
  69.             swap(curr.from,curr.to);
  70.             graph[curr.from].push_back(curr);
  71.         }
  72.         else {
  73.             if(in.size() < 2) {
  74.                 continue;
  75.             }
  76.             map<string,edge> path;
  77.             map<string,int> dist;
  78.             priority_queue<edge> pq;
  79.             int ce = nxt_com(in,0);
  80.             string origin = in.substr(0,ce);
  81.             string tgt = in.substr(ce+1,in.size()-ce-1);
  82.             dist[origin] = 0;
  83.             for(edge e : graph[origin]) {
  84.                 pq.push(e);
  85.             }
  86.             while(!pq.empty()) {
  87.                 edge curr = pq.top();
  88.                 pq.pop();
  89.                 if(dist.find(curr.to) != dist.end()) {
  90.                     continue;
  91.                 }
  92.                 dist[curr.to] = curr.cost;
  93.                 path[curr.to] = curr;
  94.                 for(edge e : graph[curr.to]) {
  95.                     if(dist.find(e.to) == dist.end()) {
  96.                         e.cost += curr.cost;
  97.                         pq.push(e);
  98.                     }
  99.                 }
  100.             }
  101.             stack<edge> ans;
  102.             string curr_p = tgt;
  103.             while(curr_p != origin) {
  104.                 ans.push(path[curr_p]);
  105.                 curr_p = path[curr_p].from;
  106.             }
  107.             cout << "\n\nFrom" << string(17, ' ') << "To" << string(19, ' ') << "Route" << string(6, ' ') << "Miles\n";
  108.             cout << string(20,'-') << ' ' << string(20,'-') << ' ' << string(10,'-') << ' ' << string(5,'-') << "\n";
  109.             int sum = 0;
  110.             int prv = 0;
  111.             while(!ans.empty()) {
  112.                 edge curr = ans.top();
  113.                 ans.pop();
  114.                 string curr_cost = to_string(curr.cost-prv);
  115.                 sum += curr.cost-prv;
  116.                 prv = curr.cost;
  117.                 cout << curr.from << string(21 - curr.from.size(),' ') << curr.to << string(21 - curr.to.size(),' ');
  118.                 cout << curr.name << string(11 - curr.name.size(),' ') << string(5-curr_cost.size(), ' ') << curr_cost << "\n";
  119.             }
  120.             string string_sum = to_string(sum);
  121.             cout << string(53, ' ') << string(5, '-') << "\n";
  122.             cout << string(42, ' ') << "Total" << string(6, ' ') << string(5 - string_sum.size(), ' ') << string_sum << "\n";
  123.         }
  124.     }
  125.     return 0;
  126. }
  127.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement