UVa Problem 117 - The Postal Worker Rings Once
2011-12-21 21:10
316 查看
// UVa Problem 117 - The Postal Worker Rings Once // Verdict: Accepted // Submission Date: 2011-12-21 // UVa Run Time: 0.040s // // 版权所有(C)2011,邱秋。metaphysis # yeah dot net // // [解题方法] // 图论,欧拉路径。 // 根据题意,度为奇数的节点数为 1 的情况不可能出现,度为奇数的节点数目只可能为 0 或者 2。当度为 // 奇数的节点数为 0 时,很好办,走遍所有边仅一次的最小花费是所有边权的和,而当度为奇数的数目为 2 // 时,最小花费是所有边权的和加上这两个度数为奇数的节点之间的最短路径长度。 // 为了简便,可以采用 floyd_warshall 算法来得到两点间的最短路径长度。 #include <iostream> using namespace std; #define MAXV 27 #define INT_MAX (1 << 10) // 邻接矩阵。 struct graph { int weight[MAXV][MAXV]; int nvertices; }; // 利用 Floyd-Warshall 算法计算所有点对之间的最短距离。 void floyd_warshall(graph * g) { int vertices = g->nvertices; for (int i = 1; i <= vertices; i++) g->weight[i][i] = 0; for (int k = 1; k <= vertices; k++) for (int i = 1; i <= vertices; i++) for (int j = 1; j <= vertices; j++) g->weight[i][j] = min(g->weight[i][j], g->weight[i][k] + g->weight[k][j]); } int main(int argc, char const *argv[]) { graph g; int length; string street; int crossroad[MAXV]; length = 0; g.nvertices = MAXV - 1; for (int i = 1; i < MAXV; i++) { crossroad[i] = 0; for (int j = 1; j < MAXV; j++) g.weight[i][j] = INT_MAX; } while (cin >> street) { if (street == "deadend") { int start, end; bool found = false; for (int i = 1; i < MAXV; i++) if (crossroad[i] & 1) { if (found == false) start = i; else end = i; found = true; } if (found) { floyd_warshall(&g); cout << (length + g.weight[start][end]) << endl; } else cout << length << endl; length = 0; g.nvertices = MAXV - 1; for (int i = 1; i < MAXV; i++) { crossroad[i] = 0; for (int j = 1; j < MAXV; j++) g.weight[i][j] = INT_MAX; } } else { length += street.length(); g.weight[street[0] - 'a' + 1][street[street.length() - 1] - 'a' + 1] = street.length(); g.weight[street[street.length() - 1] - 'a' + 1][street[0] - 'a' + 1] = street.length(); crossroad[street[0] - 'a' + 1]++; crossroad[street[street.length() - 1] - 'a' + 1]++; } } return 0; }
相关文章推荐
- The Postal Worker Rings Once(UVA 117)最短路径—SPFA算法+邻接表
- poj 1237 The Postal Worker Rings Once
- POJ 1237 The Postal Worker Rings Once
- POJ1237 The Postal Worker Rings Once
- POJ 1237 The Postal Worker Rings Once
- POJ 1237 The Postal Worker Rings Once
- Once more unto the breach, dear friends, once more
- Unable to start debugging on the web server. The IIS worker process for the launched URL is not currently running
- Find the elements in the set that appears only once
- ccah-500 第35题 What do you have to do on the cluster to allow the worker node to join
- [CSS] Specify grid columns, rows, and areas at once with the grid-template shorthand
- [zt]Which are the 10 algorithms every computer science student must implement at least once in life?
- Find the element that appears once
- UVa Problem 10092 The Problem With the Problem Setter (命题者的难题)
- Find the element that appears once(查找出现一次的元素)
- CodeSign error: Certificate identity 'iPhone Developer:xxx appears more than once in the keychain.
- The difference between Web role and Worker role in windows azure platform
- once you dive into the logging
- ios解决bug--“Pushing the same view controller instance more than once is not supported”
- (引)The item "obj/Debug/xxx.resources" was specified more than once the "Resources