POJ 1258 Agri-Net【MST】

Agri-Net

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 49389   Accepted: 20511

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

题目大意:john想把n个农场连接到一起,询问最小花费。给出邻接矩阵的值,求MST

思路:贪心克鲁斯卡尔算法AC

代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int map[150][150];
int f[150];
struct path
{
    int x,y,w;
}a[100*100*2];
int cmp(path a,path b)
{
    return a.w<b.w;
}
int find(int a)
{
    int r=a;
    while(f[r]!=r)
    r=f[r];
    int i=a;
    int j;
    while(i!=r)
    {
        j=f[i];
        f[i]=r;
        i=j;
    }
    return r;
}
void merge(int a,int b)
{
    int A,B;
    A=find(a);
    B=find(b);
    if(A!=B)
    f[B]=A;
}
int main()
{
    int n;
    while(~scanf("%d",&n))
    {
        int cont=0;
        for(int i=0;i<n;i++)
        {
            f[i]=i;
        }
        for(int i=0;i<n;i++)
        {
            for(int j=0;j<n;j++)
            {
                int k;
                scanf("%d",&k);
                a[cont].x=i;
                a[cont].y=j;
                a[cont++].w=k;
            }
        }
        int output=0;
        sort(a,a+cont,cmp);
        for(int i=0;i<cont;i++)
        {
            if(find(a[i].x)!=find(a[i].y))
            {
                merge(a[i].x,a[i].y);
                output+=a[i].w;
            }
        }
        printf("%d\n",output);
    }
}
时间: 2024-10-30 00:49:15

POJ 1258 Agri-Net【MST】的相关文章

poj 1808 Quadratic Residues 【平方剩余】【数论】

题目链接:http://poj.org/problem?id=1808 题目大意:给你T组数据,每组数据一个a一个n,判断 x^2  ≡  a ( mod  n ) 能否成立.成立则输出1否则输出-1. 一个简单的平方剩余,只用判断能否有解即可. #include<stdio.h> #define LL long long LL pow_mod(LL a, LL n, LL mod) { LL res = 1; while(n) { if (n & 1) res = res * a %

POJ 2239 Selecting Courses【最大匹配】

大意: 有一些课程,每个课程的上课时间在每周可能会有多节,你可以从中任选一个时间去听课, 每周七天一天12节课  告诉你每节课的上课时间问在不冲突的情况下最多上几节课? 分析: 左集合课程 右集合时间 边为该节课对应的上课时间 求最大匹配就行了 代码: 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <vector> 5 using namespace std

poj 1321 棋盘问题 【DFS】

题意:... 策略:深搜. 仔细分析我们发现,我们只需要对列进行标记,对于行我们考虑放棋子还是不放就行了. 代码: #include<stdio.h> #include<string.h> char s[10][10]; int n, m; int vis[10]; int ans; void dfs(int cur, int step) { if(step == m){ ans ++; return; } if(cur > n-1) return ; int i, j; f

poj 3258 River Hopscotch 【二分】

题目真是不好读,大意如下(知道题意就很好解了) 大致题意: 一条河长度为 L,河的起点(Start)和终点(End)分别有2块石头,S到E的距离就是L. 河中有n块石头,每块石头到S都有唯一的距离 问现在要移除m块石头(S和E除外),每次移除的是与当前最短距离相关联的石头,要求移除m块石头后,使得那时的最短距离尽可能大,输出那个最短距离. //Memory Time //420K 391MS #include<iostream> #include<algorithm> using

poj 2689 Prime Distance 【数论】【筛法求素数】

题目链接:传送门 题目大意: 给你L和R两组数,L和R的范围是2^32,其间隔(即R-L最大为1,000,000.) .让你求出L和R之间素数的最大间隔和最小的间隔. 比如 2 17.之间的最小素数间隔是2 3,最大的素数间隔是11 17. 要是直接进行一个2^32次方筛法然后在判断是会T的. 我们这样来想,筛法求素数的原理是什么: /**vis数组标记为0则说明是素数*/ int vis[10005]; void getPrimevis(int n) { int m=sqrt(n+0.5);

poj 1284 Primitive Roots 【原根】【数论】

题目链接 :传送门 题目大意: 求一个质数的原根个数. 先普及一下原根的定义: 设m是正整数,a是整数,若a模m的阶等于euler(m),则称a为模m的一个原根. eg: m=7,euler(7) =  6(1,2,3,4,5,6) 则: 1   1^(n)mod7=1! = 6 2   2^(n)mod7={2 4 1}!=6 3   3^(n)mod7={3,2,6,4,5,1}==6   故3是模7的原根 4   4^(n)mod7={4,2,1}!=6 5   5^(n)mod7={5,

POJ 2449 Remmarguts&#39; Date【SPFA】【A*】

Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 21978 Accepted: 5982 Description "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. Softly touching his little ducks' head, he to

矩阵十题【二】 poj 1575 Tr A 【矩阵】

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1575 题目大意:A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. 数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n(2 <= n <= 10)和k(2 <= k < 10^9)两个数据.接下来有n行,每行有n个数据,每个数据的范围是[0,9],表示方阵A的内容. 一个矩阵快速幂的裸题. 题解: #include<iostr

图论常用算法之一 POJ图论题集【转载】

POJ图论分类[转] 一个很不错的图论分类,非常感谢原版的作者!!!在这里分享给大家,爱好图论的ACMer不寂寞了... (很抱歉没有找到此题集整理的原创作者,感谢知情的朋友给个原创链接) POJ:http://poj.org/ 1062* 昂贵的聘礼 枚举等级限制+dijkstra 1087* A Plug for UNIX 2分匹配 1094 Sorting It All Out floyd 或 拓扑 1112* Team Them Up! 2分图染色+DP 1125 Stockbroker

poj 2186 Popular Cows 【强连通】

题目:poj 2186 Popular Cows 题意:n头牛,其中存在一些牛相互崇拜,具有传递性,问有多少头牛是被其他所有牛崇拜的. 分析:建立一个有向图,然后强连通缩点,之后求出度为0的点,假如存在多个,那么ans = 0,因为缩点之后如果x崇拜y,x也崇拜z,那么肯定y和z不能互相崇拜,不满足. 然后求出度为0的这个点缩点前环上有多少个点就ans AC代码: #include <cstdio> #include <vector> #include <iostream&g