using System;
using System.Collections.Generic;
class Program
{
struct GraphInfoDef
{
internal char FromPos;
internal char ToPos;
internal int Kyori;
}
static void Main()
{
var GraphInfoList = new List<GraphInfoDef>();
GraphInfoList.Add(new GraphInfoDef() { FromPos = 'A', ToPos = 'B', Kyori = 4 });
GraphInfoList.Add(new GraphInfoDef() { FromPos = 'A', ToPos = 'D', Kyori = 2 });
GraphInfoList.Add(new GraphInfoDef() { FromPos = 'B', ToPos = 'C', Kyori = 1 });
GraphInfoList.Add(new GraphInfoDef() { FromPos = 'C', ToPos = 'F', Kyori = 6 });
GraphInfoList.Add(new GraphInfoDef() { FromPos = 'D', ToPos = 'C', Kyori = 1 });
GraphInfoList.Add(new GraphInfoDef() { FromPos = 'D', ToPos = 'E', Kyori = 4 });
GraphInfoList.Add(new GraphInfoDef() { FromPos = 'E', ToPos = 'F', Kyori = 5 });
WarshallFloyd(GraphInfoList);
}
//ワーシャルフロイド法でノードの組み合わせごとの最短距離を求める
static void WarshallFloyd(List<GraphInfoDef> pGraphInfoList)
{
var MinKyoriDict = new Dictionary<string, int>();
//初期化処理
for (char I = 'A'; I <= 'F'; I++) {
for (char J = 'A'; J <= 'F'; J++) {
string wkKey = string.Format("{0},{1}", I, J);
if (I == J) {
MinKyoriDict[wkKey] = 0;
}
else {
int wkInd = pGraphInfoList.FindIndex(A => A.FromPos == I && A.ToPos == J);
if (wkInd >= 0)
MinKyoriDict[wkKey] = pGraphInfoList[wkInd].Kyori;
else MinKyoriDict[wkKey] = int.MaxValue / 2;
}
}
}
//ワーシャルフロイド法
for (char K = 'A'; K <= 'F'; K++) {
for (char I = 'A'; I <= 'F'; I++) {
for (char J = 'A'; J <= 'F'; J++) {
string wkKey1 = string.Format("{0},{1}", I, J);
string wkKey2 = string.Format("{0},{1}", I, K);
string wkKey3 = string.Format("{0},{1}", K, J);
int CurrKyori1 = MinKyoriDict[wkKey1];
int CurrKyori2 = MinKyoriDict[wkKey2];
int CurrKyori3 = MinKyoriDict[wkKey3];
if (CurrKyori1 > CurrKyori2 + CurrKyori3)
MinKyoriDict[wkKey1] = CurrKyori2 + CurrKyori3;
}
}
}
foreach (var EachPair in MinKyoriDict) {
if (EachPair.Value == 0) continue;
if (EachPair.Value == int.MaxValue / 2) continue;
Console.WriteLine("{0}={1}", EachPair.Key, EachPair.Value);
}
}
}