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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

扩散(洛谷-P1661)

發(fā)布時間:2025/3/17 编程问答 18 豆豆
生活随笔 收集整理的這篇文章主要介紹了 扩散(洛谷-P1661) 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

題目描述

一個點每過一個單位時間就會向四個方向擴散一個距離,如圖。

兩個點a、b連通,記作e(a,b),當(dāng)且僅當(dāng)a、b的擴散區(qū)域有公共部分。連通塊的定義是塊內(nèi)的任意兩個點u、v都必定存在路徑e(u,a0),e(a0,a1),…,e(ak,v)。給定平面上的n給點,問最早什么時刻它們形成一個連通塊。

輸入輸出格式

輸入格式:

第一行一個數(shù)n,以下n行,每行一個點坐標(biāo)。

對于20%的數(shù)據(jù),滿足1≤N≤5; 1≤X[i],Y[i]≤50;

對于100%的數(shù)據(jù),滿足1≤N≤50; 1≤X[i],Y[i]≤10^9。

輸出格式:

一個數(shù),表示最早的時刻所有點形成連通塊。

輸入輸出樣例

輸入樣例#1:

2
0 0
5 5

輸出樣例#1:

5

思路:任意兩點的距離為兩點間的曼哈頓距離,假設(shè)任意兩點間有邊,那么問題就轉(zhuǎn)換為求所有點構(gòu)成的最小生成樹中最長的邊+1 再除以 2,因此利用 Kruskal 求出最小生成樹,找到其中最長邊即可

源代碼

#include<iostream> #include<cstdio> #include<cstdlib> #include<string> #include<cstring> #include<cmath> #include<ctime> #include<algorithm> #include<utility> #include<stack> #include<queue> #include<vector> #include<set> #include<map> #include<bitset> #define PI acos(-1.0) #define INF 0x3f3f3f3f #define LL long long #define Pair pair<int,int> const double EPS = 1E-10; const int MOD = 1E9+7; const int N = 1000000+5; const int dx[] = {-1,1,0,0,-1,-1,1,1}; const int dy[] = {0,0,-1,1,-1,1,-1,1}; using namespace std; struct Edge{int x,y;int dis;Edge(){}Edge(int x,int y,int dis):x(x),y(y),dis(dis){}bool operator < (const Edge &rhs)const{return dis<rhs.dis;} }edge[N]; struct Node{int x,y;Node(){}Node(int x,int y):x(x),y(y){} }node[N]; int tot; int father[N]; int getDis(int i,int j){return abs(node[i].x-node[j].x)+abs(node[i].y-node[j].y); } int Find(int x){return father[x]==x?x:father[x]=Find(father[x]); } int Kruskal(int n){for(int i=1;i<=n;i++)father[i]=i;int num=0;int res=-INF;for(int i=1;i<=tot;i++){int x=edge[i].x;int y=edge[i].y;x=Find(x);y=Find(y);if(x!=y){res=max(res,edge[i].dis);num++;father[x]=y;}if(num==n)break;}return res; } int main(){int n;scanf("%d",&n);for(int i=1;i<=n;i++)scanf("%d%d",&node[i].x,&node[i].y);for(int i=1;i<n;i++){for(int j=i+1;j<=n;j++){edge[++tot].x=i;edge[tot].y=j;edge[tot].dis=getDis(i,j);}}sort(edge+1,edge+1+tot);int res=Kruskal(n);printf("%d\n",(res+1)/2);return 0; }

?

總結(jié)

以上是生活随笔為你收集整理的扩散(洛谷-P1661)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。