예제 #1
0
파일: AOJ117e.cs 프로젝트: bunbook/AOJ
 public Carpenter(int nodeNum)
 {
     tawnNodes = new TawnNode[nodeNum];
     for (int i = 0; i < nodeNum; i++)
     {
         tawnNodes[i] = new TawnNode(i);
     }
 }
예제 #2
0
 public Carpenter(int tawnNum)
 {
     tawnMaps = new TawnNode[tawnNum];
     for (int i = 0; i < tawnNum; i++)
     {
         tawnMaps[i] = new TawnNode(i);
     }
 }
예제 #3
0
파일: AOJ117e.cs 프로젝트: bunbook/AOJ
        private void Dijkstra(int nodeNum, int start, int goal)
        {
            tawnNodes[start].cost = 0;

            while (true)
            {
                TawnNode currentNode = null;

                for (int i = 0; i < nodeNum; i++)
                {
                    TawnNode node = tawnNodes[i];
                    if (node.cost == Def.INF || node.done)
                    {
                        continue;
                    }

                    if (currentNode == null)
                    {
                        currentNode = node;
                    }

                    if (currentNode.cost > node.cost)
                    {
                        currentNode = node;
                    }
                }

                if (currentNode == null || currentNode.id == goal)
                {
                    break;
                }

                currentNode.done = true;

                foreach (var edge in currentNode.edges)
                {
                    TawnNode linkedNode = edge.Key;
                    if (linkedNode.done)
                    {
                        continue;
                    }
                    linkedNode.cost = Math.Min(linkedNode.cost, currentNode.cost + edge.Value);
                }
            }
        }
예제 #4
0
        private int SearchCheapestCost(TawnNode currentTawn, int currentCost, List <TawnNode> passedTawns, int goalTawnId)
        {
            if (currentTawn.id == goalTawnId)
            {
                /*
                 * string output = currentCost.ToString() + ": ";
                 * foreach (var town in passedTawns)
                 * {
                 *  output += town.ToString() + " ";
                 * }
                 * Console.WriteLine(output);
                 */
                return(currentCost);
            }
            List <TawnNode> newPassedTawns = new List <TawnNode>(passedTawns);

            newPassedTawns.Add(currentTawn);

            int cheapestCost = INF;

            foreach (var trafficCost in currentTawn.trafficCosts)
            {
                TawnNode nextTawn = trafficCost.Key;

                if (passedTawns.Contains(nextTawn))
                {
                    continue;
                }

                int totalCost = currentCost + trafficCost.Value;
                totalCost = SearchCheapestCost(nextTawn, totalCost, newPassedTawns, goalTawnId);

                cheapestCost = Math.Min(cheapestCost, totalCost);
            }
            return(cheapestCost);
        }