void check() { int i,j,k; double min; memset(flag, 0, sizeof(flag)); memset(D, 0, sizeof(D)); flag[0] = 1; D[0] = 0; for (i = 1; i < n; i++) { min = -1; for (j = 0; j < i; j++) { for (k = 0; k < n; k++) { if (flag[k]==0) { if (min < 0) { min = distant(D[j],k); D[i] = k; } else if (min > distant(D[j],k)) { D[i] = k; min = distant(D[j],k); } } } } sum += min; flag[D[i]] = 1; } }
/********************************************************************* void double bipolar_dist(double x1, double y1, double x2, double y2) return distance of bipolar grids *********************************************************************/ double bipolar_dist(double x1, double y1, double x2, double y2, double bpeq, double bpsp, double bpnp, double rp ) { double dist, x[2],y[2], bp_lon[2], bp_lat[2], metric[2]; double h1[2], h2[2], chic; int n; x[0] = x1; x[1] = x2; y[0] = y1; y[1] = y2; /*--- get the bipolar grid and metric term ----------------------------*/ for(n=0; n<2; n++){ bp_lon[n] = bp_lam(x[n],y[n],bpeq, rp); /* longitude (degrees) in bipolar grid system */ bp_lat[n] = bp_phi(x[n],y[n],bpsp, bpnp); /* latitude (degrees) in bipolar grid system */ h1[n] = RADIUS*cos(bp_lat[n]*D2R); h2[n] = RADIUS; metric[n] = 1.0; if (fabs(y[n]-90.0) < SMALL || fabs(bp_lon[n]*D2R) >= SMALL || fabs(bp_lat[n]*D2R) >= SMALL) { chic = acos(cos(bp_lon[n]*D2R)*cos(bp_lat[n]*D2R)); /* eqn. 6 */ metric[n] = rp*(1/pow(cos(chic/2),2))/(1+(pow(rp,2))*(pow(tan(chic/2),2)));/* eq 3 */ } } /*--- then calculate the distance -------------------------------------*/ if(x1 == x2) dist = distant(bp_lon[0],bp_lon[1],metric[0]*h1[0],metric[1]*h1[1]); else if(y1 == y2) dist = distant(bp_lat[0],bp_lat[1],metric[0]*h2[0],metric[1]*h2[1]); else mpp_error("tool_util: This tripolar grid not transformed from rectangular grid"); return dist; }; /* bipolar_dist */
void sp(int s) { int p[MAXN]; int i, j, k, m; /* get all nodes belong to this subgraph */ for (i = 0, m = 0; i < n; i++) if (sub[i] == s) p[m++] = i; /* initial */ for (i = 0; i < m; i++) for (j = 0; j < m; j++) { if (adj[p[i]][p[j]]) dis[p[i]][p[j]] = distant(x[p[i]], y[p[i]], x[p[j]], y[p[j]]); else dis[p[i]][p[j]] = INFIN; } /* shortest path */ for (k = 0; k < m; k++) { for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { if (dis[p[i]][p[j]] > dis[p[i]][p[k]] + dis[p[k]][p[j]]) dis[p[i]][p[j]] = dis[p[i]][p[k]] + dis[p[k]][p[j]]; } } } }
double spherical_dist(double x1, double y1, double x2, double y2) { double dist = 0.0; double h1, h2; if(x1 == x2) { h1 = RADIUS; h2 = RADIUS; dist = distant(y1,y2,h1,h2); } else if(y1 == y2) { h1 = RADIUS * cos(y1*D2R); h2 = RADIUS * cos(y2*D2R); dist = distant(x1,x2,h1,h2); } else mpp_error("tool_till: This is not rectangular grid"); return dist; }; /* spherical_dist */
int main(int argc, char **argv) { /* code add by andrew GONG for reading file from command line argument */ point p[N]; point q[N]; point array[V]; unsigned int G[V][V]; point s = {0, 0}; point t = {0, 0}; int i, j, k; int press; i = j = k = 0; press = 0; for (i = 0; i < N; i++) { p[i].x = p[i].y = 0; q[i].x = q[i].y = 0; } if(argc != 2) { printf("usage:%s filename", argv[0]); } else { FILE *file = fopen(argv[1], "r"); if(file == 0) { printf("Could not open file!\n"); } else { i = 0; printf("%s\n", argv[1]); while(!feof(file)) { if(fscanf(file, "S (%d, %d) (%d, %d)\n", &p[i].x, &p[i].y, &q[i].x, &q[i].y) != 4) { break; } else { i++; } } } fclose(file); } /* opening display: basic connection to X Server */ if( (display_ptr = XOpenDisplay(display_name)) == NULL ) { printf("Could not open display. \n"); exit(-1); } printf("Connected to X server %s\n", XDisplayName(display_name)); screen_num = DefaultScreen(display_ptr); screen_ptr = DefaultScreenOfDisplay(display_ptr); color_map = XDefaultColormap(display_ptr, screen_num); display_width = DisplayWidth(display_ptr, screen_num); display_height = DisplayHeight(display_ptr, screen_num); printf("Width %d, Height %d, Screen Number %d\n", display_width, display_height, screen_num); /* creating the window */ border_width = 10; win_x = 0; win_y = 0; win_width = display_width/2; win_height = (unsigned int) (win_width / 1.7); /* rectangular window */ win= XCreateSimpleWindow(display_ptr, RootWindow(display_ptr, screen_num), win_x, win_y, win_width, win_height, border_width, BlackPixel(display_ptr, screen_num), WhitePixel(display_ptr, screen_num)); /* now try to put it on screen, this needs cooperation of window manager */ size_hints = XAllocSizeHints(); wm_hints = XAllocWMHints(); class_hints = XAllocClassHint(); if( size_hints == NULL || wm_hints == NULL || class_hints == NULL ) { printf("Error allocating memory for hints. \n"); exit(-1); } size_hints -> flags = PPosition | PSize | PMinSize ; size_hints -> min_width = 60; size_hints -> min_height = 60; XStringListToTextProperty(&win_name_string, 1, &win_name); XStringListToTextProperty(&icon_name_string, 1, &icon_name); wm_hints -> flags = StateHint | InputHint ; wm_hints -> initial_state = NormalState; wm_hints -> input = False; class_hints -> res_name = "x_use_example"; class_hints -> res_class = "examples"; XSetWMProperties(display_ptr, win, &win_name, &icon_name, argv, argc, size_hints, wm_hints, class_hints); /* what events do we want to receive */ XSelectInput(display_ptr, win, ExposureMask | StructureNotifyMask | ButtonPressMask); /* finally: put window on screen */ XMapWindow(display_ptr, win); XFlush(display_ptr); /* create graphics context, so that we may draw in this window */ gc = XCreateGC(display_ptr, win, valuemask, &gc_values); XSetForeground(display_ptr, gc, BlackPixel(display_ptr, screen_num)); XSetLineAttributes(display_ptr, gc, 2, LineSolid, CapRound, JoinRound); /* and three other graphics contexts, to draw in yellow and red and grey */ gc_yellow = XCreateGC( display_ptr, win, valuemask, &gc_yellow_values); XSetLineAttributes(display_ptr, gc_yellow, 2, LineSolid, CapRound, JoinRound); if(XAllocNamedColor(display_ptr, color_map, "yellow", &tmp_color1, &tmp_color2 ) == 0) {printf("failed to get color yellow\n"); exit(-1); } else XSetForeground(display_ptr, gc_yellow, tmp_color1.pixel); gc_red = XCreateGC(display_ptr, win, valuemask, &gc_red_values); /* XSetLineAttributes(display_ptr, gc_red, 6, LineSolid, CapRound, JoinRound); */ if( XAllocNamedColor(display_ptr, color_map, "red", &tmp_color1, &tmp_color2) == 0 ) {printf("failed to get color red\n"); exit(-1);} else XSetForeground(display_ptr, gc_red, tmp_color1.pixel); gc_grey = XCreateGC(display_ptr, win, valuemask, &gc_grey_values); if( XAllocNamedColor(display_ptr, color_map, "light grey", &tmp_color1, &tmp_color2) == 0) {printf("failed to get color grey\n"); exit(-1);} else XSetForeground(display_ptr, gc_grey, tmp_color1.pixel); /* and now it starts: the event loop */ while(1) { XNextEvent( display_ptr, &report ); switch(report.type) { case Expose: /* (re-)draw the example figure. This event happens * each time some part ofthe window gets exposed (becomes visible) */ /* print all the obstacles */ for (i = 0; i < N; i++) { if (p[i].x != 0 && p[i].y != 0 && q[i].x != 0 && q[i].y !=0) { XDrawLine(display_ptr, win, gc, p[i].x, p[i].y, q[i].x, q[i].y); } } break; case ConfigureNotify: /* This event happens when the user changes the size of the window */ win_width = report.xconfigure.width; win_height = report.xconfigure.height; break; /* This event happens when the user pushes a mouse button. I draw * a circle to show the point where it happened, but do not save * the position; so when the next redraw event comes, these circles * disappear again. */ case ButtonPress: { int x, y; x = report.xbutton.x; y = report.xbutton.y; /* read point s, t */ if (press == 0) { s.x = report.xbutton.x; s.y = report.xbutton.y; } else if (press == 1) { t.x = report.xbutton.x; t.y = report.xbutton.y; } press += 1; if (report.xbutton.button == Button1) XFillArc(display_ptr, win, gc_red, x - win_height/40, y - win_height/40, win_height / 20, win_height / 20, 0, 360*64); else XFillArc(display_ptr, win, gc_yellow, x - win_height / 40, y - win_height / 40, win_height / 20, win_height / 20, 0, 360*64); printf("%d, %d, %d, %d\n", s.x, s.y, t.x, t.y); printf("press = %d\n", press); if (press == 2) { XFlush(display_ptr); /* construction graph G *s is the first point and t is the last point */ array[0].x = s.x; array[0].y = s.y; array[V-1].x = t.x; array[V-1].y = t.y; for (i = 1; i < V-1; i++) { if ( i <= N ) { array[i].x = p[i-1].x; array[i].y = p[i-1].y; } else if (i > N) { array[i].x = q[i-N-1].x; array[i].y = q[i-N-1].y; } } for (i = 0; i < V; i++) { int j; for(j = 0; j < V; j++) { if (( array[i].x == 0 && array[i].y == 0 ) || ( array[j].x == 0 && array[j].y == 0)) { G[i][j] = INT_MAX; } else { G[i][j] = distant(array[i],array[j]); } for (k = 0; k < N; k++) { if (p[k].x != 0 && p[k].y != 0 && q[k].x != 0 && q[k].y != 0 && array[i].x !=0 && array[i].y !=0 && intersect( array[i], array[j], p[k], q[k])) { G[i][j] = INT_MAX; } } } } dijkstra(G, 0, array); } } break; default: /* this is a catch-all for other events; it does not do anything. *One could look at the report type to see what the event was */ break; } } exit(0); }
main () { FILE *fin = fopen ("cowtour.in", "r"); fout = fopen ("cowtour.out", "w"); int i, j, r, s, d; fscanf (fin, "%d", &n); for (i = 0; i < n; i++) fscanf (fin, "%d %d", &x[i], &y[i]); for (i = 0; i < n; i++) for (j = 0; j < n; j++) fscanf (fin, "%1d", &adj[i][j]); for (i = 0; i < n; i++) adj[i][i] = 1; /* find subgraph */ memset(sub, 0, sizeof(sub)); int subnum = 0; for (i = 0; i < n; i++) { if (!sub[i]) set_sub(i, ++subnum); } /* shortest path */ for (s = 1; s <= subnum; s++) sp(s); double dia, min_dia = INFIN; /* connect subgraph 1 with 2, 3, ... , subnum */ for (r = 1; r < subnum; r++) { for (s = r+1; s <= subnum; s++) { int a, b; double d, len = INFIN; for (i = 0; i < n; i++) { if (sub[i] == r) { for (j = 0; j < n; j++) { if (sub[j] == s) { d = distant(x[i], y[i], x[j], y[j]); if (len > max[i] + max[j] + d) { a = i; b = j; len = max[i] + max[j] + d; } } } } } adj[a][b] = adj[b][a] = 1; dis[a][b] = dis[b][a] = distant(x[a], y[a], x[b], y[b]); for (i = 0; i < n; i++) { if (sub[i] == r) { for (j = 0; j < n; j++) { if (sub[j] == s) dis[i][j] = dis[j][i] = dis[i][a] + dis[a][b] + dis[b][j]; } } } dia = 0; for (i = 0; i < n; i++) { if (sub[i] == r || sub[i] == s) { for (j = 0; j < n; j++) { if (sub[j] == r || sub[j] == s) { if (dia < dis[i][j]) dia = dis[i][j]; } } } } if (min_dia > dia) min_dia = dia; } } fprintf(fout, "%.6f\n", min_dia); exit (0); }