Ejemplo n.º 1
0
        public void Run(int f)
        {
            cost = new long[I]; for (int i = 0; i < I; ++i)
            {
                cost[i] = INF;
            }
            cost[f] = 0;
            var pq = new PQueue <edge>(); pq.Enqueue(new edge(f, 0L));

            while (!pq.Empty())
            {
                var cur = pq.Dequeue();
                if (cost[cur.to] < cur.cost)
                {
                    continue;
                }
                for (int i = 0; i < Adj[cur.to].Count; ++i)
                {
                    var tmp = Adj[cur.to][i];
                    if (cost[tmp.to] > cur.cost + tmp.cost)
                    {
                        cost[tmp.to] = cur.cost + tmp.cost;
                        pq.Enqueue(new edge(tmp.to, cost[tmp.to]));
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public long MinCost(int f, int t)
        {
            cost = new long[I]; for (int i = 0; i < I; ++i)
            {
                cost[i] = INF;
            }
            cost[f] = 0;
            var pq = new PQueue <Tuple <int, long> >(); pq.Enqueue(Tuple.Create(f, 0L));//(from,cost)

            while (pq.Count > 0)
            {
                var cur = pq.Dequeue();
                if (cost[cur.Item1] < cur.Item2)
                {
                    continue;
                }
                for (int i = 0; i < Adj[cur.Item1].Count; ++i)
                {
                    var tmp = Adj[cur.Item1][i];
                    if (cost[tmp.Item1] > cost[cur.Item1] + tmp.Item2)
                    {
                        cost[tmp.Item1] = cost[cur.Item1] + tmp.Item2; pq.Enqueue(Tuple.Create(tmp.Item1, cost[tmp.Item1]));
                    }
                }
            }
            return(cost[t]);
        }
Ejemplo n.º 3
0
    class Dijkstra : ShortestPath {//?????????,????
        public long MinCost(int f, int t)
        {
            cost = new long[I]; cost.Set(INF); cost[f] = 0;
            var pq = new PQueue <Tuple <int, long> >(); pq.Enqueue(Tuple.Create(f, 0L));//(from,cost)

            while (pq.Count > 0)
            {
                var cur = pq.Dequeue();
                if (cost[cur.Item1] < cur.Item2)
                {
                    continue;
                }
                Adj[cur.Item1].Count.REP(i => {
                    var tmp = Adj[cur.Item1][i];
                    if (cost[tmp.Item1] > cost[cur.Item1] + tmp.Item2)
                    {
                        cost[tmp.Item1] = cost[cur.Item1] + tmp.Item2; pq.Enqueue(Tuple.Create(tmp.Item1, cost[tmp.Item1]));
                    }
                });
            }
            return(cost[t]);
        }
Ejemplo n.º 4
0
        public long GetMinCost(long f, long t)
        {
            long[] w = new long[Index];
            M.REP(Index, i => w[i] = INF); w[f] = 0;
            var pq = new PQueue <Tuple <long, long> >(); pq.Enqueue(Tuple.Create(0L, f));//cost<-from

            while (pq.Count > 0)
            {
                var c = pq.Dequeue(); if (c.Item2 < c.Item1)
                {
                    continue;
                }
                M.REP(Index, i => {
                    long w2i = m[c.Item2, i] == INF ? INF : c.Item1 + m[c.Item2, i];
                    if (w2i < w[i])
                    {
                        w[i] = w2i; pq.Enqueue(Tuple.Create(w2i, (long)i));
                    }
                });
            }
            return(w[t]);
        }