1. 程式人生 > >程式設計基礎81 圖之無向圖防止回頭和訪問通向已訪問結點的路徑

程式設計基礎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

三,我的程式碼

#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;
}