#define NDEBUG // NDEBUG definita nella versione che consegno #include #ifndef NDEBUG # include // uso di cin e //cout non consentito in versione finale #endif #include #include /* *input A non empy connected weighted graph with vertices V and edges E *initialize: Vnew = {X} where X is an arbitrary node (starting point) from V,Enew=0 *repeat until Vnew=V * - choose an edge {u,v} with minimal weight such that u is in Vnew and v is not * (if there are multiple edges with the same weight any of them may be picked) * - add v to Vnew and {u,v} to Enew *Output: Vnew and Enew describe a minmal spanning tree */ using namespace std; int N, M, Q; const int MAX_N=10000, MAX_M=1000000, MAX_Q=100, MAX_W=1048576; //1048576=2^20 int ROW[MAX_M][3]; int RED[MAX_Q]; int V_new[MAX_N]; int V_new_size=0; int E_new[MAX_N]; int E_new_size=0; int edge_node_from(int pos) {return ROW[pos][0];} int edge_node_to(int pos) {return ROW[pos][1];} int edge_weight(int pos) {return ROW[pos][2];} void printEdge(int pos) { //cout << "["<< edge_node_from(pos) <<"][" << edge_node_to(pos) <<"][" << edge_weight(pos) <<"]"; } bool isInVnew(int node) { for(int i = 0; i < V_new_size; i++) { if(V_new[i]==node) { ////cout << node << " is in V_new"; return true; } } ////cout << node << " is NOT in V_new"; return false; } /** * @brief searchMinimalEdge Choose an edge {u,v} with minimal weight such that u is in V_new and v is not * @param from u in V_new * @return position in ROW for edge {u,v} */ int searchMinimalEdge(int from) { //cout << endl << "------------ MINIM --------------" <-1) { //cout << " - Found Edge "; printEdge(chosenEdge); V_new[V_new_size]=look_v; //aggiungi il vertice V_new_size++; //cout << " - ADD to V_new (new size: " << V_new_size << ")"; E_new[E_new_size] = chosenEdge; //aggiungi l'arco E_new_size++; //cout << " - ADD to E_new (new size: " << E_new_size << ")"; min_w += edge_weight(chosenEdge); //cout << " - Total Weight: " << min_w; } else { //cout << " - Not Found Edge with pos " << chosenEdge; } //cout << endl; look_v++; } while (V_new_size!=N && look_v> N; fin >> M; //fin >> Q; Q=0; for(int i = 0; i < M; i++) { fin >> ROW[i][0]; fin >> ROW[i][1]; fin >> ROW[i][2]; } fin.close(); //cout << "N: "<< N <<" - M: "<< M <<" - Q: "<< Q << endl; for(int i = 0; i < M; i++) { // printEdge(i); cout << endl; } for(int i = 1; i <= N; i++) {isInVnew(i);} for(int i = 0; i < Q; i++) { RED[i]=0; } ofstream fout("output.txt"); assert( fout ); fout << getMinW() << endl; // di quanto devono essere ridotti i primi Q archi per essere parte della soluzione ottima for(int i = 0; i < Q; i++) { fout << RED[i] << endl; } fout.close(); return 0; }