Advertisement
Guest User

soricel3

a guest
Feb 11th, 2020
237
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.51 KB | None | 0 0
  1. #include <queue>
  2. #include <fstream>
  3.  
  4. #define DMAX 80
  5.  
  6. std::ifstream fin("soricel3.in");
  7. std::ofstream fout("soricel3.out");
  8.  
  9. const int dL[] = {-1, 0, 1,  0};
  10. const int dC[] = { 0, 1, 0, -1};
  11.  
  12. struct Pos {
  13.     int lin;
  14.     int col;
  15. };
  16.  
  17. int m, n;
  18. int mat[DMAX][DMAX];
  19.  
  20. int nr;
  21. int dp[DMAX][DMAX];
  22.  
  23. class cmp {
  24.     bool reverse;
  25. public:
  26.     cmp(const bool& revParam = false) {
  27.         reverse = revParam;
  28.     }
  29.  
  30.     bool operator() (const Pos& x, const Pos& y) const {
  31.         if (reverse)
  32.             return mat[x.lin][x.col] < mat[y.lin][y.col];
  33.         return mat[x.lin][x.col] > mat[y.lin][y.col];
  34.     }
  35. };
  36.  
  37. std::queue<Pos> q;
  38. std::priority_queue<Pos, std::vector<Pos>, cmp> d;
  39.  
  40. inline int min(int x, int y) {
  41.     return x < y ? x : y;
  42. }
  43.  
  44. inline bool isInMat(Pos pos) {
  45.     return 1 <= pos.lin && pos.lin <= m &&
  46.            1 <= pos.col && pos.col <= n;
  47. }
  48.  
  49. void solve(int lin, int col, int len) {
  50.     if (lin == 1 && col == 1) {
  51.         fout << ' ' << len << "\n1 1\n";
  52.         return;
  53.     }
  54.  
  55.     int newLin, newCol;
  56.     for (int i = 0; i < 4; i++) {
  57.         newLin = lin + dL[i];
  58.         newCol = col + dC[i];
  59.  
  60.         if (isInMat({newLin, newCol}) && dp[newLin][newCol] == dp[lin][col] - mat[lin][col]) {
  61.             solve(newLin, newCol, len + 1);
  62.             fout << lin << ' ' << col << '\n';
  63.             return;
  64.         }
  65.     }
  66. }
  67.  
  68. int main() {
  69.     int x, y;
  70.  
  71.     fin >> m >> n >> nr;
  72.     for (int i = 0; i < nr; i++) {
  73.         fin >> x >> y;
  74.         q.push({x, y});
  75.         mat[x][y] = m + n;
  76.     }
  77.  
  78.     Pos pos, v;
  79.     while (!q.empty()) {
  80.         pos = q.front();
  81.         q.pop();
  82.  
  83.         for (int i = 0; i < 4; i++) {
  84.             v.lin = pos.lin + dL[i];
  85.             v.col = pos.col + dC[i];
  86.  
  87.             if (isInMat(v) && !mat[v.lin][v.col]) {
  88.                 mat[v.lin][v.col] = mat[pos.lin][pos.col] - 1;
  89.                 q.push(v);
  90.             }
  91.         }
  92.     }
  93.  
  94.     d.push({1, 1});
  95.     dp[1][1] = mat[1][1];
  96.  
  97.     while (!d.empty()) {
  98.         pos = d.top();
  99.         d.pop();
  100.  
  101.         for (int i = 0; i < 4; i++) {
  102.             v.lin = pos.lin + dL[i];
  103.             v.col = pos.col + dC[i];
  104.  
  105.             if (isInMat(v) && (!dp[v.lin][v.col] || dp[v.lin][v.col] && dp[pos.lin][pos.col] + mat[v.lin][v.col] < dp[v.lin][v.col])) {
  106.                 dp[v.lin][v.col] = dp[pos.lin][pos.col] + mat[v.lin][v.col];
  107.                 d.push(v);
  108.             }
  109.         }
  110.     }
  111.  
  112.     fout << dp[m][n];
  113.     solve(m, n, 0);
  114.  
  115.     fout.close();
  116.     return 0;
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement