Dang_Quan_10_Tin

Untitled

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