Agri-Net(最小生成树)

来源:互联网 发布:中级经济师题库软件 编辑:程序博客网 时间:2024/05/18 01:18

原题链接
Agri-Net
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 52814 Accepted: 21996
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

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
Sample Output

28
Source

USACO 102

//http://poj.org/problem?id=1258#include <algorithm>#include <iostream>#include <utility>#include <sstream>#include <cstring>#include <cstdio>#include <vector>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>using namespace std;typedef long long ll;const int MOD = int(1e9) + 7;//int MOD = 99990001;const int INF = 0x3f3f3f3f;const ll INFF = (~(0ULL)>>1);const double EPS = 1e-9;const double OO = 1e20;const double PI = acos(-1.0); //M_PI;const int fx[] = {-1, 1, 0, 0};const int fy[] = {0, 0, -1, 1};const int maxn=100 + 5;int n;int cost[maxn][maxn],mincost[maxn];bool used[maxn];int prim(){        for(int i=0;i<n;i++){                mincost[i]=INF;                used[i]=false;        }        mincost[0]=0;        int res=0;        while(true){                int v=-1;                for(int i=0;i<n;i++){                        if(!used[i] && (v==-1 || mincost[i]<mincost[v])){                                v=i;                        }                }                if(v==-1) break;                used[v]=true;                res+=mincost[v];                for(int i=0;i<n;i++)                        mincost[i]=min(mincost[i],cost[v][i]);        }        return res;}int main(){        while(scanf("%d",&n)==1){                for(int i=0;i<n;i++)                        for(int j=0;j<n;j++)                                scanf("%d",&cost[i][j]);                cout << prim() << endl;        }        return 0;}//明显矩阵形式输入的用prim算法是很方便的了,当然有kruskal算法也是可以的/*//http://poj.org/problem?id=2395#include <algorithm>#include <iostream>#include <utility>#include <sstream>#include <cstring>#include <cstdio>#include <vector>#include <queue>#include <stack>#include <cmath>#include <map>#include <set>using namespace std;typedef long long ll;const int MOD = int(1e9) + 7;//int MOD = 99990001;const int INF = 0x3f3f3f3f;const ll INFF = (~(0ULL)>>1);const double EPS = 1e-9;const double OO = 1e20;const double PI = acos(-1.0); //M_PI;const int fx[] = {-1, 1, 0, 0};const int fy[] = {0, 0, -1, 1};const int maxn=10000 + 5;struct edge{int x,y,val;}E[maxn];int n,r;int fa[maxn],deep[maxn];bool cmp(edge a,edge b){        return a.val<b.val;}int findfa(int x){        return x==fa[x] ? x : fa[x]=findfa(fa[x]);}void unite(int a,int b){        int faa=findfa(a);        int fab=findfa(b);        if(faa==fab) return;        else{                if(deep[faa]<deep[fab]){                        fa[faa]=fab;                }                else if(deep[faa]>deep[fab]){                        fa[fab]=faa;                }                else{                        fa[fab]=faa;                        deep[faa]++;                }        }}int kruskal(){        sort(E,E+r,cmp);        int res=0;        for(int i=0;i<maxn;i++){                fa[i]=i;                deep[i]=0;        }        for(int i=0;i<r;i++){                if(findfa(E[i].x) != findfa(E[i].y)){                        unite(E[i].x,E[i].y);                        res+=E[i].val;                }        }        return res;}int main(){        while(scanf("%d",&n)==1){                r=0;                for(int i=0;i<n;i++){                        for(int j=0;j<n;j++){                                int t;                                scanf("%d",&t);                                if(i<j){                                        E[r++]=(edge){i,j,t};                                }                        }                }                cout << kruskal() << endl;        }        return 0;}*/
0 0