└── post transition /post transition: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #define MAX_STRING_LENGTH 6 5 | 6 | struct package 7 | { 8 | char* id; 9 | int weight; 10 | }; 11 | 12 | typedef struct package package; 13 | 14 | struct post_office 15 | { 16 | int min_weight; 17 | int max_weight; 18 | package* packages; 19 | int packages_count; 20 | }; 21 | 22 | typedef struct post_office post_office; 23 | 24 | struct town 25 | { 26 | char* name; 27 | post_office* offices; 28 | int offices_count; 29 | }; 30 | 31 | typedef struct town town; 32 | 33 | 34 | 35 | void print_all_packages(town t) { 36 | printf("%s:\n",t.name); 37 | for(int i=0;ioffices+source_office_index; 47 | post_office *src_1=target->offices+target_office_index; 48 | package temple[src->packages_count]; 49 | int temple_size=0; 50 | for(int i=0;ipackages_count;){ 51 | if(src->packages[i].weight>=src_1->min_weight && 52 | src->packages[i].weight<=src_1->max_weight){ 53 | temple[temple_size++]=src->packages[i]; 54 | for(int j=i;jpackages_count-1;j++){ 55 | src->packages[j]=src->packages[j+1]; 56 | } 57 | src->packages_count--; 58 | } 59 | else i++; 60 | } 61 | src_1->packages=realloc(src_1->packages, 62 | (src_1->packages_count+temple_size)*sizeof(package)); 63 | for(int i=0;ipackages[src_1->packages_count++]=temple[i]; 65 | } 66 | } 67 | 68 | town town_with_most_packages(town* towns, int towns_count) { 69 | town s_max=*towns; int max=0; 70 | for(int i=0;ioffices_count;i++){ 71 | max+=towns->offices[i].packages_count; 72 | } 73 | towns++;towns_count--; 74 | while(towns_count){ 75 | int sum=0; 76 | for(int i=0;ioffices_count;i++){ 77 | sum+=towns->offices[i].packages_count; 78 | } 79 | if(sum>max){ 80 | max=sum; 81 | s_max=*towns; 82 | } 83 | towns++;towns_count--; 84 | } 85 | return s_max; 86 | } 87 | 88 | town* find_town(town* towns, int towns_count, char* name) { 89 | while(towns_count){ 90 | if(!strcmp(towns->name,name)){ 91 | return towns; 92 | } 93 | towns++; towns_count--; 94 | } 95 | return NULL; 96 | } 97 | 98 | int main() 99 | { 100 | int towns_count; 101 | scanf("%d", &towns_count); 102 | town* towns = malloc(sizeof(town)*towns_count); 103 | for (int i = 0; i < towns_count; i++) { 104 | towns[i].name = malloc(sizeof(char) * MAX_STRING_LENGTH); 105 | scanf("%s", towns[i].name); 106 | scanf("%d", &towns[i].offices_count); 107 | towns[i].offices = malloc(sizeof(post_office)*towns[i].offices_count); 108 | for (int j = 0; j < towns[i].offices_count; j++) { 109 | scanf("%d%d%d", &towns[i].offices[j].packages_count, &towns[i].offices[j].min_weight, &towns[i].offices[j].max_weight); 110 | towns[i].offices[j].packages = malloc(sizeof(package)*towns[i].offices[j].packages_count); 111 | for (int k = 0; k < towns[i].offices[j].packages_count; k++) { 112 | towns[i].offices[j].packages[k].id = malloc(sizeof(char) * MAX_STRING_LENGTH); 113 | scanf("%s", towns[i].offices[j].packages[k].id); 114 | scanf("%d", &towns[i].offices[j].packages[k].weight); 115 | } 116 | } 117 | } 118 | int queries; 119 | scanf("%d", &queries); 120 | char town_name[MAX_STRING_LENGTH]; 121 | while (queries--) { 122 | int type; 123 | scanf("%d", &type); 124 | switch (type) { 125 | case 1: 126 | scanf("%s", town_name); 127 | town* t = find_town(towns, towns_count, town_name); 128 | print_all_packages(*t); 129 | break; 130 | case 2: 131 | scanf("%s", town_name); 132 | town* source = find_town(towns, towns_count, town_name); 133 | int source_index; 134 | scanf("%d", &source_index); 135 | scanf("%s", town_name); 136 | town* target = find_town(towns, towns_count, town_name); 137 | int target_index; 138 | scanf("%d", &target_index); 139 | send_all_acceptable_packages(source, source_index, target, target_index); 140 | break; 141 | case 3: 142 | printf("Town with the most number of packages is %s\n", town_with_most_packages(towns, towns_count).name); 143 | break; 144 | } 145 | } 146 | return 0; 147 | } 148 | --------------------------------------------------------------------------------