#include #include /**************************************************************** * * Class based on ANSI C standard clock() routine to time * execution. * ****************************************************************/ class CPUStopWatch { public: /* Call this routine to start the "CPU stop watch." Can be called again to "reset" the timer. */ void startTimer(void) { _startTime = clock(); } /* Returns the elapsed CPU time since startTimer() was called in milliseconds. Be sure to call startTimer() first! */ unsigned long elapsedTime(void) { return (unsigned long) (((float) (clock() - _startTime) / (float) CLOCKS_PER_SEC) * 1000); } private: clock_t _startTime; }; /**************************************************************** * * A simple struct to hold the input and output edges. * ****************************************************************/ struct SimpleEdge { int u, v; // u and v represent the two endpoints of the // edge; the order is irrelevant since the graph is // undirected. int weight; // The weight of the edge; guaranteed to be a // positive integer. }; /* YOU ARE RESPONSIBLE FOR IMPLEMENTING THIS FUNCTION! * * INPUT: * num_vertices: The number of vertices in the graph. The vertices * are represented by the numbers 0 ... (num_vertices - 1). * * num_edges: The number of edges in the input graph. * * input_edges: An array containing the edges in the input graph. * * OUTPUT: * output_edges: The edges in the MCST. Our graphs will assume that * the input graph has a spanning tree, so there * must be exactly num_vertices - 1 edges in the array. */ void do_MCST(int num_vertices, int num_edges, SimpleEdge input_edges[], SimpleEdge output_edges[]) { } /* Times the execution of the do_MCST routine. Iterates the process several times to get an accurate reading in the case of small examples. Therefore, it is IMPERATIVE that your do_MCST routine deallocate any memory it allocates! It prints information about the elapsed time to cerr. */ void timed_MCST(int num_vertices, int num_edges, SimpleEdge input_edges[], SimpleEdge output_edges[]) { CPUStopWatch timex; unsigned long elapsed; int iters = 0; timex.startTimer(); do { iters++; do_MCST(num_vertices, num_edges, input_edges, output_edges); } while ((elapsed = timex.elapsedTime()) < 1000); cerr << "Total CPU time: " << elapsed << " ms. Iterations: " << iters << " Time per iteration: " << (elapsed / iters) << " ms.\n"; } int main(void) { int num_v, num_e, e; SimpleEdge *input_edges, *output_edges; cin >> num_v; cin >> num_e; input_edges = new SimpleEdge[num_e]; output_edges = new SimpleEdge[num_v - 1]; for (e = 0; e < num_e; e++) cin >> input_edges[e].u >> input_edges[e].v >> input_edges[e].weight; timed_MCST(num_v, num_e, input_edges, output_edges); for (e = 0; e < (num_v - 1); e++) cout << output_edges[e].u << ' ' << output_edges[e].v << ' ' << output_edges[e].weight << '\n'; delete [] input_edges; delete [] output_edges; return 0; }