Advertisement
Hippskill

Untitled

Jan 22nd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.97 KB | None | 0 0
  1. #define _USE_MATH_DEFINES
  2. #include<stdio.h>
  3. #include<iostream>
  4. #include<vector>
  5. #include<cmath>
  6. #include<algorithm>
  7. #include<memory.h>
  8. #include<map>
  9. #include<set>
  10. #include<queue>
  11. #include<list>
  12. #include<sstream>
  13. #include<cstring>
  14. #include<numeric>
  15. #include<limits.h>
  16. #include<stack>
  17. using namespace std;
  18.  
  19.  
  20.  
  21.  
  22. const int N = 1e2 + 1;
  23. const int S = 1e6 + 1;
  24. const int delta = 1e5;
  25. const int INF = 1e9;
  26.  
  27. int s;
  28. vector<int> g[N];
  29. set<int> gs[N];
  30.  
  31. bool mu = false;
  32. bool k = false;
  33.  
  34. void addEdge(int a, int b) {
  35.     g[a].push_back(b);
  36.     g[b].push_back(a);
  37.     if (a == b) {
  38.         k = true;
  39.     }
  40.     if (gs[a].count(b) != 0) {
  41.         mu = true;
  42.     }
  43.     if (gs[b].count(a) != 0) {
  44.         mu = true;
  45.     }
  46.     gs[a].insert(b);
  47.     gs[b].insert(a);
  48. }
  49.  
  50.  
  51. bool u[N];
  52. bool c = false;
  53. int p[N];
  54.  
  55. void dfsCycle(int v) {
  56.     u[v] = true;
  57.     for (auto to : g[v]) {
  58.         if (u[to]) {
  59.             if (p[v] != to) {
  60.                 c = true;
  61.                 return;
  62.             }
  63.         }
  64.         else {
  65.             p[to] = v;
  66.             dfsCycle(to);
  67.         }
  68.     }
  69. }
  70.  
  71. int d[N][N];
  72.  
  73. int n, m;
  74. int main() {
  75. #ifndef ONLINE_JUDGE
  76.     freopen("input.txt", "r", stdin);
  77. #endif
  78.     scanf("%d%d%d", &n, &m, &s);
  79.     for (int i = 0; i < m; i++) {
  80.         int a, b, c;
  81.         scanf("%d%d%d", &a, &b, &c);
  82.         addEdge(a - 1, b - 1);
  83.         d[a - 1][b - 1] = -c;
  84.         d[b - 1][a - 1] = -c;
  85.     }
  86.     for (int i = 0; i < n; ++i) {
  87.         if (!u[i]) {
  88.             dfsCycle(i);
  89.             if (c)
  90.                 break;
  91.         }
  92.     }
  93.  
  94.     if (c || mu || k) {
  95.         printf("YES");
  96.         return 0;
  97.     }
  98.  
  99.     for (int i = 0; i < n; i++) {
  100.         for (int j = 0; j < n; j++) {
  101.             if (i == j) d[i][j] = 0;
  102.             else if (d[i][j] == 0) d[i][j] = INF;
  103.         }
  104.     }
  105.  
  106.  
  107.     int res = 0;
  108.     for (int k = 0; k < n; ++k) {
  109.         for (int i = 0; i < n; ++i) {
  110.             for (int j = 0; j < n; ++j) {
  111.                 if (d[i][k] < INF && d[k][j] < INF) {
  112.                     d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
  113.                 }
  114.             }
  115.         }
  116.     }
  117.  
  118.     for (int i = 0; i < n; i++) {
  119.         for (int j = 0; j < n; j++) {
  120.             if (d[i][j] != -INF) {
  121.                 res = max(res, -d[i][j]);
  122.             }
  123.         }
  124.     }
  125.     printf(res >= s ? "YES" : "NO");
  126.  
  127.  
  128.     return 0;
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement