PAT 1034 map的再一次使用

题目

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 threthold, 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

大意 给出一系列的通话纪录,若两人之间有通话,则证明两人连通。人和人之间的边权为他们的通话时间,每人身上的点权为自己的总通话时间。给出的所有边的信息中会得到很多连通分量,要求找出边权和>k的连通分量,且连通分量中的节点个数>2。

代码和思路

  1. 传进来的都是字符串名字和边权,首先要处理的就是怎么讲字符串名字转化为下标 0 ~ X, 而且一开始我们是不知道传进来的名字的,所以说人员的信息对我们透明,所以直接使用map<string, int>,创建一个从名字到下标的映射。但为了最终的输出仍是名字,所以同时需要建立一个map<int, string>来保证还能通过下标找到名字。
  2. 整个题目是对连通块的DFS,但要注意的是,在标准DFS中,如果一个节点已经被访问,那么其他路径到达它的情况就不再考虑,标准判断中有*if (visit[v] != false)*这样一步,但在本题中需要进行变化。因为每条边的情况我们都需要记录,所以要先记录当前边,然后判断这个点是否被访问过,如果没有的话进行递归DFS。
  3. 记录边之后,因为我们是先记录边后判断是否被访问,所以说记录边之后,直接将这条边从图中抹掉,即将其边权归0,否则可能在内层DFS调用的时候会绕回来。
  4. 在DFS中即可纪录最大点权并进行更新。
  5. 当一个连通块访问结束后,立刻进行判断他们是不是Gang,是的话直接记录下来。
  6. 这里记录同样使用map,只不过是关键字变为了头目的名字和连通块节点数。map自身按照字典序排好了,输出的时候也方便
#include<map>
#include<cstdio>
#include<iostream>
using namespace std;
const int maxv = 2020;

map<string, int> StringToInt;
map<int, string> IntToString;
map<string, int> Gang;
int G[maxv][maxv];
int weight[maxv];
bool visit[maxv];

int n, k, numPerson = 0;

//对一个连通块进行DFS
void DFS(int nowvisit, int& numMember, int& head, int& times) {
	numMember++;
	visit[nowvisit] = true;
	if (weight[nowvisit] > weight[head]) {
		head = nowvisit;
	}
	for (int i = 0; i < numPerson; i++) {
		if (G[nowvisit][i] > 0) {
			times += G[nowvisit][i];
			G[nowvisit][i] = G[i][nowvisit] = 0;
			if (visit[i] == false) {
				DFS(i, numMember, head, times);
			}
		}
	}
}

//对所有联通块DFS
void DFSTrave() {
	for (int i = 0; i < numPerson; i++) {
		if (visit[i] == false) {
			//初始化DFS函数的参数信息
			int numMember = 0, head = i, times = 0;
			DFS(i, numMember, head, times);
			if (times > k && numMember > 2) {
				Gang[IntToString[head]] = numMember;
			}
		}
	}
}

//将str转换为下标,从0开始记录就好,这样同时得到了信息中总人员的个数
int StrToInt(string str) {
	if (StringToInt.find(str) != StringToInt.end()) {
		return StringToInt[str];
	}
	else {
		StringToInt[str] = numPerson;
		IntToString[numPerson] = str;
		return numPerson++;
	}
}

int main() {
	string str1, str2;
	int time;
	cin >> n >> k;
	for (int i = 0; i < n; i++) {
		cin >> str1 >> str2 >> time;
		int node1 = StrToInt(str1);
		int node2 = StrToInt(str2);
		weight[node1] += time;
		weight[node2] += time;
		G[node1][node2] += time;
		G[node2][node1] += time;
	}
	DFSTrave();
	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;
}

  1. 最后对于map的输出,使用迭代器it,这样同时输出了头目信息和人数信息,注意map的使用方法
  2. 录入边的时候要注意,我们DFS时按照无向边访问,但是读入的时候是有方向的,所以所有的权重都要加和。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值