/* ============================================================================ Name : kug.c Author : Paweł Zawada ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include <assert.h> struct s_edge { int to_vertex; int edge_cost; struct s_edge *next_list_node; }; struct s_edge * *graph; int *was, n, *previous_on_path, *path_cost, path_len; int add_edge(int from, int to, int cost) { //fprintf(stderr,"Adding edge from %i<-> to %i cost %i\n", from, to, cost); struct s_edge *new_edge = (struct s_edge*) malloc(sizeof(struct s_edge)); new_edge->to_vertex = to; new_edge->edge_cost = cost; new_edge->next_list_node = graph[from]; graph[from] = new_edge; new_edge = (struct s_edge*) malloc(sizeof(struct s_edge)); new_edge->to_vertex = from; new_edge->edge_cost = cost; new_edge->next_list_node = graph[to]; graph[to] = new_edge; return 0; } unsigned long long int edge_cost_all() { struct s_edge *next_ln; unsigned long long int cost = 0; int i; for (i = 0; i <= n; i++) { for (next_ln = graph[i]; next_ln != 0; next_ln = next_ln->next_list_node) { cost += next_ln->edge_cost; } } return cost/2; } int edge_cost(int from, int to) { struct s_edge *next_ln = graph[from]; //fprintf(stderr, "Edge cost %i %i\n", from, to); assert(from>=0 && to>=0 && from<=n && to <=n); assert(graph[from]); for (next_ln = graph[from]; next_ln != 0; next_ln = next_ln->next_list_node) { if (next_ln->to_vertex == to) return (next_ln->edge_cost); } assert(0); return -1; } void remove_edge(int from, int to) { assert(from>=0 && to>=0 && from<=n && to <=n); struct s_edge **next_ln, *tmp; if (graph[from] == 0) return; for (next_ln = &(graph[from]); (*next_ln) != 0; next_ln = &((*next_ln)->next_list_node)) { if ((*next_ln)->to_vertex == to) { tmp = *next_ln; *next_ln = (*next_ln)->next_list_node; free(tmp); return; } } } void remove_edges(int from, int to) { //fprintf(stderr, "removing edges from %i to %i\n", from, to); assert(from>=0 && to>=0 && from<=n && to <=n); remove_edge(from, to); remove_edge(to, from); //fprintf(stderr,"removed edges\n"); } void delete_max_from_cycle(int last_on_path, int first_on_cycle) { int i, max_cost = -1, max_arg = -1, p_cost; assert(last_on_path!=first_on_cycle); //fprintf(stderr,"found cycle\n"); max_cost = edge_cost(last_on_path, first_on_cycle); max_arg = first_on_cycle; for (i = last_on_path; previous_on_path[i] != -1; i = previous_on_path[i]) { p_cost = edge_cost(previous_on_path[i], i); if (max_cost < p_cost) { max_arg = i; max_cost = p_cost; } //fprintf(stderr,"%i ", i); } //fprintf(stderr,"->>>>>>>>>>>>>>>>>>> %i ", first_on_cycle); //fprintf(stderr,"Max cost: %i \n", max_cost); if (max_arg == first_on_cycle) remove_edges(last_on_path, first_on_cycle); else remove_edges(previous_on_path[max_arg], max_arg); } int bfs(int from, int edge) { int result, s; struct s_edge *next_ln; //fprintf(stderr,"recursion bfs %i\n", edge); if (graph[edge] == 0) return -1; was[edge] = 1; //for (s = 0; s <= n; s++) { // fprintf(stderr,"Was[%i]=%i ", s, was[s]); //} //fprintf(stderr,"\n"); for (next_ln = graph[edge]; next_ln != 0; next_ln = next_ln->next_list_node) { if (next_ln->to_vertex == from) continue; if (was[next_ln->to_vertex]) { delete_max_from_cycle(edge, next_ln->to_vertex); return 0; } else { previous_on_path[next_ln->to_vertex] = edge; path_len++; result = bfs(edge, next_ln->to_vertex); if (result == 0) return 0; } } was[edge] = 0; return -1; } void remove_cycle() { int i; //fprintf(stderr,"remove cycle\n"); for (i = 0; i <= n; i++) was[i] = 0; path_len = 0; previous_on_path[0] = -1; bfs(-1, 0); } int main(void) { int i, cost, w, k; FILE *inp; //fprintf(stderr,"Starting\n"); /*inp = fopen("kug.in", "r"); if (inp == 0) { fprintf(stderr,"error NULL file\n"); return -1; }*/ fscanf(stdin, "%i", &n); //fprintf(stderr,"%i\n", n); graph = (struct s_edge **) malloc((n + 1) * sizeof(struct s_edge*)); was = (int *) (malloc((n + 1) * sizeof(int))); previous_on_path = (int *) (malloc((n + 1) * sizeof(int))); for (i = 0; i <= n; i++) { graph[i] = 0; } for (w = 1; w <= n; w++) { for (k = 1; k <= n - w + 1; k++) { fscanf(stdin, "%i", &cost); //fprintf(stderr,"%i ", cost); add_edge(w - 1, k + w - 1, cost); remove_cycle(); } //fprintf(stderr,"<\n"); } unsigned long long int tot = edge_cost_all(); printf("%lli\n",tot); //printf("%lli ", (unsigned long long int)4000000*1000000000); return EXIT_SUCCESS; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 | /* ============================================================================ Name : kug.c Author : Paweł Zawada ============================================================================ */ #include <stdio.h> #include <stdlib.h> #include <assert.h> struct s_edge { int to_vertex; int edge_cost; struct s_edge *next_list_node; }; struct s_edge * *graph; int *was, n, *previous_on_path, *path_cost, path_len; int add_edge(int from, int to, int cost) { //fprintf(stderr,"Adding edge from %i<-> to %i cost %i\n", from, to, cost); struct s_edge *new_edge = (struct s_edge*) malloc(sizeof(struct s_edge)); new_edge->to_vertex = to; new_edge->edge_cost = cost; new_edge->next_list_node = graph[from]; graph[from] = new_edge; new_edge = (struct s_edge*) malloc(sizeof(struct s_edge)); new_edge->to_vertex = from; new_edge->edge_cost = cost; new_edge->next_list_node = graph[to]; graph[to] = new_edge; return 0; } unsigned long long int edge_cost_all() { struct s_edge *next_ln; unsigned long long int cost = 0; int i; for (i = 0; i <= n; i++) { for (next_ln = graph[i]; next_ln != 0; next_ln = next_ln->next_list_node) { cost += next_ln->edge_cost; } } return cost/2; } int edge_cost(int from, int to) { struct s_edge *next_ln = graph[from]; //fprintf(stderr, "Edge cost %i %i\n", from, to); assert(from>=0 && to>=0 && from<=n && to <=n); assert(graph[from]); for (next_ln = graph[from]; next_ln != 0; next_ln = next_ln->next_list_node) { if (next_ln->to_vertex == to) return (next_ln->edge_cost); } assert(0); return -1; } void remove_edge(int from, int to) { assert(from>=0 && to>=0 && from<=n && to <=n); struct s_edge **next_ln, *tmp; if (graph[from] == 0) return; for (next_ln = &(graph[from]); (*next_ln) != 0; next_ln = &((*next_ln)->next_list_node)) { if ((*next_ln)->to_vertex == to) { tmp = *next_ln; *next_ln = (*next_ln)->next_list_node; free(tmp); return; } } } void remove_edges(int from, int to) { //fprintf(stderr, "removing edges from %i to %i\n", from, to); assert(from>=0 && to>=0 && from<=n && to <=n); remove_edge(from, to); remove_edge(to, from); //fprintf(stderr,"removed edges\n"); } void delete_max_from_cycle(int last_on_path, int first_on_cycle) { int i, max_cost = -1, max_arg = -1, p_cost; assert(last_on_path!=first_on_cycle); //fprintf(stderr,"found cycle\n"); max_cost = edge_cost(last_on_path, first_on_cycle); max_arg = first_on_cycle; for (i = last_on_path; previous_on_path[i] != -1; i = previous_on_path[i]) { p_cost = edge_cost(previous_on_path[i], i); if (max_cost < p_cost) { max_arg = i; max_cost = p_cost; } //fprintf(stderr,"%i ", i); } //fprintf(stderr,"->>>>>>>>>>>>>>>>>>> %i ", first_on_cycle); //fprintf(stderr,"Max cost: %i \n", max_cost); if (max_arg == first_on_cycle) remove_edges(last_on_path, first_on_cycle); else remove_edges(previous_on_path[max_arg], max_arg); } int bfs(int from, int edge) { int result, s; struct s_edge *next_ln; //fprintf(stderr,"recursion bfs %i\n", edge); if (graph[edge] == 0) return -1; was[edge] = 1; //for (s = 0; s <= n; s++) { // fprintf(stderr,"Was[%i]=%i ", s, was[s]); //} //fprintf(stderr,"\n"); for (next_ln = graph[edge]; next_ln != 0; next_ln = next_ln->next_list_node) { if (next_ln->to_vertex == from) continue; if (was[next_ln->to_vertex]) { delete_max_from_cycle(edge, next_ln->to_vertex); return 0; } else { previous_on_path[next_ln->to_vertex] = edge; path_len++; result = bfs(edge, next_ln->to_vertex); if (result == 0) return 0; } } was[edge] = 0; return -1; } void remove_cycle() { int i; //fprintf(stderr,"remove cycle\n"); for (i = 0; i <= n; i++) was[i] = 0; path_len = 0; previous_on_path[0] = -1; bfs(-1, 0); } int main(void) { int i, cost, w, k; FILE *inp; //fprintf(stderr,"Starting\n"); /*inp = fopen("kug.in", "r"); if (inp == 0) { fprintf(stderr,"error NULL file\n"); return -1; }*/ fscanf(stdin, "%i", &n); //fprintf(stderr,"%i\n", n); graph = (struct s_edge **) malloc((n + 1) * sizeof(struct s_edge*)); was = (int *) (malloc((n + 1) * sizeof(int))); previous_on_path = (int *) (malloc((n + 1) * sizeof(int))); for (i = 0; i <= n; i++) { graph[i] = 0; } for (w = 1; w <= n; w++) { for (k = 1; k <= n - w + 1; k++) { fscanf(stdin, "%i", &cost); //fprintf(stderr,"%i ", cost); add_edge(w - 1, k + w - 1, cost); remove_cycle(); } //fprintf(stderr,"<\n"); } unsigned long long int tot = edge_cost_all(); printf("%lli\n",tot); //printf("%lli ", (unsigned long long int)4000000*1000000000); return EXIT_SUCCESS; } |