Monday, February 3, 2020

POJ.3268 Silver Cow Party

1.Problem
http://poj.org/problem?id=3268

2.Idea
From X to arbitrary node: Using Dijkstra algorithm
From arbitrary node to X: Reverse all edges direction, and using Dijkstra too.

3.Source
 #define MAX_E 10004  
 #define MAX_V 2003  
 struct edge { int from, to, cost; } es[MAX_E];  
 int V, E, X;  
 int cost[MAX_V][MAX_V];  
 int d[MAX_V];  
 int d1[MAX_V];  
 bool used[MAX_V];  
 void dijkstra(int s)  
 {  
      fill(d, d + V, INF);  
      fill(used, used + V, false);  
      d[s] = 0;  
      while (true) {  
           int v = -1;  
           for (int u = 0; u < V; u++) {  
                if (!used[u] && (v == -1 || d[v] > d[u])) v = u;  
           }  
           if (v == -1) break;  
           used[v] = true;  
           for (int u = 0; u < V; u++) {  
                d[u] = min(d[u], d[v] + cost[v][u]);  
           }  
      }  
 }  
 int main()  
 {  
      cin >> V >> E >> X;  
      X--;  
      for (int i = 0; i < V; i++)  
           for (int j = 0; j < V; j++) {  
                if (i == j) cost[i][j] = 0;  
                else cost[i][j] = INF;  
           }  
      for (int i = 0; i < E; i++) {  
           int a, b, t;  
           cin >> a >> b >> t;  
           a--;  
           b--;  
           cost[a][b] = t;  
      }  
      dijkstra(X);  
      for (int i = 0; i < V; i++) d1[i] = d[i];  
      for (int i = 0; i < V; i++)  
           for (int j = i + 1; j < V; j++) {  
                swap(cost[i][j], cost[j][i]);  
           }  
      dijkstra(X);  
      int ans = 0;  
      for (int i = 0; i < V; i++) {  
           ans = max(ans, d1[i] + d[i]);  
      }  
      cout << ans << endl;  
      return 0;  
 }  

No comments:

Post a Comment