Dang_Quan_10_Tin

Untitled

Jun 3rd, 2020
223
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.45 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <cstdio>
  4. #include <queue>
  5. #include <cstring>
  6. #define task "FAIR"
  7. using namespace std;
  8. using ll = long long;
  9. using ld = long double;
  10.  
  11. const int N = 1e3 + 2;
  12. int m, n, k;
  13. int a[N][N], l[N][N], r[N][N];
  14. int x[] = {-1, 0, 1, 0}, y[] = {0, 1, 0, -1};
  15. int dis[N][N], zz[N][N];
  16. const int Inf = 1e9 + 7;
  17.  
  18. void Read(){
  19.     cin >> m >> n >> k;
  20.     for(int i = 1; i <= m; ++i)
  21.         for(int j = 1; j <= n; ++j)
  22.             cin >> a[i][j];
  23. }
  24.  
  25. void Init(){
  26.     for(int i = 1; i <= m; ++i)
  27.         fill_n(dis[i], N, Inf),
  28.         dis[i][1] = a[i][1];
  29.     memset(zz, 0, sizeof zz);
  30. }
  31.  
  32. struct Tque{
  33.     int x, y, w, z;
  34.     Tque() {}
  35.     Tque(int m, int n, int p, int q){
  36.         x = m;
  37.         y = n;
  38.         w = p;
  39.         z = q;
  40.     }
  41.     bool Valid(){
  42.         return dis[x][y] == w && z == zz[x][y];
  43.     }
  44.     bool operator < (const Tque& a) const{
  45.         return w > a.w
  46.             ||(w == a.w && z < a.z);
  47.     }
  48. };
  49.  
  50. bool ck[N][N];
  51.  
  52. bool Relax(const Tque& s, const int& x, const int& y){
  53.     if(x > m || x < 1 || y > n || y < 1 || ck[x][y])
  54.         return false;
  55.     int res = s.w + (s.z != 0 ? 0 : a[x][y]);
  56.     if(dis[x][y] > res || (dis[x][y] == res && max(s.z - 1, 0) > zz[x][y])){
  57.         dis[x][y] = res;
  58.         ck[x][y] = 0;
  59.         return true;
  60.     }
  61.     return false;
  62. }
  63.  
  64. pair<int, int> Dijkstra(){
  65.     Init();
  66.     priority_queue<Tque> s;
  67.     for(int i = 1; i <= m; ++i)
  68.         s.push(Tque(i, 1, dis[i][1], (a[i][1] == 0) * k)),
  69.         zz[i][1] = (a[i][1] == 0) * k;
  70.     while(s.size()){
  71.         Tque c = s.top();
  72.         s.pop();
  73.         if(!c.Valid() || ck[c.x][c.y])
  74.             continue;
  75.         //cerr << c.x << " " << c.y << "\n";
  76.         ck[c.x][c.y] = 1;
  77.         if(c.y == n)
  78.             return {c.x, c.y};
  79.         for(int i = 0; i < 4; ++i)
  80.             if(Relax(c, c.x + x[i], c.y + y[i])){
  81.                 int u = c.x + x[i], v = c.y + y[i];
  82.                 s.push(Tque(u, v, dis[u][v], a[u][v] == 0 ? k : max(0, c.z - 1)));
  83.                 zz[u][v] = a[u][v] == 0 ? k : max(0, c.z - 1);
  84.                 l[u][v] = c.x;
  85.                 r[u][v] = c.y;
  86.             }
  87.     }
  88. }
  89.  
  90. void trace(int x, int y){
  91.     if(l[x][y] == 0 || r[x][y] == 0){
  92.         cout << x << " " << y << "\n";
  93.         return;
  94.     }
  95.     //cerr << x << " " << y << "\n";
  96.     trace(l[x][y], r[x][y]);
  97.     cout << x << " " << y << "\n";
  98.  
  99. }
  100.  
  101. void Solve(){
  102.     pair<int, int> s = Dijkstra();
  103.     //cerr << "*\n";
  104.     cout << dis[s.first][s.second] << "\n";
  105.     //cerr << "#\n" << s.first << " " << s.second << "\n";
  106.     trace(s.first, s.second);
  107. }
  108.  
  109. main(){
  110.     ios::sync_with_stdio(0);
  111.     cin.tie(0);
  112.     cout.tie(0); if(fopen(task".INP", "r"))
  113.     freopen(task".INP", "r", stdin),
  114.     freopen(task".OUT", "w", stdout);
  115.     Read();
  116.     Solve();
  117. }
Advertisement
Add Comment
Please, Sign In to add comment