程序设计基础81 图之无向图防止回头和访问通向已访问结点的路径

1034 Head of a Gang (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 threthold 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

一,关注点

1,本题是无向图,注意在遍历完路径时要删除回头的结点。

2,注意本题要在判断完如今结点通向任意结点加完距离后再判断这个任意结点是否该访问。而不是判断这个任意节点是否已访问且是否可通向再累加。不要和最短路径混了,否则会落下路径。

二,细节

1,本题数组和邻接矩阵都是以2000开头的。

2,使用memset必须添加string.h

扫描二维码关注公众号,回复: 4421212 查看本文章

三,我的代码

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<vector>
#include<map>
using namespace std;
const int max_n = 2020;
//const int co_max_n = 2020;
const int INF = 10000;
int N = 0, K = 0;
int co_index = 0;
int flag[max_n];
int G[max_n][max_n];
int PersonTimes[max_n];
struct Node {
	int id, num;
}node;
vector<Node> ans;
map<int, string> num_to_str;
map<string, int> str_to_num;
void convertion(string str) {
	if (str_to_num.count(str) == 0) {
		str_to_num[str] = co_index;
		num_to_str[co_index] = str;
		co_index++;
	}
}
bool cmp(Node a, Node b) {
	return num_to_str[a.id] < num_to_str[b.id];
}
void DFS(int nowPerson, int &nowTotalTime,int &nowMaxPerson,int &nowPersonNums) {
	flag[nowPerson] = 1;
	nowPersonNums++;
	if (PersonTimes[nowPerson] > PersonTimes[nowMaxPerson]) nowMaxPerson = nowPerson;
	for (int i = 0; i < N * 2; i++) {
		if (G[nowPerson][i] != 0) {
			nowTotalTime += G[nowPerson][i];
			G[i][nowPerson] = 0;
			if (flag[i] == false) {
				DFS(i, nowTotalTime, nowMaxPerson, nowPersonNums);
			}
		}
	}
}
int main() {
	string str_1, str_2;
	int times = 0;
	int TotalTime = 0, MaxPerson = 0, PersonNums = 0;
	memset(PersonTimes, 0, sizeof(PersonTimes));
	memset(flag, 0, sizeof(flag));
	memset(G, 0, sizeof(G));
	scanf("%d %d", &N, &K);
	for (int i = 0; i < N; i++) {
		cin >> str_1 >> str_2 >> times;
		convertion(str_1);
		convertion(str_2);
		G[str_to_num[str_1]][str_to_num[str_2]] += times;
		G[str_to_num[str_2]][str_to_num[str_1]] += times;
		PersonTimes[str_to_num[str_1]] += times;
		PersonTimes[str_to_num[str_2]] += times;
	}
	for (int i = 0; i < N * 2; i++) {
		TotalTime = 0, MaxPerson = i, PersonNums = 0;
		if (flag[i] == 0) {
			DFS(i, TotalTime, MaxPerson, PersonNums);
			if (TotalTime > K&&PersonNums > 2) {
				node.id = MaxPerson;
				node.num = PersonNums;
				ans.push_back(node);
			}
		}
	}
	sort(ans.begin(), ans.end(), cmp);
	printf("%d\n", ans.size());
	string name;
	for (int i = 0; i < ans.size(); i++) {
		name = num_to_str[ans[i].id];
		cout << name << " " << ans[i].num << endl;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/qq2285580599/article/details/84890071