caykhungnhonhat

 avatar
quoc14
c_cpp
5 months ago
1.2 kB
2
Indexable
caidat
#define _CRT_SECURE_NO_WARNINGS
#define SIZE 109

#include<iostream>

using namespace std;

int visit[SIZE];
int A[SIZE][SIZE];
int countdown;
int Answer;
int i, j;
int mini;
int jmini;

int main(int argc, char** argv) {
	int test_case;
	int T, N;

	//ios::sync_with_stdio(false);

	cin >> T;

	for (test_case = 1; test_case <= T; ++test_case) {
		Answer = 0;

		cin >> N;
		for (i = 1; i <= N; i++) {
			for (j = 1; j <= N; j++) {
				cin >> A[i][j];
			}
			visit[i] = 0;
		}

		// PRIM
		// Add 1st vertex to MST
		visit[1] = 1;
		countdown = 1;
		//Add one by one vertex to MST
		while (countdown < N) {
			//Find the minimum weight vertex
			mini = 1000000;
			for (i = 1; i <= N; i++) {
				if (visit[i] == 1) {
					for (j = 1; j <= N; j++) {
						if (j != i && !visit[j]) {
							if (A[i][j] < mini) {
								mini = A[i][j];
								jmini = j;
							}
						}
					}
				}
			}
			//Add found vertex
			visit[jmini] = 1;
			Answer += mini;
			countdown++;
		}


		// Print the answer to standard output(screen).
		cout << "Case #" << test_case << endl << Answer << endl;
	}
	//while(1);
	return 0;//Your program should return 0 on normal termination.
}

Leave a Comment