10099-the Tourist Guide
Topic page:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&itemid=8&page=show_problem& problem=1040
Mr.g. I am also a person ... Reflected in the code is 127 lines
?
| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131132133134135136137138139140141142 |
#include <iostream>#include <stdlib.h>#include <queue>int number_ways;intnumber_points;struct{ boolused; intparent; intn; int list[101];}buffer_map[110];structWays{ intx, y;} buffer_ways[110*110];intbuffer_values[101][101];intstart, end, number_customer;// 降序排列intcompare_decrease(constvoid*a, constvoid*b){ intbx = ((Ways*)b)->x; intby = ((Ways*)b)->y; intax = ((Ways*)a)->x; intay = ((Ways*)a)->y; returnbuffer_values[bx][by] - buffer_values[ax][ay];}voidbfs(){ // 1. build tree // a. init G for(inti = 0; i < number_points; i++) { buffer_map[i].n = 0; buffer_map[i].used = false; } // b. add ways (double dirction) for(inti = 0; i < number_points - 1; i++) { intx = buffer_ways[i].x; inty = buffer_ways[i].y; buffer_map[x].list[buffer_map[x].n] = y; buffer_map[x].n++; y = buffer_ways[i].x; x = buffer_ways[i].y; buffer_map[x].list[buffer_map[x].n] = y; buffer_map[x].n++; } // search std::queue<int> q; q.push(start); while(!q.empty()) { intu = q.front(); q.pop(); if(u == end) return; intlength = buffer_map[u].n; for(int i = 0; i < length; i++) { intv = buffer_map[u].list[i]; if(buffer_map[v].used == false) { buffer_map[v].parent = u; buffer_map[v].used = true; q.push(v); } } }}intmain(){ intcount = 1; scanf("%d%d", &number_points, &number_ways); while(number_points != 0 && number_ways != 0) { // 1. read ways for(inti = 0; i < number_ways; i++) { intx, y, values; scanf("%d%d%d", &x, &y, &values); buffer_ways[i].x = x; buffer_ways[i].y = y; buffer_values[x][y] = values; buffer_values[y][x] = values; } // 2. read start and target and customer scanf("%d%d%d", &start, &end, &number_customer); // 3. sort qsort(buffer_ways, number_ways, sizeof(Ways), compare_decrease); // 4. bfs bfs(); // 5. find ways intv = end; intmin_values = buffer_values[v][buffer_map[v].parent]; while(v != start) { intnext_values = buffer_values[v][buffer_map[v].parent]; if(next_values < min_values) min_values = next_values; v = buffer_map[v].parent; } min_values--; intremainder = number_customer % min_values; printf("Scenario #%d\nMinimum Number of Trips = ", count); if(remainder) printf("%d\n", number_customer / min_values + 1); else printf("%d\n", number_customer / min_values); // read next data scanf("%d%d", &number_points, &number_ways); count++; } return0;} |
[UVA] 10099-the tourist guide