POJ-1258-Agri-Net (prime + heap)

来源:互联网 发布:基站三角定位算法 编辑:程序博客网 时间:2024/05/19 20:20
Time Limit: 1000MS
Memory Limit: 10000KTotal Submissions: 56671
Accepted: 23500

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.

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

40 4 9 214 0 8 179 8 0 1621 17 16 0

Sample Output

28

这道题其实不难,我用prime一次过,但想学一下prime+heap,所以就写了这篇博客,其中还有c++里面的知识,因为没学c++,
都是自己在网上找的,泪奔。。。。



#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 105;
const int INF = 0x3f3f3f3f;
// int cost[MAXN][MAXN];
// int minCost[MAXN];
struct Edge {
int to,cost;
Edge (int toS = 0, int costS = INF) : to(toS), cost(costS) {} //c++里面的初始化操作

};
vector<Edge> edge[MAXN];
typedef pair<int, int> Dot;
bool used[MAXN];
int costMin[MAXN];
int N;

int prime()
{
int ret = 0;
priority_queue<Dot, vector<Dot>, greater<Dot> > que; //优先队列,最小值优先
memset(costMin, INF, sizeof(costMin));
memset(used, false, sizeof(used));
costMin[0] = 0;
//used[0] = true;
que.push(Dot(0, 0));
// for (int i = 0; i < N; i++) {
// Dot dot = que.top();
// while(used[dot.second]) {
// que.pop();
// dot = que.top();
// }
// que.pop();
// int v = dot.second;
// used[v] = true;
// ret += dot.first;
// Edge es;
// for (int j = 0; j < edge[v].size(); j++) {
// es = edge[v][j];
// if (costMin[es.to] > es.cost) {
// costMin[es.to] = es.cost;
// que.push(Dot(costMin[es.to], es.to));
// }
// }
// }
while (!que.empty()) {
Dot dot = que.top(); que.pop();
int v = dot.second;
if (costMin[v] < dot.first) {
continue;
}
ret += dot.first;
for (int i = 0; i < edge[v].size(); i++) {
Edge es = edge[v][i];
if (costMin[es.to] > es.cost) {
costMin[es.to] = es.cost;
que.push(Dot(costMin[es.to], es.to));
}
}
}
return ret;
}

int main()
{
while(~scanf("%d", &N))
{
for (int i = 0; i < N; i++) { //因为多组数据,所以要清空vector,不写的话wa
edge[i].clear();
}
Edge es;
for(int i = 0; i < N; i++){
for(int j = 0; j < N; j++){
es.to = j;
scanf("%d", &es.cost);
edge[i].push_back(es);
}
}
int d = prime();
cout << d << endl;
}
return 0;
}


0 0