POJ 1251 解题报告
来源:互联网 发布:淘宝优惠券在哪里领取 编辑:程序博客网 时间:2024/06/12 12:42
这道题是最小生成树。代码和之前一样,只是输入格式差别。
thestoryofsnow1251Accepted184K0MSC++2217B/* ID: thestor1 LANG: C++ TASK: poj1251 */#include <iostream>#include <fstream>#include <cmath>#include <cstdio>#include <cstring>#include <limits>#include <string>#include <vector>#include <list>#include <set>#include <map>#include <queue>#include <stack>#include <algorithm>#include <cassert>using namespace std;void makeset(const int N, std::vector<int> &parent, std::vector<int> &rank){for (int u = 0; u < N; ++u){parent[u] = u;rank[u] = 0;}}int find(int u, std::vector<int> &parent){if (parent[u] != u){parent[u] = find(parent[u], parent);}return parent[u];}void union_set(int u, int v, std::vector<int> &parent, std::vector<int> &rank){int ru = find(u, parent);int rv = find(v, parent);if (ru == rv){return;}if (rank[ru] < rank[rv]){parent[ru] = rv;}else if (rank[rv] < rank[ru]){parent[rv] = ru;}else{parent[ru] = rv;rank[rv]++;}}class Edge{public:int u, v, w;Edge() {}Edge(int u, int v, int w) : u(u), v(v), w(w) {}bool operator< (const Edge &rhs) const{if (this->w != rhs.w){return this->w < rhs.w;}return this->u < rhs.u || (this->u == rhs.u && this->v < rhs.v);}};int kruskal(const int N, std::vector<Edge> &edges){std::vector<int> parent(N);std::vector<int> rank(N);makeset(N, parent, rank);sort(edges.begin(), edges.end());int mst = 0;for (int i = 0; i < edges.size(); ++i){Edge edge = edges[i];if (find(edge.u, parent) != find(edge.v, parent)){mst += edge.w;union_set(edge.u, edge.v, parent, rank);}}return mst;}int main(){while (true){int N;scanf("%d", &N);if (N == 0){break;}std::vector<Edge> edges;for (int u = 0; u < N - 1; ++u){char c;int n;scanf(" %c %d", &c, &n);assert (c == 'A' + u);for (int i = 0; i < n; ++i){int w;scanf(" %c %d", &c, &w);assert(c > 'A' + u && c < 'A' + N);edges.push_back(Edge(u, c - 'A', w));}}int mst = kruskal(N, edges);printf("%d\n", mst);}return 0; }
0 0
- POJ 1251 解题报告
- poj数百篇解题报告
- poj 1141解题报告
- poj 1001解题报告
- poj 1011 解题报告
- poj 1087 解题报告
- poj 2153解题报告
- poj 2051解题报告
- POJ 1167 解题报告
- poj 1026 解题报告
- POJ 1077 解题报告
- POJ 1042 解题报告
- POJ 1095 解题报告
- POJ 1159解题报告
- poj 2312解题报告
- POJ 1001解题报告
- POJ 1002解题报告
- POJ 1006解题报告
- 黑马程序员-----String,StringBuffer,StringBulider
- C实现万年历
- OpenCV-2.4.2 安装三步曲
- 百度蜘蛛抓取规则 Baiduspider
- Linux平台下使用rman进行oracle数据库迁移
- POJ 1251 解题报告
- 新版本Android SDK 找不到adb.exe的
- listview的重写方法getview的优化
- URAL - 1788 On the Benefits of Umbrellas(水题)
- 黑马程序员-----List,Set集合
- [C++Primer]第2章.变量和基本类型
- Oracle 数据库安装规范--database所在路径和归档目录所在路径必须分开
- dede 代码调用 参数问题
- Servlet源码解析:Session、Request以及Response