poj 2485 Highways

来源:互联网 发布:objective c 高级编程 编辑:程序博客网 时间:2024/05/18 06:16

#include <cstdio>#include <vector>#include <algorithm>using namespace std;#define N 1000int bing[N];struct coord{int x, y;int quan;};bool cmp(const coord a, const coord b){return a.quan < b.quan;}int search(int a){if (a == bing[a])return a;elsereturn bing[a] = search(bing[a]);}bool getin(int a, int b){int t1, t2;t1 = search(a);t2 = search(b);if (t1 != t2){bing[t1] = t2;return true;}return false;}int main(){int i;cin >> i;while (i--){int a, b, ge, quan;vector<coord> map;int sb;coord ak_47;int count = 0;scanf("%d", &ge);for (a = 1; a <= ge; a++){bing[a] = a;for (b = 1; b <= ge; b++){scanf("%d", &quan);if (quan != 0 && a > b){ak_47.quan = quan;ak_47.x = a;ak_47.y = b;map.push_back(ak_47);}}}sort(map.begin(), map.end(), cmp);for (int a = 0; a != map.size(); a++){//cout << map[a].quan << endl;if (getin(map[a].x, map[a].y)){count++;if (count == ge - 1)sb = map[a].quan;}}printf("%d\n", sb);//system("pause");}}

一道最小生成树的水题,很简单就能做出来。题意就是找出来最小生成树中的最大边,需要注意的是对边进行排序的时候注意不要排反了。

0 0
原创粉丝点击