日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

HUD - 4463 Outlets

發布時間:2024/4/18 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 HUD - 4463 Outlets 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

題目鏈接

題意

N個商店最短修多長的路使得每個商店相連,其中apple和nike必須直接相連。

AC

  • kruskal求最短路
#include <iostream> #include <stdio.h> #include <vector> #include <map> #include <string.h> #define mem(a, b) memset(a, b, sizeof(a)) #define ll long long #include <cmath> #include <algorithm> #define N 100005 #define P pair<int,int> #define mk(a, b) make_pair(a, b) int inf = 0x3f3f3f3f; using namespace std; struct ac{double len;int s, e; }b[3000]; struct point{int x, y; }a[100]; int pre[100]; void init() {for(int i = 0; i < 100; ++i) {pre[i] = i;} } int find(int x) {if (x != pre[x]) {return pre[x] = find(pre[x]);}return pre[x]; } void join(int x, int y) {if (x < y) pre[y] = x, find(y);else pre[x] = y, find(x);for (int i = 0; i < 100; ++i) {find(i);} } int main(){// freopen("in.txt", "r", stdin);int nike, apple;int n;while (scanf("%d", &n), n){scanf("%d%d", &nike, &apple);for (int i = 0; i < n; ++i) {scanf("%d%d", &a[i].x, &a[i].y);}init();double ans = 0;int now = 0;for (int i = 0; i < n; ++i) {for (int j = i + 1; j < n; ++j) {double len = sqrt((a[i].x - a[j].x) * (a[i].x - a[j].x) + (a[i].y - a[j].y) * (a[i].y - a[j].y));b[now].s = i;b[now].e = j;b[now].len = len;if (i == nike - 1 && j == apple - 1) {ans += b[now].len;// 將 nike 和 apple 相連join(i, j);}now++;}}sort(b, b + now, [&](const ac &a, const ac & b){if ((a.len - b.len) > 1e-6) {return 0;}else {return 1;}});for (int i = 0; i < now; ++i){int s = b[i].s, e = b[i].e;if (find(s) != find(e)) {// 將老大連起來join(find(s), find(e));ans += b[i].len;}}printf("%.2lf\n", ans);}return 0; }

總結

以上是生活随笔為你收集整理的HUD - 4463 Outlets的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。