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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

atcoder 2643 切比雪夫最小生成树

發布時間:2025/3/17 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 atcoder 2643 切比雪夫最小生成树 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

There are N towns on a plane. The i-th town is located at the coordinates (xi,yi). There may be more than one town at the same coordinates.

You can build a road between two towns at coordinates (a,b) and (c,d) for a cost of min(|a?c|,|b?d|) yen (the currency of Japan). It is not possible to build other types of roads.

Your objective is to build roads so that it will be possible to travel between every pair of towns by traversing roads. At least how much money is necessary to achieve this?


Constraints
  • 2≤N≤105
  • 0≤xi,yi≤109
  • All input values are integers.
Input

Input is given from Standard Input in the following format:

N x1 y1 x2 y2 : xN yN Output

Print the minimum necessary amount of money in order to build roads so that it will be possible to travel between every pair of towns by traversing roads.

Sample Input 1 3 1 5 3 9 7 8 Sample Output 1 3

Build a road between Towns 1 and 2, and another between Towns 2 and 3. The total cost is 2+1=3 yen.

Sample Input 2 6 8 3 4 9 12 19 18 1 13 5 7 6 Sample Output 2 8

兩點之間的距離定義min( |a-b|,|c-d| ),就是切比雪夫距離;
我們要求這樣的定義下的最小生成樹;
考慮Kruskal算法:每次選取距離最小的邊加入其中且保證不能出現環;
那么我們分別按照x,y進行排序;
最后再統一排序即可;
此時進行普通的Kruskal即可;
#include<iostream> #include<cstdio> #include<algorithm> #include<cstdlib> #include<cstring> #include<string> #include<cmath> #include<map> #include<set> #include<vector> #include<queue> #include<bitset> #include<ctime> #include<deque> #include<stack> #include<functional> #include<sstream> //#include<cctype> //#pragma GCC optimize(2) using namespace std; #define maxn 2000005 #define inf 0x7fffffff //#define INF 1e18 #define rdint(x) scanf("%d",&x) #define rdllt(x) scanf("%lld",&x) #define rdult(x) scanf("%lu",&x) #define rdlf(x) scanf("%lf",&x) #define rdstr(x) scanf("%s",x) typedef long long ll; typedef unsigned long long ull; typedef unsigned int U; #define ms(x) memset((x),0,sizeof(x)) const long long int mod = 1e9 + 7; #define Mod 1000000000 #define sq(x) (x)*(x) #define eps 1e-4 typedef pair<int, int> pii; #define pi acos(-1.0) //const int N = 1005; #define REP(i,n) for(int i=0;i<(n);i++) typedef pair<int, int> pii; inline ll rd() {ll x = 0;char c = getchar();bool f = false;while (!isdigit(c)) {if (c == '-') f = true;c = getchar();}while (isdigit(c)) {x = (x << 1) + (x << 3) + (c ^ 48);c = getchar();}return f ? -x : x; }ll gcd(ll a, ll b) {return b == 0 ? a : gcd(b, a%b); } int sqr(int x) { return x * x; }/*ll ans; ll exgcd(ll a, ll b, ll &x, ll &y) {if (!b) {x = 1; y = 0; return a;}ans = exgcd(b, a%b, x, y);ll t = x; x = y; y = t - a / b * y;return ans; } */int n; int tot; struct node {int x, y;int d;node(){}node(int x,int y,int d):x(x),y(y),d(d){}}nd[maxn],nd2[maxn];bool cmp(node a, node b) {return a.x < b.x; } bool cmp2(node a, node b) {return a.y < b.y; }bool cmp3(node a, node b) {return a.d < b.d; }int fa[maxn]; void init() {for (int i = 0; i <= n; i++)fa[i] = i; } int findfa(int x) {if (x == fa[x])return x;return fa[x] = findfa(fa[x]); }void merge(int u, int v) {int p = findfa(u);int q = findfa(v);if (p != q) {fa[p] = q;} }bool chk(int x, int y) {if (findfa(x) == findfa(y))return true;else return false; }int kruskal() {int sum = 0;init();for (int i = 0; i < tot; i++) {node tmp = nd2[i];if (tmp.d == 0)merge(tmp.x, tmp.y);if (!chk(tmp.x, tmp.y)) {merge(tmp.x, tmp.y); sum += tmp.d;}}return sum; }int main() { // ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);cin >> n;for (int i = 1; i <= n; i++) {rdint(nd[i].x); rdint(nd[i].y);nd[i].d = i;}tot = 0;sort(nd + 1, nd + 1 + n, cmp);for (int i = 2; i <= n; i++) {nd2[tot++] = node(nd[i].d, nd[i - 1].d, nd[i].x - nd[i - 1].x);}sort(nd+1, nd + n+1, cmp2);for (int i = 2; i <= n; i++) {nd2[tot++] = node(nd[i].d, nd[i - 1].d, nd[i].y - nd[i - 1].y);}sort(nd2, nd2 + tot, cmp3);int sum = kruskal();cout << sum << endl;return 0; }

?

轉載于:https://www.cnblogs.com/zxyqzy/p/10319151.html

總結

以上是生活随笔為你收集整理的atcoder 2643 切比雪夫最小生成树的全部內容,希望文章能夠幫你解決所遇到的問題。

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