Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm Floyd Warshall最短路径算法错误_Algorithm_Scala_Floyd Warshall - Fatal编程技术网

Algorithm Floyd Warshall最短路径算法错误

Algorithm Floyd Warshall最短路径算法错误,algorithm,scala,floyd-warshall,Algorithm,Scala,Floyd Warshall,问题陈述: 代码: 导入scala.io.StdIn_ 导入scala.collection.mutable 对象解{ def FloydWarshall(n:Int,adj:Array[Array[Int]]):Array[Array[Int]={ val距离:数组[Array[Int]]=adj 对于(k,程序使用值351作为无效距离的标记,这似乎是问题所在 尽管已知每条边的最大重量为350,但通过由两条或多条边组成的路径,仍然可以达到值为351的距离。程序使用值351作为无效距离的标记,这

问题陈述:

代码:

导入scala.io.StdIn_
导入scala.collection.mutable
对象解{
def FloydWarshall(n:Int,adj:Array[Array[Int]]):Array[Array[Int]={
val距离:数组[Array[Int]]=adj

对于(k,程序使用值351作为无效距离的标记,这似乎是问题所在


尽管已知每条边的最大重量为350,但通过由两条或多条边组成的路径,仍然可以达到值为351的距离。

程序使用值351作为无效距离的标记,这似乎是问题所在


虽然已知每条边的最大权重为350,但是,通过由两条或多条边组成的路径,仍然可以达到值为351的距离。

最有可能的问题是,假设使用有效路径无法达到距离351。@qwertyman问题陈述定义了权重r,其中constraint 1虽然350确实是单条边的最大重量,但距离351仍然可以通过由两条或多条边组成的路径到达。@qwertyman是的,这就是问题所在。将此作为答案提交,我将接受。我添加了一个答案,谢谢!最有可能的问题是假设距离351不能通过va到达lid路径。@QWERTIMAN问题陈述定义了权重r,约束条件为1。虽然350确实是单条边的最大权重,但通过由两条或多条边组成的路径仍然可以到达距离351。@QWERTIMAN是的,这就是问题所在。将此作为答案提交,我接受。我添加了一个答案,谢谢!
import scala.io.StdIn._
import scala.collection.mutable
object Solution {
    def FloydWarshall(n: Int, adj: Array[Array[Int]]): Array[Array[Int]] = {
        val distance: Array[Array[Int]] = adj
        for(k <- 0 until n){
            for(u <- 0 until n){
                for(v <- 0 until n){
                    distance(u)(v) = minimum(distance(u)(v), distance(u)(k) + distance(k)(v))
                }
            }
        }
        distance
    }

    def minimum(a: Int, b: Int):Int = {
        if(a < b){
            a
        } else {
            b
        }
    }

    def main(args: Array[String]) {
        var input: Array[Int] = readLine().split(" ").map(_.toInt)
        val n: Int = input(0)
        val m: Int = input(1)    
        val adj: Array[Array[Int]] = Array.fill(n, n)(351)

        for(_ <- 1 to m){
            input = readLine().split(" ").map(_.toInt)
            val u: Int = input(0)
            val v: Int = input(1)
            val w: Int = input(2)
            adj(u-1)(v-1) = w
        }

        for(i <- 0 until n){
            adj(i)(i) = 0
        }

        val q: Int = readInt()
        val distance: Array[Array[Int]] = FloydWarshall(n, adj)
        val results: mutable.ListBuffer[Int] = mutable.ListBuffer[Int]()
        for(_ <- 1 to q) {
            input = readLine().split(" ").map(_.toInt)
            val u: Int = input(0)
            val v: Int = input(1)
            val result: Int = if(distance(u-1)(v-1) == 351) -1 else distance(u-1)(v-1)    
            results += result
        }
        println(results.mkString("\n"))
    }
}
import scala.io.StdIn._
import scala.collection.mutable
import util.control.Breaks._
object Solution {
    def FloydWarshall(n: Int, adj: Array[Array[Int]]): Array[Array[Int]] = {
        val distance: Array[Array[Int]] = adj
        for(k <- 0 until n){
            for(u <- 0 until n){
                for(v <- 0 until n){
                        distance(u)(v) = minimum(distance(u)(v), distance(u)(k) + distance(k)(v))    
                }
            }
        }
        distance
    }

    def minimum(a: Int, b: Int):Int = {
        if(a < b){
            a
        } else {
            b
        }
    }

    def main(args: Array[String]) {
        var input: Array[Int] = readLine().split(" ").map(_.toInt)
        val n: Int = input(0)
        val m: Int = input(1)
        val infinity: Int = 350 * 399 + 1// maximum shortest path N-1 edges
        val adj: Array[Array[Int]] = Array.fill(n, n)(infinity)

        for(_ <- 1 to m){
            input = readLine().split(" ").map(_.toInt)
            val u: Int = input(0) - 1
            val v: Int = input(1) - 1
            val w: Int = input(2)
            adj(u)(v) = w
        }

        for(i <- 0 until n){
            adj(i)(i) = 0
        }

        val q: Int = readInt()
        val distance: Array[Array[Int]] = FloydWarshall(n, adj)
        val results: mutable.ListBuffer[Int] = mutable.ListBuffer[Int]()
        for(_ <- 1 to q) {
            input = readLine().split(" ").map(_.toInt)
            val u: Int = input(0) - 1 
            val v: Int = input(1) - 1
            val result: Int = if(distance(u)(v) == infinity) -1 else distance(u)(v)
            results += result
        }
        println(results.mkString("\n"))
    }
}