暑期刷题系列之PAT A1034

  刷题过程中第二道个人认为值得被纪念的题-PAT A1034。这题的核心思想并不难,就是考察图的遍历。但本人在做这题的时候,并且最后只过了两个测试点,后来看了晴神笔记之后才AC。本题算法并不难,我觉得难的是对数据及结果的处理,以及如果会使用map的话,本题难度会降低很多。

以下是原题:


1034 Head of a Gang (30)(30 分)

One way that the police finds the head of a gang is to check people's phone calls. If there is a phone call between A and B, we say that A and B is related. The weight of a relation is defined to be the total time length of all the phone calls made between the two persons. A "Gang" is a cluster of more than 2 persons who are related to each other with total relation weight being greater than a given threshold K. In each gang, the one with maximum total weight is the head. Now given a list of phone calls, you are supposed to find the gangs and the heads.

Input Specification:

Each input file contains one test case. For each case, the first line contains two positive numbers N and K (both less than or equal to 1000), the number of phone calls and the weight threshold, respectively. Then N lines follow, each in the following format:

Name1 Name2 Time

where Name1 and Name2 are the names of people at the two ends of the call, and Time is the length of the call. A name is a string of three capital letters chosen from A-Z. A time length is a positive integer which is no more than 1000 minutes.

Output Specification:

For each test case, first print in a line the total number of gangs. Then for each gang, print in a line the name of the head and the total number of the members. It is guaranteed that the head is unique for each gang. The output must be sorted according to the alphabetical order of the names of the heads.

Sample Input 1:

8 59
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 1:

2
AAA 3
GGG 3

Sample Input 2:

8 70
AAA BBB 10
BBB AAA 20
AAA CCC 40
DDD EEE 5
EEE DDD 70
FFF GGG 30
GGG HHH 20
HHH FFF 10

Sample Output 2:

0

题目大意:一个无向图,找出其中的各个连通图,并且各个连通图的结点个数必须大于2并且总权重大于K,然后确定各个连通图中的头结点(头结点即该店连接的所有边的权重之和最大的那个),最后按字典序输出各个头结点的名称以及头结点所在连通图的结点数。

思路:思路很简单,我用的是DFS。

注意:虽然思路很简单,但有几个关键点我觉得还是值得留意的。

1、因为结点名称是三个大写字母,所以我们可以采用hash的方法。但其实如果我们熟悉STL中的map的运用的话,可以设置两个map:intToString以及stringToInt来操作,这样会方便的多,然后由于map默认是根据key的值按字典序排序的,所以我们可以将最终结果保存在一个map<string,int>中其中string是头结点的名称,int是连通图的结点数,然后直接输出即可(具体看代码)。

2、当我们在DFS的时候,我们不能先对邻接点判断是否已经访问过,因为我们需要计算连通图的总权重,所以应该先对所连接的边进行判断,因为可能会有环的出现,所以需要先将边的权重加到总权重上。之后记的将边的权重设置为0,防止重复累加。

以下是代码:

#include <iostream>
#include <map>
#include <string>
#define MAXN 2005
using namespace std;


int N, K, cou = 0;                             //cou来记录结点的总数
int G[MAXN][MAXN] = { 0 };                     //使用邻接矩阵来存储图
int weight[MAXN] = { 0 };
bool visit[MAXN] = { false };
map<int, string> intToString;                  //注意此处的设置可不用使用hash
map<string, int> stringToInt;
map<string, int> gang;

int convert(string str) {                                 //设置结点名称string类型与int类型的对应
	if (stringToInt.find(str) != stringToInt.end()) {
		return stringToInt[str];
	}
	else {
		intToString[cou] = str;
		stringToInt[str] = cou;
		cou++;
	}
	return stringToInt[str];
}

void DFS(int now, int &head, int &numMember, int &totalWeight) {   //注意此处为引用
	visit[now] = true;
	numMember++;
	if (weight[now] > weight[head]) {
		head = now;
	}
	for (int i = 0; i < cou; i++) {
		if (G[now][i] > 0) {                      //因为可能有环的出现,以免漏掉某条边,故先考虑是否有边来累加权重
			totalWeight += G[now][i];             
			G[now][i] = G[i][now] = 0;            //累加后需要将权重设为0防止重复累加   
			if (!visit[i]) {
				DFS(i, head, numMember, totalWeight);
			}
		}
	}
	return;
}

void solve() {
	int head;
	int numMember, totalWeight;
	for (int i = 0; i < cou; i++) {
		if (!visit[i]) {
			head = i;
			numMember = totalWeight = 0;
			DFS(i, head, numMember, totalWeight);
		}
		if (numMember > 2 && totalWeight > K) {
			gang[intToString[head]] = numMember;
		}
	}
	return;
}

int main() {
	string str_1, str_2;
	int w, v_1, v_2;
	cin >> N >> K;
	for (int i = 0; i < N; i++) {
		cin >> str_1 >> str_2 >> w;
		v_1 = convert(str_1);
		v_2 = convert(str_2);
		weight[v_1] += w;
		weight[v_2] += w;
		G[v_1][v_2] += w;
		G[v_2][v_1] += w;
	}
	solve();
	cout << gang.size() << endl;
	map<string, int>::iterator it;
	for (it = gang.begin(); it!=gang.end(); it++) {
		cout << it->first << " " << it->second << endl;
	}
	return 0;
}

 

这题的核心算法并不难,但是细节还是比较值得回味,包括对STL中map的灵活使用以及在DFS的过程中可直接找出符合条件的连通图等方面。

 

 

 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值