dnhirapara2104

https://codeforces.com/gym/102644/problem/F

Jul 22nd, 2020
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.79 KB | None | 0 0
  1. /*
  2. * @Author: dnhirapara
  3. */
  4. #include <bits/stdc++.h>
  5.  
  6. using namespace std;
  7.  
  8. /************defination************/
  9.  
  10. #define endl "\n"
  11. #define log(x) cout << __LINE__ << ": " << #x << " -> " << (x) << endl;
  12. #define ll long long int
  13. #define ull unsigned long long int
  14. const long long int MOD = 1e9 + 7;
  15.  
  16. class Matrix
  17. {
  18. public:
  19.     vector<vector<ll>> mat;
  20.     ll row, column;
  21.     bool square_mat = false;
  22.  
  23.     Matrix(vector<vector<ll>> &a)
  24.     {
  25.         mat = a;
  26.         this->row = a.size();
  27.         this->column = a[0].size();
  28.     }
  29.  
  30.     Matrix(ll same = 2)
  31.     {
  32.         square_mat = true;
  33.         this->column = same;
  34.         this->row = same;
  35.         mat.resize(row, vector<ll>(column, 1e10));
  36.     }
  37.  
  38.     Matrix(ll row, ll column)
  39.     {
  40.         if (row == column)
  41.             square_mat = true;
  42.         this->row = row;
  43.         this->column = column;
  44.         mat.resize(row, vector<ll>(column, 1e10));
  45.     }
  46.     pair<ll, ll> get_size()
  47.     {
  48.         // cout << row << " " << column << endl;
  49.         return make_pair(row, column);
  50.     }
  51.     void make_identity()
  52.     {
  53.         for (ll i = 0; i < row; i++)
  54.         {
  55.             for (ll j = 0; j < column; j++)
  56.             {
  57.                 mat[i][j] = i == j;
  58.             }
  59.         }
  60.     }
  61.     void make_inf(ll inf = LLONG_MAX)
  62.     {
  63.         for (ll i = 0; i < row; i++)
  64.         {
  65.             for (ll j = 0; j < column; j++)
  66.             {
  67.                 mat[i][j] = inf;
  68.             }
  69.         }
  70.     }
  71.     void make_val(ll val = 0)
  72.     {
  73.         for (ll i = 0; i < row; i++)
  74.         {
  75.             for (ll j = 0; j < column; j++)
  76.             {
  77.                 mat[i][j] = val;
  78.             }
  79.         }
  80.     }
  81.     Matrix operator+(Matrix m)
  82.     {
  83.         if (this->row != m.row || this->column != m.column)
  84.         {
  85.             cerr << "Matrix : In addition operation both matrix should be of same dimension..." << endl;
  86.             exit(0);
  87.         }
  88.         Matrix res(m.row, m.column);
  89.         for (ll i = 0; i < m.row; i++)
  90.         {
  91.             for (ll j = 0; j < m.column; j++)
  92.             {
  93.                 res.mat[i][j] = m.mat[i][j] + this->mat[i][j];
  94.             }
  95.         }
  96.         return res;
  97.     }
  98.  
  99.     Matrix operator%(ll mod)
  100.     {
  101.         if (mod <= 0)
  102.         {
  103.             cerr << "Invalid mod value..."
  104.                  << "\n";
  105.             exit(0);
  106.         }
  107.         Matrix res(row, column);
  108.         for (ll i = 0; i < this->row; i++)
  109.         {
  110.             for (ll j = 0; j < this->column; j++)
  111.             {
  112.                 res.mat[i][j] = (mat[i][j]) % mod;
  113.             }
  114.         }
  115.         return res;
  116.     }
  117.  
  118.     Matrix operator*(const Matrix &m)
  119.     {
  120.         if (this->column != m.row)
  121.         {
  122.             cerr << "Matrix : In multplication operation first matrix column and seocnd matrix row should be same ..." << endl;
  123.             exit(0);
  124.         }
  125.         Matrix res(this->row, m.column);
  126.         for (ll i = 0; i < this->row; i++)
  127.         {
  128.             for (ll j = 0; j < m.column; j++)
  129.             {
  130.                 cout << "j" << endl;
  131.                 for (ll k = 0; k < m.row; k++)
  132.                 {
  133.                     ll temp = this->mat[i][k] + m.mat[k][j];
  134.                     cout << temp << endl;
  135.                     cout << res.mat[i][j] << endl;
  136.                     res.mat[i][j] = min(res.mat[i][j], temp);
  137.                     cout << "mini :" << res.mat[i][j] << endl;
  138.                 }
  139.             }
  140.         }
  141.         return res;
  142.     }
  143.  
  144.     Matrix multi()
  145.     {
  146.         return mat;
  147.     }
  148.  
  149.     vector<ll> &operator[](ll i)
  150.     {
  151.         return mat[i];
  152.     }
  153.  
  154.     void printMat()
  155.     {
  156.         for (ll i = 0; i < row; i++)
  157.         {
  158.             for (ll j = 0; j < column; j++)
  159.             {
  160.                 cout << mat[i][j] << " ";
  161.             }
  162.             cout << "\n";
  163.         }
  164.     }
  165.  
  166. private:
  167.     void add(ll &a, ll b)
  168.     {
  169.         a = b;
  170.     }
  171.     ll mul(ll a, ll b)
  172.     {
  173.         return min(a, b);
  174.     }
  175. };
  176.  
  177. // Matrix pre[32];
  178.  
  179. // void init(Matrix &mat)
  180. // {
  181. //     pre[0].make_identity();
  182. //     pre[1] = mat;
  183. //     for (ll i = 2; i < 32; ++i)
  184. //     {
  185. //         pre[i] = pre[i - 1] * pre[i - 1];
  186. //     }
  187. // }
  188.  
  189. // Matrix power(Matrix &a, long long x)
  190. // {
  191. //     init(a);
  192. //     Matrix res;
  193. //     res.make_identity();
  194. //     ll cnt = 1;
  195. //     while (x)
  196. //     {
  197. //         if (x & 1)
  198. //         {
  199. //             res = res * pre[cnt];
  200. //         }
  201. //         cnt += 1;
  202. //         x >>= 1;
  203. //     }
  204. //     return res;
  205. // }
  206.  
  207. Matrix power(Matrix a, long long x)
  208. {
  209.     Matrix res(a.row, a.column);
  210.     res.make_val(1e10);
  211.     while (x)
  212.     {
  213.         if (x & 1)
  214.         {
  215.             res = res * a;
  216.             cout << "@@@@@@@@@@@@@@@@printres@@@@@@@@@@@@@@@@@@" << endl;
  217.             res.printMat();
  218.             cout << "printend" << endl;
  219.         }
  220.         a = a * a;
  221.         cout << "print a" << endl;
  222.         a.printMat();
  223.         cout << "printend" << endl;
  224.         x >>= 1;
  225.     }
  226.     return res;
  227. }
  228.  
  229. //@time comp :
  230. //@space comp :
  231. int main()
  232. {
  233.     // ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
  234.     // cin.ignore(); must be there when using getline(cin, s)
  235.     ll tc = 1;
  236.     // cin >> tc;
  237.     while (tc--)
  238.     {
  239.         ll n, m, k;
  240.         cin >> n >> m >> k;
  241.         ll weight;
  242.         Matrix mat(n, n);
  243.         ll x, y;
  244.         for (ll i = 0; i < n; i++)
  245.         {
  246.             for (ll j = 0; j < n; j++)
  247.             {
  248.                 mat[i][j] = 1e10;
  249.             }
  250.         }
  251.         for (ll i = 0; i < m; i++)
  252.         {
  253.             cin >> x >> y >> weight;
  254.             mat[x - 1][y - 1] = weight;
  255.         }
  256.         // power(mat,k).printMat();
  257.         ll ans = 0;
  258.         Matrix ansmat = power(mat, k);
  259.         ansmat.printMat();
  260.     }
  261.     return 0;
  262. }
Add Comment
Please, Sign In to add comment