典型問題    次の典型問題へ    前の典型問題へ

典型問題(初級) F 最小全域木問題


問題へのリンク


C#のソース

using System;
using System.Collections.Generic;
using System.Linq;

class Program
{
    static string InputPattern = "InputX";

    static List<string> GetInputList()
    {
        var WillReturn = new List<string>();

        if (InputPattern == "Input1") {
            WillReturn.Add("5 7");
            WillReturn.Add("0 1 10");
            WillReturn.Add("0 4 30");
            WillReturn.Add("1 2 10");
            WillReturn.Add("1 4 20");
            WillReturn.Add("2 3 30");
            WillReturn.Add("4 2 20");
            WillReturn.Add("4 3 10");
            //50
        }
        else {
            string wkStr;
            while ((wkStr = Console.ReadLine()) != null) WillReturn.Add(wkStr);
        }
        return WillReturn;
    }

    struct EdgeInfoDef
    {
        internal int FromNode;
        internal int ToNode;
        internal int Cost;
    }

    static List<EdgeInfoDef> mEdgeInfoList = new List<EdgeInfoDef>();

    static int mN;

    static void Main()
    {
        List<string> InputList = GetInputList();

        int[] wkArr = { };
        Action<string> SplitAct = pStr =>
            wkArr = pStr.Split(' ').Select(pX => int.Parse(pX)).ToArray();

        SplitAct(InputList[0]);
        mN = wkArr[0];

        foreach (string EachStr in InputList.Skip(1)) {
            SplitAct(EachStr);

            int U = wkArr[0];
            int V = wkArr[1];
            int C = wkArr[2];

            EdgeInfoDef WillAdd;
            WillAdd.FromNode = U;
            WillAdd.ToNode = V;
            WillAdd.Cost = C;
            mEdgeInfoList.Add(WillAdd);
        }

        // クラスカル法で解く
        var InsUnionFind = new UnionFind();
        for (int I = 0; I <= mN - 1; I++) {
            InsUnionFind.MakeSet(I);
        }

        // 枝をコストの昇順にソート
        mEdgeInfoList.Sort((a, b) => a.Cost.CompareTo(b.Cost));

        long SumCost = 0;
        foreach (EdgeInfoDef EachEdgeInfo in mEdgeInfoList) {
            int FromNode = EachEdgeInfo.FromNode;
            int ToNode = EachEdgeInfo.ToNode;
            int RootNode1 = InsUnionFind.FindSet(EachEdgeInfo.FromNode);
            int RootNode2 = InsUnionFind.FindSet(EachEdgeInfo.ToNode);
            if (RootNode1 != RootNode2) {
                InsUnionFind.Unite(FromNode, ToNode);
                SumCost += EachEdgeInfo.Cost;
            }
        }
        Console.WriteLine(SumCost);
    }
}

#region UnionFind
// UnionFindクラス
internal class UnionFind
{
    private class NodeInfoDef
    {
        internal int ParentNode;
        internal int Rank;
    }
    private Dictionary<int, NodeInfoDef> mNodeInfoDict =
        new Dictionary<int, NodeInfoDef>();

    // 要素が1つである木を森に追加
    internal void MakeSet(int pNode)
    {
        NodeInfoDef WillAdd = new NodeInfoDef();
        WillAdd.ParentNode = pNode;
        WillAdd.Rank = 0;
        mNodeInfoDict[pNode] = WillAdd;
    }

    // 合併処理
    internal void Unite(int pX, int pY)
    {
        int XNode = FindSet(pX);
        int YNode = FindSet(pY);
        int XRank = mNodeInfoDict[XNode].Rank;
        int YRank = mNodeInfoDict[YNode].Rank;

        if (XRank > YRank) {
            mNodeInfoDict[YNode].ParentNode = XNode;
        }
        else {
            mNodeInfoDict[XNode].ParentNode = YNode;
            if (XRank == YRank) {
                mNodeInfoDict[YNode].Rank++;
            }
        }
    }

    // ノードを引数として、木の根を取得
    internal int FindSet(int pTargetNode)
    {
        // 根までの経路上のノードのList
        var PathNodeList = new List<int>();

        int CurrNode = pTargetNode;
        while (CurrNode != mNodeInfoDict[CurrNode].ParentNode) {
            PathNodeList.Add(CurrNode);
            CurrNode = mNodeInfoDict[CurrNode].ParentNode;
        }

        // 経路圧縮 (親ポインタの付け替え)
        foreach (int EachPathNode in PathNodeList) {
            mNodeInfoDict[EachPathNode].ParentNode = CurrNode;
        }
        return CurrNode;
    }

    internal void DebugPrint()
    {
        foreach (var EachPair in mNodeInfoDict.OrderBy(pX => pX.Key)) {
            Console.WriteLine("mNodeInfoDict[{0}].ParentNode={1}",
                EachPair.Key, EachPair.Value.ParentNode);
        }
    }
}
#endregion


解説

クラスカル法で解いてます。


類題

GRL_2_A: Minimum Spanning Tree