POJ-1258-Agri Ned
阿新 • • 發佈:2019-01-31
sort fat 農場 per 代碼 poj typedef += class
鏈接:https://vjudge.net/problem/POJ-1258#author=fuxianda
題意:
有n個農場,已知這n個農場都互相相通,有一定的距離,現在每個農場需要裝光纖,問怎麽安裝光纖能將所有農場都連通起來,並且要使光纖距離最小,輸出安裝光纖的總距離
任意兩個村莊之間的距離小於 100,000.
思路:
最小生成樹
代碼:
#include <iostream> #include <memory.h> #include <string> #include <istream> #include <sstream> #include <vector> #include <stack> #include <algorithm> #include <map> #include <queue> #include <math.h> #include <cstdio> using namespace std; typedef long long LL; const int MAXM = 10000+10; const int MAXN = 100+10; struct Node { double _x,_y; }node[MAXN]; struct Path { int _l,_r; double _value; bool operator < (const Path & that)const{ return this->_value < that._value; } }path[MAXM]; int Father[MAXN]; double a[MAXN]; int n, m, v; int s, p; int pos;//邊數 int Get_F(int x) { return Father[x] = (Father[x] == x) ? x : Get_F(Father[x]); } void Init(int x) { for (int i = 1;i <= x;i++) Father[i] = i; } double Get_Len(Node a,Node b) { return sqrt((a._x - b._x) * (a._x - b._x) + (a._y - b._y) * (a._y - b._y)); } int main() { while (cin >> n) { Init(n); pos = 0; for (int i = 1;i <= n;i++) { for (int j = 1;j <= n;j++) { cin >> v; path[++pos]._l = i; path[pos]._r = j; path[pos]._value = v; } } sort(path + 1,path + 1 + pos); LL res = 0; for (int i = 1;i <= pos;i++) { int tl = Get_F(path[i]._l); int tr = Get_F(path[i]._r); if (tl != tr) { Father[tl] = tr; res += path[i]._value; } } cout << res << endl; } return 0; }
POJ-1258-Agri Ned