标签:
题目页:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=1040
Mr.G.自己也算一个人……… 反映到代码里是127行
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
|
#include <iostream> #include <stdlib.h> #include <queue> int number_ways; int number_points; struct { bool used; int parent; int n; int list[101]; }buffer_map[110]; struct Ways { int x, y; } buffer_ways[110*110]; int buffer_values[101][101]; int start, end, number_customer; // 降序排列 int compare_decrease( const void *a, const void *b) { int bx = ((Ways*)b)->x; int by = ((Ways*)b)->y; int ax = ((Ways*)a)->x; int ay = ((Ways*)a)->y; return buffer_values[bx][by] - buffer_values[ax][ay]; } void bfs() { // 1. build tree // a. init G for ( int i = 0; i < number_points; i++) { buffer_map[i].n = 0; buffer_map[i].used = false ; } // b. add ways (double dirction) for ( int i = 0; i < number_points - 1; i++) { int x = buffer_ways[i].x; int y = 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()) { int u = q.front(); q.pop(); if (u == end) return ; int length = buffer_map[u].n; for ( int i = 0; i < length; i++) { int v = buffer_map[u].list[i]; if (buffer_map[v].used == false ) { buffer_map[v].parent = u; buffer_map[v].used = true ; q.push(v); } } } } int main() { int count = 1; scanf ( "%d%d" , &number_points, &number_ways); while (number_points != 0 && number_ways != 0) { // 1. read ways for ( int i = 0; i < number_ways; i++) { int x, 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 int v = end; int min_values = buffer_values[v][buffer_map[v].parent]; while (v != start) { int next_values = buffer_values[v][buffer_map[v].parent]; if (next_values < min_values) min_values = next_values; v = buffer_map[v].parent; } min_values--; int remainder = 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++; } return 0; } |
[uva] 10099 - The Tourist Guide
标签:
原文地址:http://www.cnblogs.com/night-ride-depart/p/5065054.html