USACO Training Section 3.1 Agri-Net

英文原题  中文题译

最原始和经典的最小生成树,用了个最土最简单的办法做,编译了直接提交通过。

/*
ID: blackco3
TASK: agrinet
LANG: C++
*/
#include <iostream>
using namespace std;
#define _max_farm_ 100
int n_farm, dist[_max_farm_][_max_farm_], is_conn[_max_farm_];

int main() {
	freopen("agrinet.in", "r", stdin);
	freopen("agrinet.out", "w", stdout);
	cin >> n_farm ;
    for(int i=0; i<n_farm; i++)
	    for(int j=0; j<n_farm; j++) 
			cin >> dist[i][j] ;
	int total = 0;
	is_conn[0] = 1 ;
    for(int n_edge=0; n_edge < n_farm-1; n_edge++) {
		int min_dis = 0, sel_farm ;
		for(int i=0; i<n_farm; i++)
			for(int j=0; j<n_farm; j++) 
				if(dist[i][j] && is_conn[i] && !is_conn[j]) 
	    			if(min_dis == 0 || dist[i][j] < min_dis) 
						min_dis = dist[i][j], sel_farm = j;
		is_conn[sel_farm]=1, total += min_dis;
	}
	cout << total << endl ;
	return 0;
}

你可能感兴趣的:(c,.net,J#,asp.net,asp)