Código:
el archivo nodos contiene numeros del 1-9 y el archivo arcos contiene 60 series de numeros separados por 3 (origen,destino,peso) algo com esto#include <cstdlib> #include <iostream> #include <conio.h> #include <stdio.h> #include <cmath> #include <cstring> using namespace std; int destino[20], origen[20], vertices[10]; int costo[20]; typedef struct arista { int peso,nombre; struct arista *Sig; }arista, *parista; typedef struct vertice { int nombre; struct vertice *Sig; struct arista *Ady; }vertice, *pvertice; pvertice PrimerNodo = NULL; pvertice UltimoNodo = NULL; parista UltimaArista = NULL; void Agregar(int Nombre,int Peso) { vertice *NuevoNodo = new vertice; NuevoNodo->nombre = Nombre; if(PrimerNodo == NULL) { PrimerNodo = NuevoNodo; UltimoNodo = NuevoNodo; NuevoNodo->Sig = NULL; } else { UltimoNodo->Sig = NuevoNodo; UltimoNodo = NuevoNodo; NuevoNodo->Sig = NULL; } //Para agragar aristas arista *NuevaArista = new arista; NuevaArista->peso = Peso; if(NuevoNodo->Ady == NULL) { NuevoNodo->Ady = NuevaArista; UltimaArista = NuevaArista; NuevaArista->Sig = NULL; } else { UltimaArista->Sig = NuevaArista; UltimaArista = NuevaArista; } } int main(int argc, char **argv) { printf("Implementacion del Algoritmo de Dijkstra\n"); FILE *fptr1,*fptr2; char sTexto[5]; int iArreglo[60]; fptr1 = fopen ( "arcos.txt", "r"); fptr2 = fopen ( "nodos.txt", "r"); if (fptr1 != NULL || fptr2 != NULL) //comprueba si los archivos se abrieron { printf ("\nArchivo Nodos leido\n"); printf ("Los vertices son: \n"); for(int i = 0; i < 9; i++) { fscanf(fptr2, "%s", sTexto); vertices[i] = atoi(sTexto); //AgregarVertice(vertices[i]); printf ("%d\n",vertices[i]); } fclose(fptr2); printf ("Archivo Arcos leido\n"); printf ("El arbol se compone con: (origenes) (destinos) (costos)\n"); for(int j = 0; j < 60; j = j+3) { fscanf(fptr1, "%d%d%d", &origen[j], &destino[j+1], &costo[j+2]); printf ("%d %d %d\n",origen[j],destino[j+1],costo[j+2]); Agregar(origen[j],costo[j+2]); Agregar(destino[j+1],costo[j+2]); } fclose(fptr1); } else { printf ("Error: Algun archivo no se abrio.\n"); } system("PAUSE"); return EXIT_SUCCESS; }
1 origen
2 destino
40 peso
1
4
55
1
5
40
2
3
25