Advertisement
cmiN

vacanta

Dec 18th, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.96 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <vector>
  4. #include <cfloat>
  5. #include <queue>
  6. #include <stack>
  7. using namespace std;
  8.  
  9.  
  10. struct Elem {
  11.  
  12.     int node;
  13.     float dist;
  14.  
  15.     Elem(int node=-1, float dist=-1)
  16.     {
  17.         this->node = node;
  18.         this->dist = dist;
  19.     }
  20. };
  21.  
  22.  
  23. struct Comp {
  24.  
  25.     bool operator()(const Elem & first,
  26.                     const Elem & second) const
  27.     {
  28.         return first.dist > second.dist;
  29.     }
  30. };
  31.  
  32.  
  33. typedef vector<int> vi_t;
  34. typedef vector<float> vf_t;
  35. typedef vector<vi_t> vvi_t;
  36. typedef vector<vf_t> vvf_t;
  37.  
  38.  
  39. int src, dest, nodes, edges;
  40.  
  41. vf_t timeList; // timpul in oras
  42. vvf_t timeMat; // timpul intre orase
  43. vvi_t adjList; // lista de adiacenta
  44.  
  45. vf_t dist; // timpul de ajungere
  46. float totalTime; // timpul intregului traseu
  47.  
  48.  
  49. void preprocess()
  50. {
  51.     ifstream fin("vacanta.in");
  52.     fin >> src >> dest >> nodes >> edges;
  53.     int size = nodes + 1;
  54.     timeList.resize(size);
  55.     timeMat.resize(size);
  56.     adjList.resize(size);
  57.     for (int i = 1; i <= nodes; ++i)
  58.         timeMat[i].resize(size);
  59.     for (int i = 0; i < nodes; ++i) {
  60.         int n;
  61.         float d, v;
  62.         fin >> n >> d >> v;
  63.         timeList[n] = d / v;
  64.     }
  65.     for (int i = 0; i < edges; ++i) {
  66.         int x, y;
  67.         float d, v;
  68.         fin >> x >> y;
  69.         fin >> d >> v;
  70.         timeMat[x][y] = timeMat[y][x] = d / v;
  71.         adjList[x].push_back(y);
  72.         adjList[y].push_back(x);
  73.     }
  74.     fin.close();
  75. }
  76.  
  77.  
  78. void process()
  79. {
  80.     /// Dijkstra folosind coada cu prioritati.
  81.     dist.assign(nodes + 1, FLT_MAX);
  82.     dist[src] = 0;
  83.     priority_queue<Elem, vector<Elem>, Comp> pq;
  84.     pq.push(Elem(src, dist[src]));
  85.     while (!pq.empty()) {
  86.         Elem elem = pq.top();
  87.         pq.pop();
  88.         if (elem.node == dest) {
  89.             totalTime = elem.dist;
  90.             return;
  91.         }
  92.         for (vi_t::iterator it = adjList[elem.node].begin();
  93.              it != adjList[elem.node].end(); ++it) {
  94.             float tmp;
  95.             if ((tmp = dist[elem.node] + timeMat[elem.node][*it] +
  96.                 timeList[*it]) < dist[*it]) {
  97.                 dist[*it] = tmp;
  98.                 pq.push(Elem(*it, tmp));
  99.             }
  100.         }
  101.     }
  102. }
  103.  
  104.  
  105. void write_route()
  106. {
  107.     ofstream fout("vacanta.out");
  108.     fout << totalTime << endl;
  109.     // acum refacem traseul
  110.     stack<int> road;
  111.     int node = dest;
  112.     while (node != src) {
  113.         road.push(node);
  114.         for (vi_t::iterator it = adjList[node].begin();
  115.              it != adjList[node].end(); ++it) {
  116.             if (dist[node] - timeList[node] -
  117.                 timeMat[node][*it] == dist[*it]) {
  118.                 node = *it;
  119.                 break;
  120.             }
  121.         }
  122.     }
  123.     road.push(src);
  124.     while (!road.empty()) {
  125.         fout << road.top() << ' ';
  126.         road.pop();
  127.     }
  128.     fout << endl;
  129.     fout.close();
  130. }
  131.  
  132.  
  133. int main()
  134. {
  135.     preprocess();
  136.     process();
  137.     write_route();
  138.     return 0;
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement