今,c言語でNN法やった後に改善法で2-opt法を実行するプログラムを書いているんですけど、アルゴリズムは、理解できたですけどうまく書きあらわすことができないんで皆様の力を貸して欲しいです。その回答をみながら勉強させて欲しいです。
c言語,
1コード 2``#include <stdio.h> #include <stdlib.h> #include <string.h> #include <math.h> 3 4 5struct City { 6 int id; 7 float x; 8 float y; 9}; 10 11struct City city_data[1000000]; 12int city_data_num = 0; 13 14/* The program starts here */ 15int main(int argc, char** argv) 16{ 17 char line[512]; 18 FILE* fp; 19 20 /* check the number of arguments */ 21 if (argc != 2) { 22 printf("usage: ./a.out input_file_name\n"); 23 exit(1); 24 } 25 26 /* check whether the file can be opened */ 27 fp = fopen(argv[1], "r"); 28 if (fp == NULL) { 29 printf("The file (%s) cannot be opened!\n", argv[1]); 30 exit(1); 31 } 32 33 /* read city data and write the position of each city into the array "city_data" */ 34 while(fgets(line, sizeof(line), fp)) { 35 char* line_r; 36 line_r = strtok(line, " \t\n"); 37 city_data[city_data_num].id = (int)strtol(line_r, NULL, 10); 38 line_r = strtok(NULL, " \t\n"); 39 city_data[city_data_num].x = (float)strtod(line_r, NULL); 40 line_r = strtok(NULL, " \t\n"); 41 city_data[city_data_num].y = (float)strtod(line_r, NULL); 42 city_data_num++; 43 } 44 45 /* do traveling and calculate the distance */ 46 do_travel(); //nn法のソースコートは省いています。 47 48}