USACO agrinet Prim

裸最短生成树

/*
ID:kevin_s1
PROG:agrinet
LANG:C++
*/

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <cstdlib>
#include <list>
#include <cmath>

using namespace std;

#define MAXN 110
#define INF 100001

//gobal variable====

int N;
int Edge[MAXN][MAXN];
int lowcost[MAXN];
int sumdist;

//==================

//function==========
void prim(){
	lowcost[1] = -1;
	int v;
	for(int i = 2; i <= N; i++){
		lowcost[i] = Edge[1][i];
	}
	for(int i = 2; i <= N; i++){
		int min = INF;
		for(int k = 2; k <= N; k++){
			if(lowcost[k] != -1 && lowcost[k] < min){
				v  = k;
				min = lowcost[k];
			}
		}
		sumdist += min;
		lowcost[v] = -1;
		for(int k = 1; k <= N; k++){
			if(lowcost[k] > Edge[v][k])
				lowcost[k] = Edge[v][k];
		}
	}
}

//==================

int main(){
	freopen("agrinet.in","r",stdin);
	freopen("agrinet.out","w",stdout);
	cin>>N;
	for(int i = 1; i <= N; i++){
		for(int j = 1; j <= N; j++){
			cin>>Edge[i][j];
		}
	}
	memset(lowcost, 0, sizeof(lowcost));
	sumdist = 0;
	prim();
	cout<<sumdist<<endl;
	return 0;
}

USACO agrinet Prim,布布扣,bubuko.com

时间: 2024-10-26 00:31:36

USACO agrinet Prim的相关文章

POJ 1258 Agri-Net (prim最小生成树)

最小生成树模板题 #include<bits/stdc++.h> using namespace std; int n,a; int dist[120],m[120][120]; void prim() {     bool p[1020];     for(int i=2;i<=n;i++)     {         p[i]=false;         dist[i]=m[1][i];     }     dist[1]=0,p[1]=true;     for(int i=1;

POJ 1258 Agri-Net(Prim)

题目网址:http://poj.org/problem?id=1258 题目: Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 60004   Accepted: 24855 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet conne

poj 1258 Agri-Net(Prim)(基础)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 44487   Accepted: 18173 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

POJ 1258 Agri-Net (Prim&amp;Kruskal)

题意:FJ想连接光纤在各个农场以便网络普及,现给出一些连接关系(给出邻接矩阵),从中选出部分边,使得整个图连通.求边的最小总花费. 思路:裸的最小生成树,本题为稠密图,Prim算法求最小生成树更优,复杂度O(n^2) prim: #include <cstdio> #include <iostream> #include <algorithm> #include <cstring> using namespace std; int mat[110][110]

poj 1258 Agri-Net prim模板 prim与dijkstra的区别

很裸地求最小生成树的题目.题意就不多说了,最重要的就是记录一下学会了prim算法. 初学prim,给我的第一感觉就是和dijkstra好像啊,感觉两者的区别还是有的: 1:prim是求最小生成树的算法. dijkstra是求最短路的算法. 2:prim中dis保存的是未加入集合的点,加入集合需要的代价,而中的代价是其他变量保存的. dijkstra中dis保存的是从起点到当前点所花费的最小代价. 1 #include <cstdio> 2 #include <cstring> 3

POJ1258 Agri-Net

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 46319   Accepted: 19052 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

POJ1258 Agri-Net 【最小生成树Prim】

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 40889   Accepted: 16677 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

POJ 1258 Agri-Net (最小生成树+Prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39820   Accepted: 16192 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee

poj 1258 Agri-Net (最小生成树 prim)

Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 39499   Accepted: 16017 Description Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He nee