杭电ACM1301——Jungle Roads~~最小生成树

这题,简单的最小生成树问题。

只是输入的时候比较麻烦,一开始的N,是村庄的个数,下面N - 1 条信息,一开始的大写字母S和数K,是S村庄有K条路连接,后面是K个村庄以及权值。

处理好了输入的数据,就很简单了。

下面的是AC的代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

class data
{
public:
	int from, to, cost;
};

data Road[100];
int n, count1;
int par[30];

int cmp(data &a, data &b)             //权值从小到大排
{
	return a.cost < b.cost;
}

int finds(int x)                    //并查集查找函数
{
	if(par[x] == x)
		return x;
	else
		return par[x] = finds(par[x]);
}

void unite(int x, int y)            //合并函数
{
	x = finds(x);
	y = finds(y);
	if(x == y)
		return;
	else
		par[y] = x;
}

int same(int x, int y)               //判断是否属于同一个并查集
{
	return finds(x) == finds(y);
}

int solve()                         //构造最小生成树函数
{
	int res = 0;
	for(int i = 0; i < count1; i++)
	{
		data d = Road[i];
		if(!same(d.from, d.to))
		{
			unite(d.from, d.to);
			res += d.cost;
		}
	}
	return res;
}

int main()
{
//	freopen("data.txt", "r", stdin);
	int i, j, k, a, w;
	char s;
	while(scanf("%d", &n) != EOF && n)       //输入的处理
	{
		getchar();
		count1 = 0;
		for(i = 0; i < 30; i++)
			par[i] = i;
		for(i = 0; i < n - 1; i++)
		{
			scanf("%c%d", &s, &k);
			a = s - 'A';
			getchar();
			for(j = 0; j < k; j++)
			{
				scanf("%c%d", &s, &w);
				Road[count1].from = a; Road[count1].to = s - 'A'; Road[count1].cost = w;
				count1++;
				getchar();
			}
		}

		sort(Road, Road + count1, cmp);
		int ans = solve();
		printf("%d\n", ans);
	}
	return 0;
}
时间: 2024-08-29 03:28:20

杭电ACM1301——Jungle Roads~~最小生成树的相关文章

poj----(1251)Jungle Roads(最小生成树)

Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19265   Accepted: 8806 Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some

杭电ACM1025——Constructing Roads In JGShining&#39;s Kingdom

题目的意思是,两行城市,从左到右为1,2,3--n个城市.上面的下面的城市要与上面某个的城市相连,在不出现相交的情况下,最多可以连多少条. 知道了题意,就知道了应该用DP来做. 这一题,数据量比较大,用普通的DP,时间复杂度为N^2,会超时,我们应该用另一种DP方法,时间复杂度为nlogn. nlogn的DP的思路大概就是给你一个数,插到一个数组中,该位置已存在,就覆盖它.也就是可以用二分查找提高效率. 注意:输出的时候,要注意英语的单复数... 下面的是AC的代码: #include <ios

杭电 1025 Constructing Roads In JGShining&#39;s Kingdom(二分查找)

http://acm.hdu.edu.cn/showproblem.php?pid=1025 Constructing Roads In JGShining's Kingdom Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 15515    Accepted Submission(s): 4412 Problem Descriptio

hdu 301 Jungle Roads (最小生成树)

Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4073    Accepted Submission(s): 2970 Problem Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of

杭电3371--Connect the Cities(最小生成树)

题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 RT: 一直超时, 网上也找不到好方法, 不能忍的是同样的写法他们能过!! Prim() 998ms卡过去了, 严重怀疑杭电判题有问题: ac: #include <cstdio> #include <cstring> #include <iostream> using namespace std; const int INF = 0x3f3f3f3f; int map[

hdu1301 Jungle Roads 最小生成树

The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to mainta

hdu 1301 Jungle Roads 最小生成树

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly,

杭电1102 Constructing Roads

Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 13518    Accepted Submission(s): 5128 Problem Description There are N villages, which are numbered from 1 to N, and you should

NYOJ 434 &amp;&amp; POJ 1251 Jungle Roads(最小生成树)

链接:click here 题意: 题目大意在相通n个岛屿的所有桥都坏了,要重修,重修每一个桥所用的时间不同,求重修使每个岛屿都间接或直接与其他岛屿相同时所用的的最短时间(只有修完一个桥后才可修下一个桥).简言之就是求最小生成树. 对于数据,数据输入的第一行n代表岛屿的个数,当为0是结束程序,接着n-1行开始时为这岛屿的编号,用大写字母表示,接着是一个整数m,表示与该岛屿连接的字典序大于该岛屿编号的个数,然后该行输入m对数据,每对数据的第一个字母表示与该岛屿连通的岛屿的编号,第二个数字表示要重修