Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define TASK "CROBOT"
- using namespace std;
- const long long oo = 1e18 + 100;
- const int N = 1e2;
- vector<vector< long long > > w, d;
- int n, m, k;
- void ResizeMatrix(vector<vector<long long> > & x)
- {
- x.resize(n + 1);
- for (int i = 1; i <= n; ++ i)
- {
- x[i].resize(n + 1, oo);
- }
- }
- vector<vector<long long> > operator *= (vector<vector<long long> > & a, vector<vector<long long> > & b)
- {
- vector<vector<long long> > c;
- ResizeMatrix(c);
- for (int i = 1; i <= n; ++ i)
- {
- for (int j = 1; j <= n; ++ j)
- {
- for (int k = 1; k <= n; ++ k)
- {
- if (a[i][k] < oo && b[k][j] < oo)
- {
- c[i][j] = min(c[i][j], a[i][k] + b[k][j]);
- }
- }
- }
- }
- swap(a, c);
- return a;
- }
- void read()
- {
- cin >> n >> m >> k;
- ResizeMatrix(w);
- ResizeMatrix(d);
- for (int i = 1; i <= n; ++ i)
- {
- d[i][i] = 0;
- }
- for (int i = 1; i <= m; ++ i)
- {
- int u, v;
- long long weight;
- cin >> u >> v >> weight;
- w[u][v] = min(w[u][v], weight);
- }
- }
- void solve()
- {
- vector<vector<long long> > a = w;
- for (; k > 0; k /= 2)
- {
- if (k & 1)
- d *= a;
- a *= a;
- }
- long long ans = oo;
- for (int i = 1; i <= n; ++ i)
- {
- ans = min(ans, d[i][i]);
- }
- if (ans == oo)
- {
- cout << -1;
- return ;
- }
- cout << ans;
- }
- int main()
- {
- ios_base::sync_with_stdio(false);
- cin.tie(nullptr);
- freopen(TASK".INP", "r", stdin);
- freopen(TASK".OUT", "w", stdout);
- int t = 1;
- bool typetest = false;
- if (typetest)
- {
- cin >> t;
- }
- for (int __ = 1; __ <= t; ++ __)
- {
- read();
- solve();
- }
- }
Add Comment
Please, Sign In to add comment