示例#1
0
        static void Run()
        {
            int t = int.Parse(Console.ReadLine());

            while (t > 0)
            {
                adj = new Dictionary <int, List <Street> >();
                int price = int.Parse(Console.ReadLine());
                int n     = int.Parse(Console.ReadLine());
                int m     = int.Parse(Console.ReadLine());
                for (int i = 0; i < m; i++)
                {
                    string[] str = Console.ReadLine().Split();
                    int      a   = int.Parse(str[0]) - 1;
                    int      b   = int.Parse(str[1]) - 1;
                    int      l   = int.Parse(str[2]);
                    if (!adj.ContainsKey(a))
                    {
                        adj.Add(a, new List <Street>());
                    }
                    if (!adj.ContainsKey(b))
                    {
                        adj.Add(b, new List <Street>());
                    }
                    adj[a].Add(new Street()
                    {
                        endPoint = b, length = l
                    });
                    adj[b].Add(new Street()
                    {
                        endPoint = a, length = l
                    });
                }
                int[] dist = new int[n];
                for (int i = 0; i < n; i++)
                {
                    dist[i] = Int32.MaxValue;
                }
                dist[0] = 0;
                PriorityQ pq = new PriorityQ(n);
                pq.Insert(0, 0);
                bool[] visited = new bool[n];
                while (!pq.isEmpty())
                {
                    var item = pq.ExtractMin();
                    int val  = item.Value;
                    int d    = item.Key;
                    visited[val] = true;
                    foreach (var a in adj[val])
                    {
                        if (visited[a.endPoint])
                        {
                            continue;
                        }
                        int newDist = d + a.length;
                        if (a.length < dist[a.endPoint])
                        {
                            if (pq.Contains(a.endPoint))
                            {
                                dist[a.endPoint] = a.length;
                                pq.Update(a.endPoint, dist[a.endPoint]);
                            }
                            else
                            {
                                dist[a.endPoint] = a.length;
                                pq.Insert(a.endPoint, dist[a.endPoint]);
                            }
                        }
                    }
                }
                long finalLen = 0;
                for (int i = 0; i < n; i++)
                {
                    finalLen += dist[i];
                }
                Console.WriteLine(finalLen * price);
                t--;
            }
        }
示例#2
0
        static void Run()
        {
            int t = int.Parse(Console.ReadLine());

            while (t > 0)
            {
                string[] str = Console.ReadLine().Split();
                int      M   = int.Parse(str[0]);
                int      N   = int.Parse(str[1]);
                int[,] overPowerCost = new int[M, N];
                Dictionary <int, List <Cell> > dict = new Dictionary <int, List <Cell> >();
                for (int i = 0; i < M; i++)
                {
                    string[] strCost = Console.ReadLine().Split();
                    for (int j = 0; j < N; j++)
                    {
                        overPowerCost[i, j] = int.Parse(strCost[j]);
                    }
                }
                string[] strLastLine   = Console.ReadLine().Split();
                int      x             = int.Parse(strLastLine[0]);
                int      y             = int.Parse(strLastLine[1]);
                int      T             = int.Parse(strLastLine[2]);
                int      finalPosition = (x - 1) * N + (y - 1);
                for (int i = 0; i < M; i++)
                {
                    for (int j = 0; j < N; j++)
                    {
                        int key = i * N + j;
                        dict.Add(key, new List <Cell>());
                        if (i - 1 >= 0)
                        {
                            dict[key].Add(new Cell()
                            {
                                cost = overPowerCost[i - 1, j], positionId = (i - 1) * N + j
                            });
                        }
                        if (i + 1 < M)
                        {
                            dict[key].Add(new Cell()
                            {
                                cost = overPowerCost[i + 1, j], positionId = (i + 1) * N + j
                            });
                        }
                        if (j - 1 >= 0)
                        {
                            dict[key].Add(new Cell()
                            {
                                cost = overPowerCost[i, j - 1], positionId = i * N + (j - 1)
                            });
                        }
                        if (j + 1 < N)
                        {
                            dict[key].Add(new Cell()
                            {
                                cost = overPowerCost[i, j + 1], positionId = i * N + (j + 1)
                            });
                        }
                    }
                }
                int[]     dist = new int[M * N];
                PriorityQ pq   = new PriorityQ(M * N);
                for (int i = 0; i < M * N; i++)
                {
                    dist[i] = Int32.MaxValue;
                }
                dist[0] = overPowerCost[0, 0];
                pq.Insert(0, dist[0]);
                while (!pq.isEmpty())
                {
                    KeyValuePair <int, int> currentCell = pq.ExtractMin();
                    if (currentCell.Value == finalPosition)
                    {
                        break;
                    }
                    foreach (var neighbour in dict[currentCell.Value])
                    {
                        if (neighbour.cost + dist[currentCell.Value] < dist[neighbour.positionId])
                        {
                            int newTime = neighbour.cost + dist[currentCell.Value];
                            dist[neighbour.positionId] = newTime;
                            if (pq.Contains(neighbour.positionId))
                            {
                                pq.Update(neighbour.positionId, newTime);
                            }
                            else
                            {
                                pq.Insert(neighbour.positionId, newTime);
                            }
                        }
                    }
                }
                if ((dist[finalPosition]) <= T)
                {
                    Console.WriteLine("YES");
                    Console.WriteLine(T - (dist[finalPosition]));
                }
                else
                {
                    Console.WriteLine("NO");
                }
                t--;
            }
        }