Agri-Net_usaco3.1.1_poj1258_最小生成树

本文介绍了一种基于Kruskal算法解决最小生成树问题的方法,通过实例详细解释了如何使用邻接矩阵和并查集来寻找连接所有节点所需的最短路径。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Description


Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.

Input


The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.

Output


For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.

Source


USACO 102

题目大意


给出n*n的邻接矩阵,求这n个点的最小生成树

思路


因为我比较懒所以最小生成树用了kruskal,写了邻接表+并查集优化
不是很理解为什么写c++比较优美了现在

代码/pas

type
  edge=record
    x,y,w:longint;
  end;
var
  a,b,n,i,k:longint;
  f,v:array[1..200]of longint;
  e:array[1..10000]of edge;
  min,ans:longint;
procedure qsort(l,r:longint);
var
  x,y,key:longint;
  temp:edge;
begin
  if l>=r then exit;
  x:=l;
  y:=r;
  key:=e[l+random(r-l+1)].w;
  repeat
    while  (e[x].w<key) do inc(x);
    while  (e[y].w>key) do dec(y);
    if x<=y then
    begin
      temp:=e[x];
      e[x]:=e[y];
      e[y]:=temp;
      inc(x);
      dec(y);
    end;
  until x>y;
  qsort(l,y);
  qsort(x,r);
end;
function find(x:longint):longint;
var
  y,w,root:longint;
begin
  y:=x;
  while f[y]<>0 do y:=f[y];
  root:=y;
  y:=x;
  while f[y]<>0 do
  begin
    w:=f[y];
    f[y]:=root;
    y:=w;
  end;
  find:=root;
end;
procedure union(x,y:longint);
begin
  if find(x)<>find(y) then
  f[x]:=y;
end;
procedure init;
var
  i,j:Longint;
begin
  readln(n);
  for i:=1 to n do
  for j:=1 to n do
  with e[(i-1)*n+j] do
  begin
    read(w);
    x:=i;
    y:=j;
  end;
  for i:=1 to n do v[i]:=i;
  fillchar(f,sizeof(f),0);
  ans:=0;
end;
begin
  while not seekeof do
  begin
    init;
    qsort(1,n*n);
    k:=0; 
    for i:=1 to n*n-1 do
    if k=n then break
    else
    begin
      a:=find(e[i].x);
      b:=find(e[i].y);
      if a<>b then
      begin
        ans:=ans+e[i].w;
        union(a,b);
        inc(k);
      end;
    end;
    writeln(ans);
  end;
end.

代码/c++:

/*
ID:wjp13241
PROG:agrinet
LANG:C++
*/
#include <stdio.h>
#include <algorithm>
using namespace std;
struct edge
{
    int x,y,w;
};
edge e[10002];
int f[101];
int maxE=0;
bool cmp(edge x,edge y)
{
    return x.w<y.w;
}
void add(int x,int y,int w)
{
    e[++maxE]=(edge){x,y,w};
}
int find(int x)
{
    if (!f[x])
        return x;
    f[x]=find(f[x]);
    return f[x];
}
int main()
{
    freopen("agrinet.in","r",stdin);
    freopen("agrinet.out","w",stdout);
    int n,k=0,ans=0;
    scanf("%d",&n);
    for (int i=1;i<=n;i++)
        for (int j=1;j<=n;j++)
        {
            int tmp;
            scanf("%d",&tmp);
            if (tmp)
                add(i,j,tmp);
        }
    sort(e+1,e+maxE+1,cmp);
    for (int i=1;i<maxE;i++)
    {
        if (find(e[i].x)!=find(e[i].y))
        {
            f[find(e[i].x)]=find(e[i].y);
            ans+=e[i].w;
            k++;
        }
        if (k==n-1)
            break;
    }
    printf("%d\n",ans);
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值