AlexandruT

Priority queue #3

Jul 6th, 2015
229
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.99 KB | None | 0 0
  1. #include <fstream>
  2. #include <iostream>
  3. #include <queue>
  4.  
  5. #define INF 100000000
  6. using namespace std;
  7.  
  8. int a[100][100], c[10001];
  9.  
  10. struct Coord
  11. {
  12.     int x, y, cost;
  13.     bool operator < (const Coord &v) const
  14.     {
  15.         return a[x][y] > a[v.x][v.y];
  16.     }
  17. };
  18.  
  19. priority_queue <Coord> q;
  20.  
  21. int main()
  22. {
  23.     int i, j, x, p;
  24.     Coord v;
  25.     int n, k;
  26.     ifstream fin("date.in");
  27.     fin >> k >> n;
  28.     for(i = 1; i <= k; i++)
  29.         for(j = 1; j <= n; j++)
  30.             fin >> a[i][j];
  31.  
  32.     /// pun infnit pe ultima coloana
  33.     for(i = 1; i <= k; i++)
  34.         a[i][n + 1] = INF;
  35.  
  36.     /// pun prima coloana in q
  37.     for(i = 1; i <= k; i++)
  38.     {
  39.         v.x = i;
  40.         v.y = 1;
  41.         q.push(v);
  42.     }
  43.  
  44.     /// interclasarea
  45.     x = n * k;
  46.     for(p = 1; p <= x; p++)
  47.     {
  48.         v = q.top();
  49.         q.pop();
  50.         c[p] = a[v.x][v.y];
  51.         v.y++;
  52.         q.push(v);
  53.     }
  54.  
  55.     for(p = 1; p <= x; p++)
  56.         cout << c[p] << " ";
  57.  
  58.     return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment