Dijkstra Algorithm - How do I change the reading of the txt file in C to be identified in int and not string?

1

Good evening, I have a question about how to change the reading of the txt file, where the compiler identifies with char however within the file that I need to read, have only numbers, could anyone help me?

Attached is the error you are reporting.

usingnamespacestd;classGrafo{private:intV;//numerodevertices//ponteiroparaumarraycontendoaslistasdeadjacenciaslist<pair<int,int>>*adj;public://construtorGrafo(intV){this->V=V;//atribuionumerodevertices/*criaaslistasondecadalistaéumalistadepairsondecadapairéformadopelovérticedestinoeocusto*/adj=newlist<pair<int,int>>[V];}//adicionaumaarestaaografodev1a v2voidaddAresta(intv1,intv2,intcusto){adj[v1].push_back(make_pair(v2,custo));}//algoritmodeDijkstraintdijkstra(intorig,intdest){//vetordedistanciasintdist[V];/*vetordevisitadosserveparacasooverticeja¡tenhasidoexpandido(visitado),naoexpandirmais*/intvisitados[V];//filadeprioridadesdepair(distancia,vertice)priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>>pq;//iniciaovetordedistanciasevisitadosfor(inti=0;i<V;i++){dist[i]=INFINITO;visitados[i]=false;}//adistanciadeorigparaorigem0dist[orig]=0;//inserenafilapq.push(make_pair(dist[orig],orig));//loopdoalgoritmowhile(!pq.empty()){pair<int,int>p=pq.top();//extraiopairdotopointu=p.second;//obtemoverticedopairpq.pop();//removedafila//verificaseoverticenaofoiexpandidoif(visitados[u]==false){//marcacomovisitadovisitados[u]=true;list<pair<int,int>>::iteratorit;//percorreosvertices"v" adjacentes de "u"
                for(it = adj[u].begin(); it != adj[u].end(); it++)
                {
                    // obtem o vertice adjacente e o custo da aresta
                    int v = it->first;
                    int custo_aresta = it->second;

                    // relaxamento (u, v)
                    if(dist[v] > (dist[u] + custo_aresta))
                    {
                        // atualiza a distancia de "v" e insere na fila
                        dist[v] = dist[u] + custo_aresta;
                        pq.push(make_pair(dist[v], v));
                    }
                }
            }
        }

        // retorna a distancia minima ate o destino
        return dist[dest];
    }
};

int main(int argc, char *argv[])
{
    int indicei, indicej, mat[5][5];

    for (indicei=0; indicei<5;indicei++){
        for(indicej=0 ; indicej<5; ++indicej){

            FILE *file;

            file = fopen("C:\Users\VINICIUSBENTOMARQUES\Desktop\Trabalho IA\Trabalho IA\cidades.txt", "r");

            if(file == NULL){
                printf("Não foi possível abrir o arquivo.\n");
                getchar();
                exit(0);
             }

            char numeros[1000];

            while(fgets(numeros, 1000, file) != NULL){
                printf("%s", numeros);
             }

            fclose(file);

            Grafo g(5);

        g.addAresta(indicei, indicej, mat[indicei,indicej]);


            cout << fprintf(file,"%i", g.dijkstra(mat[indicei,indicej])) <<endl;

        }
    }
    return 0;
}
    
asked by anonymous 10.09.2018 / 17:12

1 answer

0

When you are calling the g.dijkstra() function it needs two integer parameters, one for source and one for destination g.dijkstra(origem, destino) . And in your case you are only sending a value that is in the array in the index positions.

    
10.09.2018 / 21:23