private void button2_Click(object sender, EventArgs e) { List <Node> nodeselected = new List <Node>(); nodeselected.Add(CurrentGraph.GetNodeById(0)); //foreach (ListViewItem item in listView1.SelectedItems) { // var node = (Node)item.Tag; // nodeselected.Add(CurrentGraph.GetNodeById(node.GetId())); //} foreach (ListViewItem item in listView1.SelectedItems) { var node = (Node)item.Tag; foreach (var item2 in CurrentGraph.Nodes) { if (item2.Value.Name == node.Name) { nodeselected.Add(CurrentGraph.GetNodeById(item2.Value.GetId())); } } } Queue <Node> nodeFila = new Queue <Node>(); nodeFila.Enqueue(CurrentGraph.GetNodeById(0)); Node nodePai = null; Node inicio = null; Node fim = null; List <int> visited = new List <int>(); List <string> visitedProf = new List <string>(); double distancia = double.MaxValue; double distanciaAux = 0; double distanciaTotal = 0; var flag = 0; while (nodeFila.Count > 0 && visited.Count <= nodeselected.Count) { nodePai = nodeFila.Dequeue(); foreach (var item in nodeselected) { if (!visitedProf.Contains(item.profissao) && nodePai.profissao != item.profissao && nodePai != null && nodePai.Name != item.Name && !visited.Contains(nodePai.GetId()) && !visited.Contains(item.GetId()) && CurrentGraph.GetNodeById(nodePai.GetId()).AdjacencyList[CurrentGraph.GetNodeById(item.GetId())].Cost <= distancia) { distancia = CurrentGraph.GetNodeById(nodePai.GetId()).AdjacencyList[CurrentGraph.GetNodeById(item.GetId())].Cost; if (double.Parse(textBox1.Text) >= distancia) { distanciaAux += distancia; if (distanciaAux <= double.Parse(textBox1.Text) && !visitedProf.Contains(item.profissao)) { inicio = CurrentGraph.Nodes[CurrentGraph.Nodes.Keys.ToArray()[nodePai.GetId()]]; fim = CurrentGraph.Nodes[CurrentGraph.Nodes.Keys.ToArray()[item.GetId()]]; Steps = CurrentGraph.Dijkstra(inicio, fim); } } } } nodeFila.Enqueue(fim); if (inicio != null) { if (!visitedProf.Contains(fim.profissao)) { distanciaTotal += inicio.AdjacencyList[fim].Cost; } flag = 0; visited.Add(inicio.GetId()); visitedProf.Add(fim.profissao); distancia = double.MaxValue; CurrentStep = Steps.Count - 1; CurrentGraph = Steps[CurrentStep]; RefreshGraphDraw(); } else { MessageBox.Show("Não existe nenhum serviço nas proximidades"); return; } } if (inicio != null) { inicio = CurrentGraph.Nodes[CurrentGraph.Nodes.Keys.ToArray()[nodePai.GetId()]]; fim = CurrentGraph.Nodes[CurrentGraph.Nodes.Keys.ToArray()[0]]; Steps = CurrentGraph.Dijkstra(inicio, fim); CurrentStep = Steps.Count - 1; CurrentGraph = Steps[CurrentStep]; RefreshGraphDraw(); } CurrentGraph = BaseGraph.Copy(); distanciaTotal += inicio.AdjacencyList[fim].Cost; MessageBox.Show("Total Percorrido " + distanciaTotal.ToString("F") + " KM"); }