http://poj.org/problem?id=1258
G级光纤:农夫约翰当上村长,要给全村建光纤,求最小花费?
最小生成树
Read full article from 1258 -- Agri-Net
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 needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28http://www.hankcs.com/program/cpp/poj-1258-agri-net.html
G级光纤:农夫约翰当上村长,要给全村建光纤,求最小花费?
最小生成树
int mincost[MAX_V]; // 从集合X出发的边到每个顶点的最小权值bool used[MAX_V]; // 顶点i是否包含在集合X中int V; // 顶点数// first 最短路径,second顶点编号typedef pair<int, int> P;struct edge{ int to, cost; edge(int to = 0, int cost = 0) : cost(cost), to(to) {}};// 边vector<edge> G[MAX_V]; // G[i] 顶点i到G[i].to的权值为G[i].costint prim(){ int res = 0; memset(mincost, 0x3f, V * sizeof(int)); memset(used, 0, V * sizeof(bool)); mincost[0] = 0; priority_queue<P, vector<P>, greater<P> > que; que.push(P(0, 0)); while (!que.empty()) { P p = que.top(); que.pop(); int v = p.second; if (used[v] || p.first > mincost[v]) continue; used[v] = true; res += mincost[v]; for (int i = 0; i < G[v].size(); ++i) { edge e = G[v][i]; if (mincost[e.to] > e.cost) { mincost[e.to] = e.cost; que.push(P(mincost[e.to], e.to)); } } } return res;}int main(int argc, char *argv[]){#ifndef ONLINE_JUDGE freopen("in.txt", "r", stdin); freopen("out.txt", "w", stdout);#endif while (cin >> V && V) { for (int i = 0; i < V; ++i) { G[i].clear(); for (int j = 0; j < V; ++j) { int cost; cin >> cost; G[i].push_back(edge(j, cost)); } } cout << prim() << endl; }#ifndef ONLINE_JUDGE fclose(stdin); fclose(stdout); system("out.txt");#endif return 0;}