Exemplo 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]));
                    }
                }
            }
        }
Exemplo 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]);
        }
Exemplo n.º 3
0
        static void Main(string[] args)
        {
            //Создадим массив с беспорядочным расположением сотрудников
            Work[] empsRandom =
            {
                new Work(Employes.Manager,    100, 10),
                new Work(Employes.Supervisor, 101, 20),
                new Work(Employes.Worker,     102, 30),
                new Work(Employes.Manager,    103, 40),
                new Work(Employes.Supervisor, 104, 50),
                new Work(Employes.Manager,    105, 10),
                new Work(Employes.Manager,    106, 20),
                new Work(Employes.Worker,     107, 30),
                new Work(Employes.Supervisor, 108, 40),
                new Work(Employes.Worker,     109, 50)
            };

            //Запишем данные в файл
            Write(empsRandom);

            //Запичем данные в очередь приоритетов
            PQueue <Work> empsOrder = new PQueue <Work>();

            Read(empsOrder);

            //Запишем упорядоченные данные в файл
            int[] summTime = new int[3];
            Work  item;

            using (StreamWriter Write = new StreamWriter("OrderList.txt"))
            {
                Write.WriteLine("Должность\t\tНомер\tВремя\n");

                while (!empsOrder.IsEmpty)
                {
                    item = empsOrder.PDequeue();

                    summTime[(int)item.Employ] += item.Time;

                    Write.WriteLine(Enum.GetName(item.Employ.GetType(), item.Employ).PadLeft(10)
                                    + item.ID.ToString().PadLeft(10) + item.Time.ToString().PadLeft(5));
                }

                Write.WriteLine("\n");
                Write.WriteLine($"Manager       {summTime[(int)Employes.Manager]}");
                Write.WriteLine($"Worker        {summTime[(int)Employes.Worker]}");
                Write.WriteLine($"Supervisor    {summTime[(int)Employes.Supervisor]}");
            }
        }
Exemplo n.º 4
0
        static void Read(PQueue <Work> emps)
        {
            Work item = new Work(Employes.Manager, 0, 0);

            FileInfo f = new FileInfo("work.txt");

            using (BinaryReader br = new BinaryReader(f.OpenRead()))
            {
                while (br.BaseStream.Position < br.BaseStream.Length)
                {
                    item.Employ = (Employes)br.ReadInt32();
                    item.ID     = br.ReadInt32();
                    item.Time   = br.ReadInt32();
                    br.BaseStream.Position++;

                    emps.PEnqueue((int)item.Employ, item);
                }
            }
        }
Exemplo n.º 5
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]);
        }
Exemplo n.º 6
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]);
        }
Exemplo n.º 7
0
 //void Stream() { Test(); io.writeFlush(); }
 void Test()
 {
     var pq = new PQueue <int>(1);
 }
Exemplo n.º 8
0
 //void Stream() { Test(); io.writeFlush(); }
 void Test()
 {
     var pq = new PQueue <edge>(1);
 }