예제 #1
0
        public static unsafe int Answer(int numOfServers, int targetServer, int[,] connectionTimeMatrix)
        {
            int    sz  = numOfServers + 10;
            State *s   = stackalloc State[sz];
            int *  ixs = stackalloc int[sz];

            PQ.Init(s, ixs);
            bool *v  = stackalloc bool[sz];
            int * sp = stackalloc int[sz];

            sp[0] = 0;
            for (int i = 1; i < numOfServers; i++)
            {
                sp[i] = 1 << 30;
            }
            PQ.Push(0, 0);
            int cn, p;

            while (PQ.edx != 1)
            {
                cn = PQ.Pop(out p);
                if (cn == targetServer)
                {
                    return(p);
                }
                if (v[cn])
                {
                    continue;
                }
                v[cn] = true;
                for (int i = 1; i < numOfServers; i++)
                {
                    if (v[i])
                    {
                        continue;
                    }
                    int np = sp[cn] + connectionTimeMatrix[cn, i];
                    if (np < sp[i])
                    {
                        if (PQ.Contains(i))
                        {
                            PQ.Update(i, np);
                        }
                        else
                        {
                            PQ.Push(i, np);
                        }
                        sp[i] = np;
                    }
                }
            }
            return(connectionTimeMatrix[0, targetServer]);
        }