[题解] uva 10369 Arctic Network(kruskal最小生成树)

- 传送门 -

 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1310

# 10369 - Arctic Network Time limit: 3.000 seconds | [Root](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=0) | [![Submit](https://uva.onlinejudge.org/components/com_onlinejudge/images/button_submit.png "Submit")](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=submit_problem&problemid=1310&category=) [![Problem Stats](https://uva.onlinejudge.org/components/com_onlinejudge/images/button_stats.png "Problem Stats")](https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=problem_stats&problemid=1310&category=) [![uDebug](https://uva.onlinejudge.org/components/com_onlinejudge/images/button_debug.png)](https://www.udebug.com/UVa/10369) [![Download as PDF](https://uva.onlinejudge.org/components/com_onlinejudge/images/button_pdf.png "Download as PDF")](https://uva.onlinejudge.org/external/103/10369.pdf) | ###(题面见pdf)
  ### - 题意 -  求最小生成树上第k大的边.   ### - 思路 -  难度大概在读题吧, 但我直接看题解的中文题意23333  可以把符合条件的第s大转化为第n-s小, kruskal到第n-s条边时直接得到答案.    细节见代码.   ### - 代码 - ```c++ #include #include #include #include using namespace std;

typedef double db;
const int N = 500 + 5;

struct edge {
int x, y;
db v;
}W[N*N];

int F[N], X[N], Y[N];
int cas, s, n, sz, cnt;

bool cmp (edge a, edge b) { return a.v < b.v; }

db distance(int i, int j) {
return sqrt((X[i] - X[j]) * (X[i] - X[j]) * 1.0 + (Y[i] - Y[j]) * (Y[i] - Y[j]) * 1.0);
}

void init() {
sz = cnt = 0;
for (int i = 1; i <= n; ++i)
F[i] = i;
}

int find(int x) { return x == F[x] ? x : F[x] = find(F[x]); }

void kruskal() {
for (int i = 1; i <= sz; ++i) {
int fx = find(W[i].x), fy = find(W[i].y);
if (fx == fy) continue;
cnt++;
if (cnt == s) {
printf("%.2lf\n", W[i].v);
break;
}
F[fx] = fy;
}
}

int main() {
scanf("%d", &cas);
for (int i = 1; i <= cas; ++i) {
scanf("%d%d", &s, &n);
if (s >= n) {
printf("0.00\n");
continue;
}
init();
for (int j = 1; j <= n; ++j) {
scanf("%d%d", &X[j], &Y[j]);
for (int k = 1; k < j; ++k) {
W[++sz].x = k; W[sz].y = j; W[sz].v = distance(k, j);
}
}
s = n - s;
sort(W + 1, W + sz + 1, cmp);
kruskal();
}
return 0;
}

posted @ 2017-08-14 09:44  lstttt  阅读(124)  评论(0编辑  收藏  举报