最小生成树 --- 求最小权值、MST

Agri-Net










Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 37109   Accepted: 14982

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

【题目来源】

USACO
102

【题目大意】

给定一个强连通图,让你求最小生成树的权值之和。

【题目分析】

数据很水,用Kruskal或者prim都能水过。


#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<cstring>
#define MAX 600*300
using namespace std;
struct Node
{
int a,b,c;
};
Node node[MAX];
int parent[MAX];
int num[600][600];
int sum;
int temp;

int Find(int x)
{
return x==parent[x]?x:parent[x]=Find(parent[x]);
}

void Kruskal()
{
int x,y;
int i,j;
for(i=0;i<temp;i++)
{
x=node[i].a;
y=node[i].b;
x=Find(x);
y=Find(y);
if(x!=y)
{
parent[x]=y;
sum+=node[i].c;
}
}
}

bool cmp(Node a,Node b)
{
return a.c<b.c;
}

int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
sum=0;
int i,j;
for(i=0;i<MAX;i++)
parent[i]=i;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
scanf("%d",&num[i][j]);
}
}
temp=0;
for(i=0;i<n;i++)
{
for(j=0;j<n;j++)
{
node[++temp].a=i;
node[temp].b=j;
node[temp].c=num[i][j];
}
}
sort(node,node+temp,cmp);
Kruskal();
printf("%d\n",sum);
}
return 0;
}

最小生成树 --- 求最小权值、MST

时间: 2024-10-03 12:34:03

最小生成树 --- 求最小权值、MST的相关文章

POJ 2195 Going Home(BFS+KM求最小权值)

Description: On a grid map there are n little men and n houses. In each unit time, every little man can move one unit step, either horizontally, or vertically, to an adjacent point. For each little man, you need to pay a $1 travel fee for every step

POJ-2195 Going Home---KM算法求最小权值匹配(存负边)

题目链接: https://vjudge.net/problem/POJ-2195 题目大意: 给定一个N*M的地图,地图上有若干个man和house,且man与house的数量一致.man每移动一格需花费$1(即单位费用=单位距离),一间house只能入住一个man.现在要求所有的man都入住house,求最小费用. 思路: KM算法传送门: 理解篇    运用篇 每个man和house建立带权二分图,曼哈顿距离就是边的值,这里要求最小费用,也就是二分图最小权值匹配,但是KM算法求的是二分图最

HDU 4738——Caocao&#39;s Bridges——————【求割边/桥的最小权值】

Caocao's Bridges Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Status Practice HDU 4738 Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. But he wouldn't give up. Caocao's army st

poj3565 Ants km算法求最小权完美匹配,浮点权值

/** 题目:poj3565 Ants km算法求最小权完美匹配,浮点权值. 链接:http://poj.org/problem?id=3565 题意:给定n个白点的二维坐标,n个黑点的二维坐标. 求是否存在n条边,每条边恰好连一个白点,一个黑点,且所有的边不相交. 输出所有黑点连接的白点编号. 思路:最小权完美匹配. 假定有白点1(a1,b1), 2(a2,b2), 黑点3(a3,b3),4(a4,b4); 如果1(a1,b1)与3(a3,b3)相连,2(a2,b2)与4(a4,b4)相连,如

[HDU4738]Caocao&#39;s Bridges(求割边最小权值)

http://acm.hdu.edu.cn/showproblem.php?pid=4738 #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn=1e3+4,maxm=1e6+4; int dfn[maxn],low[maxn],head[maxn]; int n,m,cnt=0,rt,ans

POJ 2253 Frogger(Dijkstra变形——最短路径最小权值)

题目链接: http://poj.org/problem?id=2253 Description Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists' suns

HDU 3488--Tour【最小费用最大流 &amp;&amp; 有向环最小权值覆盖 &amp;&amp; 经典】

Tour Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others) Total Submission(s): 2308    Accepted Submission(s): 1156 Problem Description In the kingdom of Henryy, there are N (2 <= N <= 200) cities, with M (M <= 3000

【POJ 2400】 Supervisor, Supervisee(KM求最小权匹配)

[POJ 2400] Supervisor, Supervisee(KM求最小权匹配) Supervisor, Supervisee Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 2538   Accepted: 719 Description Suppose some supervisors each get to hire a new person for their department. There are N

【POJ 2195】 Going Home(KM算法求最小权匹配)

[POJ 2195] Going Home(KM算法求最小权匹配) Going Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 20303   Accepted: 10297 Description On a grid map there are n little men and n houses. In each unit time, every little man can move one unit ste