POJ 1258 Agri-Net (最小產生樹+Prim)

來源:互聯網
上載者:User

標籤:poj   最小產生樹   

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 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

Source

USACO 102
代碼:(新手 勿噴)

Slyar:簡單介紹一下題意。農民要建立互連網絡,目的使村莊裡所有的農民連上網,並且總費用最小。多組資料,每組資料給出一個n,然後給出n * n大小的無向圖的鄰接矩陣表示,值表示邊權。要求輸出最小產生樹的權值和。

可以用Kruskal演算法解決該題,用並查集檢查待加入產生樹的兩邊是否會構成迴路,快速排序按權值排列邊。

這裡有一個最佳化:因為是無向圖,所以矩陣是對稱的,因此我們只儲存上三角矩陣即可。這樣到最後k的值就是邊數,由迴圈n*n次縮減到迴圈k次...
不過我用的是Prim......
全是模板啦,我也不怎麼會解釋,盡量啦。
#include <iostream>#include <algorithm>#include <cstdio>using namespace std;#define M 101#define INF 1000001int map[M][M];int n;int Prim(){    int s=1;    int m=1;    bool cmp[M]={0};   //標記這個點是否在子圖內。    cmp[s]=1;          //從第一個點開始。    int min_w;         //用來找當前的最小權邊。    int prim_w=0;      //用來存線路的總長。    int point;         //這是用來存那個即將入圖的那個點的。    int low_dis[M];    //這個用來存子圖到這個點的最短距離。(關鍵)    for(int i=1;i<=n;i++)        low_dis[i]=INF;  //初始化一定要大。    while(1)     {         if(m==n) break; //如果每個點都入圖,已經將所有點連好了。        min_w=INF;        for(int i=2;i<=n;i++)        {            if(!cmp[i] && low_dis[i]>map[s][i])                 low_dis[i]=map[s][i];          //如果子圖有更短的才替換(關鍵)            if(!cmp[i] && min_w>low_dis[i])            {                 min_w=low_dis[i];                point=i;                      //這就是找那個最小權邊,並標記它的下標。            }        }        s=point;        cmp[s]=1;                           //這個點入圖,加上權邊值。        prim_w+=min_w;        m++;  //子圖中的點數+1.      }    return prim_w;}int main(){    int i,j;    while(cin>>n)    {        for(i=1;i<=n;i++)        for(j=1;j<=n;j++)        cin>>map[i][j];        cout<<Prim()<<endl;    }    return 0;}
我也沒辦法。。。模板就這樣。。。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.