Exemplo n.º 1
0
        public int solve(int start, int end)
        {
            var first = _graph.vertex(start);
            var last  = _graph.vertex(end);

            first._dist = 0;

            List <Graph.VertexInfo> minList = new List <Graph.VertexInfo>();

            minList.Add(first);

            while (true)
            {
                Graph.VertexInfo min = null;

                foreach (var v in minList)
                {
                    if (min == null || min._dist > v._dist)
                    {
                        min = v;
                    }
                }

                if (min == null)
                {
                    break;
                }

                min._closed = true;
                minList.Remove(min);

                foreach (var e in min._edges)
                {
                    int w = 0;
                    int i = Graph.unpackEdge(e, out w);

                    var v = _graph._vertices[i];
                    if (v == null || v._closed)
                    {
                        continue;
                    }

                    w += min._dist;

                    if (v._dist < 0)
                    {
                        if (v != last)
                        {
                            minList.Add(v);
                        }
                        v._dist = w;
                    }
                    else if (v._dist > w)
                    {
                        v._dist = w;
                    }
                }
            }

            return(last._dist);
        }