首页 > 其他分享 >POJ 2110(最小生成树)

POJ 2110(最小生成树)

时间:2022-10-25 12:36:31浏览次数:64  
标签:begin end father 最小 getfather edge POJ 2110 now


这题的思路就是找一个范围,看看这个范围是否可行

主流是二分Ans,我是先把点排序,求最小生成树检查首位的


Program P2110;
type
ed=record
u,v,w:longint;
end;
var
a:array[1..120,1..120] of longint;
edge:array[0..30000] of ed;
n,i,j,size,ans:longint;
now:longint;
father:array[0..10001] of longint;
b:array[0..121,0..121] of boolean;
procedure add(u,v,w:longint);
begin
inc(size);
edge[size].u:=u;
edge[size].v:=v;
edge[size].w:=w;

end;
procedure qsort(l,r:longint);
var
i,j,m:longint;
p:ed;
begin
i:=l;
j:=r;
m:=edge[(l+r) shr 1].w;
repeat
while edge[i].w<m do inc(i);
while edge[j].w>m do dec(j);
if i<=j then
begin
p:=edge[i];
edge[i]:=edge[j];
edge[j]:=p;
inc(i);
dec(j);
end;
until i>j;
if l<j then qsort(l,j);
if i<r then qsort(i,r);



end;
function getfather(x:longint):longint;
begin
if x=father[x] then exit(x);
father[x]:=getfather(father[x]);
exit(father[x]);
end;
function hash(x,y:longint):longint;
begin
exit((x-1)*n+y);
end;
begin
size:=0;
read(n);
for i:=1 to n do
for j:=1 to n do
begin
read(a[i,j]);
add(i,j,a[i,j]);
end;

// (0,n*n)
qsort(1,size);


edge[0].w:=edge[1].w-1;
ans:=110;
// writeln;
for i:=1 to size do
begin

if edge[i].w=edge[i-1].w then continue;
for j:=0 to n*n do father[j]:=j;
fillchar(b,sizeof(b),false);
for j:=i to size do
begin
b[edge[j].u,edge[j].v]:=true;
now:=hash(edge[j].u,edge[j].v);

if b[edge[j].u-1,edge[j].v] then
if getfather(now)<>getfather(now-n) then
begin
father[father[now]]:=father[father[now-n]];
if getfather(0)=getfather(n*n) then break;
end;

if b[edge[j].u+1,edge[j].v] then
if getfather(now)<>getfather(now+n) then
begin
father[father[now]]:=father[father[now+n]];
if getfather(0)=getfather(n*n) then break;
end;

if b[edge[j].u,edge[j].v+1] then
if getfather(now)<>getfather(now+1) then
begin
father[father[now]]:=father[father[now+1]];
if getfather(0)=getfather(n*n) then break;
end;

if b[edge[j].u,edge[j].v-1] then
if getfather(now)<>getfather(now-1) then
begin
father[father[now]]:=father[father[now-1]];
if getfather(0)=getfather(n*n) then break;
end;



if getfather(1)=getfather(n*n) then break;

end;



if getfather(1)<>getfather(n*n) then break;
if ans>edge[j].w-edge[i].w then ans:=edge[j].w-edge[i].w;


end;
writeln(ans);



end.



标签:begin,end,father,最小,getfather,edge,POJ,2110,now
From: https://blog.51cto.com/u_15724837/5794479

相关文章

  • POJ 1951(空串特判)
    这题的教训是要特判空串ProgramP1951;vars:string;len,i,j:longint;b:array[0..10000]ofboolean;functionisdight(x:longint):boolean;beginif(x>=65)an......
  • POJ 1952(最长不下降子序列的个数)
    求一个序列的最长不下降子序列的长度,与个数(相同数列算1个)关键是如何判重。显然如果之前有一个尾数相同且长度相同的序列,哪么后一个包含前一个所有可能的序列相同的序列,故将......
  • POJ 2588(解析几何+并查集)
    题目就是早从左到右的路注意输入的实数这题图画好就行,别像我一开始把图弄反就成从上开始找,若找到一个与下边界相邻的就无解,找到与左边相邻的记圆与左边界相交的下边的点(相当......
  • POJ 3844(同余)
    果断同余……D[j]-D[i] mod k=0D[j]=D[i]求有多少相等数对,用队列O(n)ProgramP3844;constmaxn=50000;maxd=1000000;varans,t,f,n,i,j:longint;d:array[0........
  • POJ 3122(二分答案)
    二分答案……Programpie;constlef=0.00001;vart,n,f,i,j:longint;r:array[1..10000]oflongint;s:array[1..10000]ofdouble;maxs:double;procedures......
  • POJ 3842(质数判断)
    7!=5040所以这题直接求质数比打一千万的表都快这提高诉我们阶乘其实不算大&看(算)清数据规模Programcc;varn,t,len,i,j,ans:longint;s:string;b:array[0..9]oflong......
  • POJ 1125(Floyd)
    裸FloydProgramP1125;constmaxn=100;maxedge=10;NULL=-2139062144;varn,i,j,k,m,v,t,ans:longint;f:array[1..maxn,1..maxn]oflongint;functionmax(a,b:......
  • POJ 1700(过河问题)
    玩过《雷顿》就知道这题可以贪心小等于2人:1,2->3人时:1,3->1<-1,2->1<-否则:1,2->2<-max1,max2->1<-OR:1,max1->1<-2,max2->2<-于是数据规模-2ProgramP1700;vart,n,i,j:long......
  • POJ 3264(STRMQ)
    forj:=1toln(n)/ln(2)  fori:=1ton-(1shlj)+1do    f[i,j]:=min(f[i,j-1],f[i+(1shl(j-1),j-1];f[l,r]:=min(f[l,j],f[r-(1shlj)+1,j];j=ln(r-l+1......
  • POJ 3748(C++的16进制读法 %x)
    P党写几小时的程序C++才几行……首先P的位运算有上限2^30此时即便是int64也会因为补码坑死人的到1shl31时 int64是负数故这个时候不能shr为多出好多位造成以......