#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;
}
|