Advertisement
Guest User

Untitled

a guest
Nov 17th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <climits>
  5.  
  6. using namespace std;
  7.  
  8. int main() {
  9. int n, m, k, s;
  10. cin >> n >> m >> k >> s;
  11. s--;
  12. vector<pair<pair<int,int>,long long>> vertex;
  13. vector<vector<int>> graph(n);
  14. for (int i = 0; i < m; i++) {
  15. int a, b;
  16. long long w;
  17. cin >> a >> b >> w;
  18. a--; b--;
  19. graph[a].push_back(b);
  20. pair<int,int> p={a,b};
  21. pair<pair<int,int>,long long> elem = {p,w};
  22. vertex.emplace_back(elem);
  23. }
  24. vector<vector<long long>> d(k + 1,vector<long long> (n, LONG_LONG_MAX));
  25. d[0][s] = 0;
  26. for (int i = 1; i <= k; i++) {
  27. for (int j = 0; j < m; ++j) {
  28. if (d[i - 1][vertex[j].first.first] < LONG_LONG_MAX) {
  29. d[i][vertex[j].first.second] = min(d[i][vertex[j].first.second],
  30. d[i - 1][vertex[j].first.first] + vertex[j].second);
  31. }
  32. }
  33. }
  34. for (int i = 0; i < n; i++) {
  35. if (d[k][i] >= LONG_LONG_MAX) {
  36. cout << -1 << endl;
  37. }
  38. else {
  39. cout << d[k][i] << endl;
  40. }
  41. }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement