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
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
const int mod = ;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn = ; int u,v,w;
int n,m,ans,k,sum,cnt;
int a[][];
struct node
{
int u,v,w;
}e[maxn];
int fa[maxn];
int Find(int x)
{
if(fa[x]!=x)
fa[x]=Find(fa[x]);
return fa[x];
}
void join(int x,int y)
{
int xx = Find(x);
int yy = Find(y);
fa[xx]=yy;
}
bool cmp(node a,node b)
{
return a.w < b.w;
}
void kruskal()
{
cnt=;
rep(i,,m)
{
int x=e[i].u;
int y=e[i].v;
if(Find(x)!=Find(y))
{
join(x,y);
cnt++;
sum += e[i].w;
}
if(cnt == n-) break;
}
printf("%d\n",sum);
}
int main()
{
while(~scanf("%d",&n))
{
sum=,cnt=,m=,ms(e,);
rep(i,,n)
fa[i]=i;
rep(i,,n)
{
rep(j,,n)
{
scanf("%d",&a[i][j]);
if(j<i) //对称的无向图,建一半即可
{
e[++m].u = i;
e[m].v = j;
e[m].w = a[i][j]; //注意是m条边
}
}
}
sort(e+, e+m+, cmp);
kruskal();
}
}
/*
【题意】
给你n*n矩阵表示i(行)和j(列)之间的权值,求该图的MST。 【类型】
最小生成树模板题 【分析】 【时间复杂度&&优化】 【trick】
*/
You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. 
Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal.

Input

The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P=0 denotes the end of the input. The data sets are separated with an empty line. 
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as i j or as j i. 

Output

For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network.

Sample Input

1 0

2 3
1 2 37
2 1 17
1 2 68 3 7
1 2 19
2 3 11
3 1 7
1 3 5
2 3 89
3 1 91
1 2 32 5 7
1 2 5
2 3 7
2 4 8
4 5 11
3 5 10
1 5 6
4 2 12 0

Sample Output

0
17
16
26
#include<cstdio>
#include<string>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<cstring>
#include<set>
#include<queue>
#include<algorithm>
#include<vector>
#include<map>
#include<cctype>
#include<stack>
#include<sstream>
#include<list>
#include<assert.h>
#include<bitset>
#include<numeric>
#define debug() puts("++++")
#define gcd(a,b) __gcd(a,b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a,b,sizeof(a))
#define sz size()
#define be begin()
#define pu push_up
#define pd push_down
#define cl clear()
#define lowbit(x) -x&x
#define all 1,n,1
#define rep(i,x,n) for(int i=(x); i<=(n); i++)
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e18;
const int maxm = 1e6 + ;
const double PI = acos(-1.0);
const double eps = 1e-;
const int dx[] = {-,,,,,,-,-};
const int dy[] = {,,,-,,-,,-};
int dir[][] = {{,},{,-},{-,},{,}};
const int mon[] = {, , , , , , , , , , , , };
const int monn[] = {, , , , , , , , , , , , };
const int mod = ;
#define inf 0x3f3f3f3f
#define ll long long
const int maxn = ; int u,v,w;
int n,m,ans,k,sum,cnt;
int a[][];
struct node
{
int u,v,w;
}e[maxn]; int fa[maxn]; int Find(int x)
{
if(fa[x]!=x)
fa[x]=Find(fa[x]);
return fa[x];
}
void join(int x,int y)
{
int xx = Find(x);
int yy = Find(y);
fa[xx]=yy;
}
bool cmp(node a,node b)
{
return a.w < b.w;
}
void kruskal()
{
cnt=;
rep(i,,m)
{
int x=e[i].u;
int y=e[i].v;
if(Find(x)!=Find(y))
{
join(x,y);
cnt++;
sum += e[i].w;
}
if(cnt == n-) break;
}
printf("%d\n",sum);
}
int main()
{
while(~scanf("%d%d",&n,&m))
{
if(n==) break;
sum=,cnt=;
rep(i,,n)
fa[i]=i;
for(int i=;i<=m;i++)
scanf("%d%d%d",&e[i].u,&e[i].v,&e[i].w);
sort(e+, e+m+, cmp);
kruskal();
}
}
/*
【题意】
给你u v w表示u和v之间的权值w,求该图的MST。 【类型】
最小生成树模板题 【分析】 【时间复杂度&&优化】 【trick】
*/

POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】的更多相关文章

  1. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  2. POJ 2195 Going Home 最小费用流 裸题

    给出一个n*m的图,其中m是人,H是房子,.是空地,满足人的个数等于房子数. 现在让每个人都选择一个房子住,每个人只能住一间,每一间只能住一个人. 每个人可以向4个方向移动,每移动一步需要1$,问所有 ...

  3. HDU 1102 最小生成树裸题,kruskal,prim

    1.HDU  1102  Constructing Roads    最小生成树 2.总结: 题意:修路,裸题 (1)kruskal //kruskal #include<iostream> ...

  4. poj 2135 Farm Tour 最小费用最大流建图跑最短路

    题目链接 题意:无向图有N(N <= 1000)个节点,M(M <= 10000)条边:从节点1走到节点N再从N走回来,图中不能走同一条边,且图中可能出现重边,问最短距离之和为多少? 思路 ...

  5. 图论--网络流--最大流--POJ 3281 Dining (超级源汇+限流建图+拆点建图)

    Description Cows are such finicky eaters. Each cow has a preference for certain foods and drinks, an ...

  6. 网络流--最大流--POJ 2139(超级源汇+拆点建图+二分+Floyd)

    Description FJ's cows really hate getting wet so much that the mere thought of getting caught in the ...

  7. POJ 1287 Networking(最小生成树裸题有重边)

    Description You are assigned to design network connections between certain points in a wide area. Yo ...

  8. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

  9. Networking POJ - 1287 最小生成树板子题

    #include<iostream> #include<algorithm> using namespace std; const int N=1e5; struct edge ...

随机推荐

  1. Maven -- 将引用的本地jar文件打进war包里

    我们在做maven项目开发中有时候引用了本地第三方包,例如: <dependency> <groupId>org.artofsolving.jodconverter</g ...

  2. HDFS不存在绝对路径,无法找到文件所在具体位置

    This is set in the dfs.datanode.data.dir property, which defaults to file://${hadoop.tmp.dir}/dfs/da ...

  3. mave的依赖范围

    compile(编译范围) compile是默认的范围:如果没有提供一个范围,那该依赖的范围就是编译范 围.编译范围依赖在所有的classpath中可用,同时它们也会被打包. provided(已提供 ...

  4. js原生读取json

    function showJson(){ var test; if(window.XMLHttpRequest){ test = new XMLHttpRequest(); }else if(wind ...

  5. python基础=== itertools介绍(转载)

    原文链接:http://python.jobbole.com/85321/ Python提供了一个非常棒的模块用于创建自定义的迭代器,这个模块就是 itertools.itertools 提供的工具相 ...

  6. mongodb-linux-x86_64

    卷 DataDisk 的文件夹 PATH 列表卷序列号为 4A8E-D95CD:.│  1.txt│  GNU-AGPL-3.0│  MPL-2│  README│  THIRD-PARTY-NOTI ...

  7. 判断cookie创建的时间是否已经24小时

    def read_cookie(self): cookiesfilepath="cookies%s" % self.uid if os.path.exists(cookiesfil ...

  8. vue-router 基础

    安装 NPM npm install vue-router 如果在一个模块化工程中使用它,必须要通过 Vue.use() 明确地安装路由功能: import Vue from 'vue' import ...

  9. puppet安装和配置

    一.安装puppet准备 //安装准备 ,两台机器都要操作 . 两台机器 172.7.15.106 (server) 172.7.15.111 (client) . 关闭防火墙 setenforce ...

  10. 安装lszrz,用于上传文件

    wget http://down1.chinaunix.net/distfiles/lrzsz-0.12.20.tar.gztar zxvf lrzsz-0.12.20.tar.gzcd lrzsz- ...