pku1258Agri-Net(MST)

Xredman posted @ Apr 06, 2010 02:20:37 AM in Algorithm with tags pku MST , 1152 阅读

//6693590 Xredman 1258 Accepted 356K 16MS C++ 1086B 2010-04-06 13:07:43 
#include <iostream>
#include <cstdio>
#include <climits>
using namespace std;

const int MAX_SIZE = 502;
const int INF = 0x7FFFFFFF;

int G[MAX_SIZE][MAX_SIZE];
int lowcost[MAX_SIZE],closeset[MAX_SIZE];

int n;//共有n个节点

void readin()
{
	int i, j;
	for(i = 0; i < n; i++)
	{
		for(j = 0; j < n; j++)
		{
			scanf("%d", &G[i][j]);
		}
	}
}

int prim(int v0)
{
	int i, j, k, minx;
	int sum = 0;;

	for(i = 0; i < n; i++)
	{//初始化辅助数组
		lowcost[i] = G[v0][i];
		closeset[i] = v0;
	}
	
	for(i = 1; i < n; i++)
	{
		//寻找离生成树最近的未加入顶点k
		minx = INF;
		for(j = 0; j < n; j++)
		{
			if(lowcost[j] < minx && lowcost[j] != 0)
			{
				minx = lowcost[j];
				k = j;
			}
		}
		//将顶点k加入生成树
		lowcost[k] = 0;
		sum += minx;
		/*
		*生成树中增加一条新边k到closeset[k],
		*修正各点的lowcost和closeset值
		*/
		for(j = 0; j < n; j++)
		{
			if(G[k][j] < lowcost[j])
			{
				lowcost[j] = G[k][j];
				closeset[j] = k;
			}
		}
	}
	return sum;
}

int main()
{
	while(scanf("%d", &n) != EOF)
	{
		readin();
		printf("%d\n", prim(0));
	}
	return 0;
}

 

 






Talent Pool Scholars 说:
Aug 27, 2022 03:12:12 AM

Bangladesh All Education Board has announced PSC Scholarship Result 2022 on last week of December 2022 for all divisional educational boards under DPE, and they have going to issue Primary Britti 2022 to all qualified student, and the DPE is going to announce likely on March or April 2022 with school wise selection list for Talent Pool Scholarship and General Grade Scholarship to all eligible student under Prathomik Somaponi Result 2022. Every year the All Education Board Bangladesh has announced a selected candidate list school wise and district wise to all divisional boards of Dhaka, Chittagong, Comilla, Rajshahi, Sylhet, Barisal, Jessore, Talent Pool Scholarship Dinajpur, and Madrasah Board working under DPE and this year also going to declare PSC Scholarship Result 2022 with selection list and type of scholarship through PSC Britty 2022.

jnanabhumiap.in 说:
Jan 19, 2024 06:17:26 AM

JNANABHUMI AP offers the most recent information on education. The primary idea or goal of this website has been to offer comprehensive resources with information on any subject that can be accessed online. To make sure that every reader finds out what they should know about the subject they are interested in and jnanabhumiap.in links to our content.Jnanabhumi AP is a startup founded by enthusiastic bloggers and webmasters who are driven to produce interesting, factual, and well-written content. We are similar to an online community where you can find a variety of resources, information, and topics about newsworthy events or daily occurrences.


登录 *


loading captcha image...
(输入验证码)
or Ctrl+Enter