Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

why ac

Posted by fucku at 2009-01-02 22:57:51 on Problem 2421
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

//package kruskal;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Scanner;

/**
 *
 * @author yepin
 */
class MySet
{
    ArrayList <Integer> set = new ArrayList<Integer>();
}

class Edge
{
    int u;
    int v;
    int weight;
}

class Map
{
    int [][] cost = new int [Kruskal.MaxSize][Kruskal.MaxSize];
}

class MyComparator implements Comparator
{
    public int compare(Object o1, Object o2)
    {
        Edge a = (Edge)o1;
        Edge b = (Edge)o2;
        return a.weight-b.weight;
    }
}
class Kruskal
{
    /*
     *  MST-Kruskal(G,w)
     *      A = null
     *      for each vertex v in V[G]
     *          do MAKE-SET(v)
     *      sort the edges of E into nondecreasing order by weight up
     *      for each edge (u,v) in E ,taken in nondecreasing order by weight
     *          do if FIND_SET(u) != FIND_SET(v)
     *              then (u,v) add to A
     *                  UNION(u,v)
     *      return 
     */
    final static int MaxSize = 101;
    boolean [][] mst = new boolean [MaxSize][MaxSize];
    Map map = new Map();
    ArrayList <MySet> sets = new ArrayList<MySet>();
    ArrayList <Edge> edges = new ArrayList<Edge>();
    int N;
    int minicost;
    
    Kruskal(int n,Map m)
    {
        minicost = 0;
        N = n;
        this.map = m;
        init();
    }
    
    public void init()
    {
        initMST();
        initSet();
        initEdge();
    }
    
    public void initMST()
    {
        for(int i=0;i<=N;i++)
        {
            for(int j=0;j<=N;j++)
            {
                mst[i][j] = false;
            }
        }
    }
    
    public void initSet()
    {
        for(int i=1;i<=N;i++)
        {
            MySet s = new MySet();
            s.set.add(i);
            sets.add(s);
        }
    }
    
    public void initEdge()
    {
        for(int i=1;i<=N;i++)
        {
            for(int j= i+1;j<=N;j++)
            {
                Edge e = new Edge();
                e.u = i;
                e.v = j;
                e.weight = map.cost[i][j];
                edges.add(e);
            }
        }
    }
    
    public int find(int v)
    {
        for(int i=0;i<sets.size();i++)
        {
            if(sets.get(i).set.contains(v))
            {
                return i;
            }
        }
        return -1;
    }
    
    public void union(int u,int v,int fu,int fv)
    {
        MySet t = new MySet();
        for(int i=0;i<sets.get(fu).set.size();i++)
        {
            t.set.add(sets.get(fu).set.get(i));
        }
        for(int i=0;i<sets.get(fv).set.size();i++)
        {
            t.set.add(sets.get(fv).set.get(i));
        }
        int big = (fu>fv)?fu:fv;
        int small = (fu<fv)?fu:fv;
        sets.remove(big);
        sets.remove(small);
        sets.add(t);
    }
    /*
     * this is a shit
     */
    public int miniCost()
    {
        return minicost;
    }
    /*
     * another is a shit
     */
    public void kruskal()
    {
        Comparator comp = new MyComparator();
        Collections.sort(edges,comp);
        for(int i=0;i<edges.size();i++)
        {
            int u = edges.get(i).u;
            int v = edges.get(i).v;
            if(mst[u][v])    continue;
            int fu = find(u);
            int fv = find(v);
            if(fu != fv)
            {
                mst[u][v] = true;
                mst[v][u] = true;
                union(u,v,fu,fv);
                minicost += map.cost[u][v];
            }
        }
    }
}
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        new Main().run();
    }
    public void run()
    {
        Scanner scan = new Scanner(System.in);
        Map newmap = new Map();
        int n = scan.nextInt();

        for(int i=1;i<=n;i++)
        {
            for(int j=1;j<=n;j++)
            {
                newmap.cost[i][j] = scan.nextInt();
            }
        }
        Kruskal k = new Kruskal(n,newmap);
        int m = scan.nextInt();
        for(int i=0;i<m;i++)
        {
            int u = scan.nextInt();
            int v = scan.nextInt();
            int fu = k.find(u);
            int fv = k.find(v);
            if(fu != fv)
            {
                k.mst[u][v] = true;
                k.mst[v][u] = true;
                k.union(v, u,fu,fv);
            }
        }
        k.kruskal();
        System.out.println(k.miniCost());
    }
}



Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator