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.


The input includes several cases. For each case, the first line contains the number of farms, N (3


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

0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0

Sample Output




#define maxn 1000
#define INF 0x3f3f3f3f
using namespace std;
int n, m, fa[maxn];
bool vis[maxn];
int dis[maxn];
int mp[maxn][maxn];

int prim(int n) {
    int ans = 0;
    memset(vis, false, sizeof(vis));
    vis[0] = true;
    for (int i = 0; i 0][i];
    for (int i = 1; i int minn = INF;
        int pos = -1;
        for (int j = 0; jif (!vis[j] && minn>dis[j]) {
                minn= dis[j];
                pos= j;

        if (minn == INF) return -1;
        ans+= minn;
        vis[pos] = true;

        for (int j = 0; jif (!vis[j] && dis[j] > mp[pos][j])
                dis[j] = mp[pos][j];

    return ans;

int main()
    while (~scanf_s("%d",&n))
        for (int i = 0; i for (int j = 0; j "%d", &mp[i][j]);
        int ans = prim(n);
        printf("%d\n", ans);
    return 0;

本博客所有内容采用 知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议 进行许可

转载文章请注明:POJ 1258 – Agri-Net(最小生成树 Prim) -

分类: Coding





电子邮件地址不会被公开。 必填项已用*标注