Advertisement
Guest User

Untitled

a guest
May 3rd, 2016
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | None | 0 0
  1. #include <cstdio>
  2. #include <queue>
  3. #include <vector>
  4. using std::vector;
  5. using std::priority_queue;
  6.  
  7. struct State {
  8.     int len, to;
  9.     State(int len, int to) : len(len), to(to) {}
  10.     bool operator <(const State &rhs) const {
  11.         if (len != rhs.len) {
  12.             return len > rhs.len;
  13.         }
  14.         return to < rhs.to;
  15.     }
  16. };
  17.  
  18. const int maxN = 300;
  19. int maxValue;
  20.  
  21. int N, M;
  22. vector<vector<State>> graph;
  23. priority_queue<State> pQueue;
  24. int distTo[maxN];
  25. bool used[maxN];
  26.  
  27. void dijkstra(int start) {
  28.     memset(distTo, 63, sizeof(distTo));
  29.     maxValue = distTo[0];
  30.     distTo[start] = 0;
  31.     int currVertex, to;
  32.     pQueue.push(State(0, start));
  33.     while (!pQueue.empty()) {
  34.         currVertex = pQueue.top().to;
  35.         pQueue.pop();
  36.         if (!used[currVertex]) {
  37.             used[currVertex] = true;
  38.             for (auto &state : graph[currVertex]) {
  39.                 to = state.to;     
  40.                 if (!used[to] && distTo[to] > distTo[currVertex] + state.len) {
  41.                     distTo[to] = distTo[currVertex] + state.len;
  42.                     pQueue.push(state);
  43.                 }
  44.             }
  45.         }
  46.     }
  47. }
  48.  
  49. int minCycle;
  50. vector<vector<int>> cycles;
  51. void DFS(int currVertex, int n, vector<int>& cycle) {
  52.     int v;
  53.     for (auto &state : graph[currVertex]) {
  54.         v = state.to;
  55.         if (v == 0 && n >= 2) {
  56.  
  57.         }
  58.         if (!used[v]) {
  59.             used[v] = true;
  60.             DFS(v, n + 1);
  61.             used[v] = false;
  62.         }
  63.     }
  64. }
  65. int main()
  66. {
  67.     scanf("%d %d", &N, &M);
  68.     int from, to, len, start;
  69.     graph.resize(N);
  70.     for (int i = 0; i < M; i++)
  71.     {
  72.         scanf("%d %d %d", &from, &to, &len);
  73.         graph[from - 1].push_back(State(len, to - 1));
  74.         graph[to - 1].push_back(State(len, from - 1));
  75.     }
  76.     dijkstra(0);
  77.     memset(used, 0, sizeof(used));
  78.     minCycle = maxValue;
  79.     used[0] = true;
  80.     DFS(0, 0, 0);
  81.     if (minCycle == maxValue) {
  82.         printf("-1\n");
  83.     }
  84.     else {
  85.         printf("%d\n", minCycle);
  86.     }
  87.     return 0;
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement