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
思路与注意点
-
题目给出的输入是无向图边的个数(通话个数),每个满足条件连通图边权之和的最小权值。(也就是犯罪团伙总通话最小时间),以及每条边的结点及对应权值。
如AAA BBB value 指的是AAA 打给BBB的电话时间。 -
我们需要在这些通话时间大于K值的犯罪团伙,并输出犯罪团伙中通话时间最长人的名字以及犯罪团伙总人数(连通块中结点个数)
需要注意的点
- 在输入的过程中记录每一个人的通话时间也就是点权。
- 输入的过程中累加相同结点的双向通话时间,如AAA->BBB和 BBB-> AAA的时间。
- 用计数器来累加结点的编号,做名字和编号的映射转换
- 结点个数最多为2*N
- dfs遍历 遍历每条边而非每个结点
图的dfs访问可以把连通块的信息记录下来,但是dfs特点是每个结点只访问一次,而我们需要的是访问所有的边,未防止漏解(在含有环的时候会出现漏接 如第一个样例F->G->H->F)需要在先累加边权在去判断vis是否为0然后进行递归访问下一节点。同时为了防止重复累加边权,再没每访问一条边后需要对边权清零,下一次访问这条无向边的时候对累加总边权就没有影响。
代码实现
#include<iostream>
#include<string>
#include<map>
using namespace std;
const int maxn=2010; //数据范围
int nodes[maxn][maxn]={0}; //存储整个图
bool vis[maxn];
map<int,int> connect;//连通块
map<string,int> Gang;//团伙 head的名字 -> 团伙中包含的人数
map<string ,int> stringToInt;
map<int,string> intToString;
int weight[maxn]={0};
int numPerson,n,k;//顶点数,边数,边权和下限
void dfs(int nowvist,int &cnum,int & head,int & totalvalue){ //要加引用 不然不改变值
cnum++;
vis[nowvist]=1;
if(weight[nowvist]>weight[head]){
head=nowvist;
}
for(int i=0;i<numPerson;i++){
if(nodes[nowvist][i]>0){
totalvalue+=nodes[nowvist][i];
nodes[nowvist][i]=nodes[i][nowvist]=0;
if(vis[i]==false){
dfs(i,cnum,head,totalvalue);
}
}
}
}//访问单个连通块
void DFSTrave(){
for(int i=0;i<numPerson;i++){
if(vis[i]) continue;
int cnum=0,head=i,totalvalue=0;
dfs(i,cnum,head,totalvalue);
// cout<<"connnect Gang nowvist"<<nowvist<<"连通块结点数"<<cnum<<"head:"<<head<<"totalvalue"<<totalvalue<<endl;
if(cnum>2&&totalvalue>k){
Gang[intToString[head]]=cnum;
}
}
}
int change(string str){
if(stringToInt.find(str)!=stringToInt.end()){
return stringToInt[str];
}else{
stringToInt[str]=numPerson;
intToString[numPerson]=str;
return numPerson++;
}
}
int main(){
int w;
string str1,str2;
freopen("test.txt","r",stdin);
cin>>n>>k;
for(int i=0;i<n;i++){
cin>>str1>>str2>>w;
int id1=change(str1);
int id2=change(str2);
weight[id1]+=w;
weight[id2]+=w; //点权
nodes[id1][id2]+=w; //更新边权
nodes[id2][id1]+=w;
}
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;
}