chillurbrain

12.2.2. Лабиринт знаний

May 26th, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <climits>
  4. using namespace std;
  5.  
  6. struct edge
  7. {
  8.     int start, finish, cost;
  9. };
  10.  
  11. int main()
  12. {
  13.     int n, m;
  14.     cin >> n >> m;
  15.     vector<edge>e;
  16.     for (int i = 0; i<m; i++)
  17.     {
  18.         edge a;
  19.         cin >> a.start >> a.finish >> a.cost;
  20.         a.start--;
  21.         a.finish--;
  22.         e.push_back(a);
  23.     }
  24.     vector<int> length(n, INT_MIN);
  25.     vector<int> p(n, -1);
  26.     length[0] = 0;
  27.     int x;
  28.     for (int i = 0; i<n; i++)
  29.     {
  30.         x = -1;
  31.         for (int j = 0; j<m; j++)
  32.         if ((length[e[j].start]>INT_MIN) && (length[e[j].finish]<length[e[j].start] + e[j].cost))
  33.         {
  34.             length[e[j].finish] = length[e[j].start] + e[j].cost;
  35.             p[e[j].finish] = e[j].start;
  36.             x = 1;
  37.         }
  38.     }
  39.     vector<int> path;
  40.     int k = 0;
  41.     for (int i = n - 1; i != -1; i = p[i])
  42.     {
  43.         k++;
  44.         path.push_back(i);
  45.         if (k>n) break;
  46.     }
  47.     if (length[n - 1] == INT_MIN) cout << ":(" << endl;
  48.     else
  49.     if ((x != -1) && ((path.back() != 0) || (path.front() != n - 1) || (k>n))) cout << ":)" << endl;
  50.     else cout << length[n - 1] << endl;
  51.     system("pause");
  52.     return 0;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment