double Car::budget_cost() { /* Return sum(cost) / ODO * 100 */ double totalPrice = 0; foreach(Cost *cost, _costlist) { totalPrice += cost->cost(); } return totalPrice / ((maxdistance() - mindistance())/ 100.0); }
void dijkstra_ex(graph * gr,int start) { int min = 0; unsigned int * set = (unsigned int *)malloc(gr->num * sizeof(int)); unsigned int * dist = (unsigned int *)malloc(gr->num * sizeof(int)); memset(set,0,gr->num); memset(dist,INT_MAX,gr->num*sizeof(int)); // printDistance(dist,gr->num); dist[start] = 0; min = mindistance(set,dist,gr->num); dijkstra_algo(gr,min,set,dist); printDistance_ex(dist,gr->num,start); }
int mindistance(int last, int visited) { if (visited == (1 << p) - 1) { return dist(last, 0); } else { /* Try each not visited point and solve recursively */ int i, ans, minans = 100000; for (i = 1; i < p; ++i) { if ((visited & (1 << i)) == 0) { ans = mindistance(i, visited | (1 << i)); if (ans + dist(last, i) < minans) minans = ans + dist(last, i); } } return minans; } }
void dijkstra(graph * gr) { int min = 0; unsigned int * set = (unsigned int *)malloc(gr->num * sizeof(int)); unsigned int * dist = (unsigned int *)malloc(gr->num * sizeof(int)); memset(set,0,gr->num); memset(dist,INT_MAX,gr->num*sizeof(int)); printDistance(dist,gr->num); dist[0] = 0; min = mindistance(set,dist,gr->num); dijkstra_algo(gr,min,set,dist); updateDistance(set,dist,gr->num); printDistance(dist,gr->num); }
int main(void) { #ifdef TESTING freopen("input.txt","r",stdin); freopen("output.txt","w",stdout); #endif int t, m, n, b; int i; scanf("%d ", &t); while (t--) { scanf("%d %d %d %d %d ", &m, &n, &X[0], &Y[0], &b); for (i = 1; i <= b; ++i) scanf("%d %d ", &X[i], &Y[i]); p = b + 1; printf("The shortest path has length %d\n", mindistance(0, 1)); } return 0; }
void dijkstra_algo(graph * gr,int min,unsigned int * set,unsigned int *dist) { int dest = 0; int weight = 0; unsigned int min_val = INT_MAX; unsigned int min_dest = INT_MAX; unsigned int min_idx = 0; set[min] = 1; graphNode * gn = gr->myArrayList[min].head; while(gn) { dest = gn->dest; if(!set[dest]) { weight = gn->weight; if(dist[dest] >= (dist[min]+weight) ) { dist[dest] = dist[min] + weight; if(min_val > dist[dest]) { min_val = dist[dest]; min_dest = dest; } PRINT("dist[%d](%u) = dist[%d](%u) + %d \n",dest,dist[dest],min,dist[min],weight); } } gn = gn->next; } PRINT("PATH %d -->\n",min_dest); min_idx = mindistance(set,dist,gr->num); if((min_idx != gr->num) && (min != min_idx) ) { dijkstra_algo(gr,min_idx,set,dist); } }