# PAT 甲级 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 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
//问题1：这里的做法是不正确的，虽然G[a][b] 和G[b][a]是不一样的，但为了求所有通话时间，应G[a][b] G[b][a]的通话时间求和，作为(a,b)边的真正权值，陷入了思维定势 G[a][b]=G[b][a] =l
//问题2：字符串和下标的对应，本来想耍个小聪明，用字符串首位字母-'A' 作为字符串的下标，其实麻烦了，直接设置两个map用来字符串和下标，下标和字符串的对应。有时侯一个问题偷懒了别的就会成倍复杂
//问题3： 连通分量的数目，这里按照算法笔记的做法，先在dfstrav()函数中对图进行深度搜索，如果在从一个点 i 深度搜索图后仍发现有结点未被访问，就使得cnt加1.但更方便的做法是由输出结果的性质入手，将每次搜索得到的头目信息存放在map中，这样连通分量的数目就是这个map的数目，是 顺手的事情
//还有一个未弄懂的在通过的代码上说
#include <bits/stdc++.h>
#define maxn 2500

using namespace std;

int N;
int K;

bool visited[maxn];
int G[maxn][maxn];
int w[maxn];
int weight_all = 0;
int number_all = 0;
int max_t = 0;
int max_index = 0;

map<string,int> m;

void dfs(int u)
{
visited[u] = true;
if(w[u]>max_t)
{
max_t = w[u];
max_index = u;
}

weight_all+=w[u];
number_all++;
for(int i = 0;i<N;i++)
{
if(visited[i]==false&&G[u][i]!=0)
{
dfs(i);   // 问题4
}
}
}

void dfstra()
{
int cnt = 0;
for(int i = 0;i<N;i++)
{
weight_all = 0;
number_all = 0;
max_t = 0;
max_index = 0;
if(visited[i]==false)
{
dfs(i);
if(number_all>2&&(weight_all/2)>K)
{
cnt++;    // 问题3
//cout<<max_index<<" "<<number_all<<endl;
string name = "";
for(int j = 0;j<3;j++)
{
name+=(char)('A' + max_index);   // 同问题2
}
m[name] = number_all;
}
}
}
if(cnt==0)
{
cout<<0<<endl;
}
else
{
cout<<cnt<<endl;
}
}

int main()
{
cin>>N>>K;
fill(visited,visited+maxn,false);
fill(G[0],G[0]+maxn*maxn,0);
fill(w,w+maxn,0);

string n_a,n_b;
int t;

for(int i = 0;i<N;i++)
{
cin>>n_a>>n_b>>t;
int a = (int)(n_a[0]-'A');   //问题2
int b = (int)(n_b[0]-'A');
G[a][b] = t;    // 问题1
w[a]+=t;
w[b]+=t;
}
dfstra();
for(auto it = m.begin();it!=m.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}


通过的代码：

// 问题5 把和现在点相关的点的权值全部相加求和，下一行的G[u][i] = G[i][u] = 0防止回头
#include<bits/stdc++.h>
#define maxn 2500

using namespace std;

int N,K;
int num = 0;                       //代表的是每个帮派的人数
map<int,string> int_to_string;
map<string,int> string_to_int;
map<string,int> gang;
bool visited[maxn] = {false};
int G[maxn][maxn];
int w[maxn] = {0};
int head = 0,num_all = 0,weight_all = 0;
// change 返回姓名str对应的编号,这里是这题没有设计好的
int change(string a)
{
if(string_to_int.find(a)!=string_to_int.end())
{
return string_to_int[a];    // 返回编号
}
else
{
string_to_int[a] = num;
int_to_string[num] = a;
return num++;
}
}

void dfs(int u,int& head,int& num_all,int& weight_all)
{
num_all++;
visited[u] = true;
{
}
for(int i = 0;i<N;i++)
{
if(G[u][i]>0)
{
weight_all+=G[u][i];     // 问题5
G[u][i] = G[i][u] = 0;
if(visited[i]==false)
{
}
}
}
}

void dfstra()
{

for(int i = 0;i<N;i++)
{
if(visited[i]==false)
{
num_all = weight_all = 0;
if(num_all>2&&weight_all>K)
{
}
}
}
}

int main()
{
fill(G[0],G[0]+maxn*maxn,0);
cin>>N>>K;
string n_a,n_b;
int t;
for(int i = 0;i<N;i++)
{
cin>>n_a>>n_b>>t;
int a = change(n_a);
int b = change(n_b);
G[a][b] +=t;
G[b][a] +=t;
w[a]+=t;
w[b]+=t;
}
dfstra();
cout<<gang.size()<<endl;
for(auto it = gang.begin();it!=gang.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}


• 点赞
• 评论
• 分享
x

海报分享

扫一扫，分享海报

• 收藏
• 打赏

打赏

时间领主大锤

你的鼓励将是我创作的最大动力

C币 余额
2C币 4C币 6C币 10C币 20C币 50C币
• 举报
• 一键三连

点赞Mark关注该博主, 随时了解TA的最新博文
08-21 245
09-02 1075

08-01 104
04-27 40