USACO_3_1_Agri-Net

本文介绍了一种使用Kruskal算法寻找最小生成树的方法,并提供了一个详细的Java实现案例。通过排序边并按需合并顶点集来构建最小生成树。
这倒题考你最小生成树算法,我参考了算法导论上的Kruskal算法,读者在网上查查


ContractedBlock.gifExpandedBlockStart.gifCode
ExpandedBlockStart.gifContractedBlock.gif/**//*
ID: sdjllyh1
PROG: agrinet
LANG: JAVA
complete date: 2008/12/28
author: LiuYongHui From GuiZhou University Of China
more articles: www.cnblogs.com/sdjls
*/


import java.io.*;
import java.util.*;

public class agrinet
ExpandedBlockStart.gifContractedBlock.gif
{
    
private static int n;
    
private static List edges = new ArrayList();//
    private static int cost = 0;//总花费,即题目所求

    
public static void main(String[] args) throws IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        init();
        run();
        output();
        System.exit(
0);
    }


    
private static void run()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
//根据边的距离排序
        Collections.sort(edges);
        
//依次考虑每条边
        Iterator it = edges.iterator();
        
while (it.hasNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            edge e 
= (edge)it.next();
            
//如果边的两个顶点属于不同的集合
            if (e.u.set != e.v.set)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//把此边加入最小生成树
                cost += e.w;
                
//合并两个集合
                union(e.u.set, e.v.set);
            }

        }

    }


    
private static void union(Set s1, Set s2)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        s1.addAll(s2);
//把s2集合中的元素加入s1中
        
//让s2中的每个顶点指向s1集合
        Iterator it = s2.iterator();
        
while (it.hasNext())
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            vertex v 
= (vertex)it.next();
            v.set 
= s1;
        }

    }


    
private static void init() throws IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        BufferedReader f 
= new BufferedReader(new FileReader("agrinet.in"));
        n 
= Integer.parseInt(f.readLine());

        vertex[] vertexes 
= new vertex[n];
        
for (int i = 0; i < n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            vertexes[i] 
= new vertex();
        }


        
for (int i = 0; i < n; i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
int j = 0;
            
while (j < n)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                StringTokenizer st 
= new StringTokenizer(f.readLine());
                
while (st.hasMoreTokens())
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    
int w = Integer.parseInt(st.nextToken());
                    
if (j > i)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
{
                        edges.add(
new edge(vertexes[i], vertexes[j], w));
                    }

                    j
++;
                }

            }

        }

        f.close();
    }


    
private static void output() throws IOException
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        PrintWriter out 
= new PrintWriter(new BufferedWriter(new FileWriter("agrinet.out")));
        out.println(cost);
        out.close();
    }

}


class edge implements Comparable
ExpandedBlockStart.gifContractedBlock.gif
{
    
public vertex u, v;//边的两个顶点
    public int w;//边的权值,即两个农场之间的距离
    public edge(vertex u, vertex v, int w)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
this.u = u;
        
this.v = v;
        
this.w = w;
    }


    
public int compareTo(Object arg0)
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        edge comObject 
= (edge)arg0;
        
if (this.w > comObject.w)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return 1;
        }

        
else if (this.w < comObject.w)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return -1;
        }

        
else
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
return 0;
        }

    }

}


class vertex
ExpandedBlockStart.gifContractedBlock.gif
{
    
public Set set = new HashSet();//包含这个(this)点的集合
    public vertex()
ExpandedSubBlockStart.gifContractedSubBlock.gif    
{
        
this.set.add(this);
    }

}

转载于:https://www.cnblogs.com/SDJL/archive/2008/12/28/1364078.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值