Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #NoTrayIcon
- ; A Day in the Life of a Network Router
- ; http://redd.it/287jxh
- Global $Form, $idInput, $idOutput, $idButton
- $Form = GUICreate("A Day in the Life of a Network Router", 440, 360)
- GUISetFont(10, 1000, 0, "Courier New", $Form)
- $idInput = GUICtrlCreateEdit("Input", 20, 20, 400, 300)
- $idOutput = GUICtrlCreateInput("", 20, 320, 260, 20)
- $idButton = GUICtrlCreateButton("Find Path", 300, 320, 120, 20)
- GUISetState(@SW_SHOW, $Form)
- Do
- Switch GUIGetMsg()
- Case -3
- ExitLoop
- Case $idButton
- GUICtrlSetData($idOutput, FindPath(GUICtrlRead($idInput)))
- EndSwitch
- Until False
- Exit
- Func FindPath($sInput)
- Local $sOutput = "", $aInput, $n, $x, $y
- $aInput = StringSplit($sInput, @CRLF, 1)
- $n = Number($aInput[1])
- If ($aInput[0] <> $n+2) Then Return "ERROR"
- Local $anMatrix[$n][$n]
- Local $nStart, $nEnd, $aTemp, $i
- $aTemp = StringSplit($aInput[$aInput[0]], " ")
- If ($aTemp[0] <> 2) Then Return "ERROR"
- $nStart = Asc($aTemp[1]) - 65
- $nEnd = Asc($aTemp[2]) - 65
- For $x = 0 To $n-1
- $aTemp = StringSplit($aInput[$x+2], ",")
- If ($aTemp[0] <> $n) Then Return "ERROR"
- For $y = 0 To $n-1
- ; I had to restructure the graph a bit,
- ; because the algorithm wouldn't have worked otherwise.
- If ($x = $y) Then
- $anMatrix[$x][$y] = 0
- Else
- $anMatrix[$x][$y] = $aTemp[$y+1]
- If ($anMatrix[$x][$y] < 0) Then $anMatrix[$x][$y] = 10e10
- EndIf
- Next
- Next
- Local $anNext = Warshall($anMatrix)
- Return Path($nStart, $nEnd, $anNext)
- EndFunc
- Func Warshall($dist)
- Local $n = UBound($dist), $next[$n][$n]
- Local $u, $v, $k, $i, $j
- For $u = 0 To $n-1
- For $v = 0 To $n-1
- If ($dist[$u][$v] > 0) Then
- $next[$u][$v] = $v
- Else
- $next[$u][$v] = Null
- EndIf
- Next
- Next
- For $k = 0 To $n-1
- For $i = 0 To $n-1
- For $j = 0 To $n-1
- If ($dist[$i][$k] + $dist[$k][$j] < $dist[$i][$j]) Then
- $dist[$i][$j] = $dist[$i][$k] + $dist[$k][$j]
- $next[$i][$j] = $next[$i][$k]
- EndIf
- Next
- Next
- Next
- Return $next
- EndFunc
- Func Path($u, $v, ByRef $next)
- If ($next[$u][$v] = Null) Then
- Return ""
- EndIf
- Local $path = Chr(65 + $u)
- While ($u <> $v)
- $u = $next[$u][$v]
- $path &= Chr(65 + $u)
- WEnd
- Return $path
- EndFunc
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement