【PAT】1034. Head of a Gang (30)
来源:互联网 发布:工信部网站域名查询 编辑:程序博客网 时间:2024/05/01 10:51
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 59AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10Sample Output 1:
2AAA 3GGG 3Sample Input 2:
8 70AAA BBB 10BBB AAA 20AAA CCC 40DDD EEE 5EEE DDD 70FFF GGG 30GGG HHH 20HHH FFF 10Sample Output 2:
0
分析:题目写的不够清楚。
什么是gang?
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.
(1)成员数据大于2 (2)相互间的通话时间总和大于K,例如:
AAA BBB 10BBB AAA 20AAA CCC 40则她们相互间的通话时间为10 + 20 + 40 。
以下代码有两组数据错误,得分为22,待改进:
#include <iostream>#include <vector>#include <queue>#include <map>#include <algorithm>using namespace std;#define INF 9999int N,K,index;vector<vector<int> > cost;vector<bool> used;vector<int> weight; map<string,int> name2id;map<int, string> id2name;pair<int,int> bfs(int s){queue<int> que;que.push(s);int total=0, max=weight[s], maxIndex=s, cnt=0;while(!que.empty()){int t = que.front();que.pop();cnt++;total += weight[t]; if(weight[t]>max){max = weight[t];maxIndex = t;}used[t] = true;for(int i=0; i<index; i++){ if( (!used[i])&&(cost[t][i]!=INF) ){used[i] = true; que.push(i);}}}if(total>K*2 && cnt>2)return make_pair(maxIndex,cnt);elsereturn make_pair(-1,-1);}int main(){scanf("%d%d",&N,&K);int i,time;string stra, strb;int a, b;cost.resize(2*N,vector<int>(2*N,INF));weight.resize(2*N,0);index=0;for(i=0; i<N; i++){cin>>stra>>strb>>time;if(name2id.find(stra)!=name2id.end()){a = name2id[stra]; }else{a = index;name2id.insert(make_pair(stra,index));id2name.insert(make_pair(index,stra));index++;}if(name2id.find(strb)!=name2id.end()){b = name2id[strb]; }else{b = index;name2id.insert(make_pair(strb,index));id2name.insert(make_pair(index,strb));index++;}if(cost[a][b]==INF){cost[a][b] = 0;cost[b][a] = 0;} cost[a][b] += time;cost[b][a] += time;weight[a] += time;weight[b] += time;}used.resize(index-1,false);vector<pair<int,int> > result;int max = 0;for(i=0; i<index; i++){if(!used[i]){pair<int, int> p;p = bfs(i);if(p.first==-1){continue;}if(p.second==max){result.push_back(p);}else if(p.second>max){result.clear();result.push_back(p);max = p.second;}}}printf("%d\n",result.size());for(i=0; i<result.size(); i++){cout<<id2name[result[i].first]<<" "<<result[i].second<<endl;}return 0;}
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- pat 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- PAT 1034. Head of a Gang (30)
- 【PAT】1034. Head of a Gang (30)
- PAT A 1034. Head of a Gang (30)
- PAT-A-1034. Head of a Gang (30)
- PAT 1034. Head of a Gang
- PAT 1034. Head of a Gang
- PAT 1034. Head of a Gang
- PAT 1034. Head of a Gang
- 【PAT】1034. Head of a Gang
- PAT A1034. Head of a Gang (30)
- pat-a1034. Head of a Gang (30)
- PAT (Advanced) 1034. Head of a Gang (30)
- 【PAT甲级】1034. Head of a Gang (30)
- 1034. Head of a Gang (30) PAT 甲级
- Cocos2dx3.x版本在部分安卓手机(某X为居多)精灵渲染不出问题
- Matlab策略
- JAVA-class对象与实例对象
- jquery思维定势的坑
- Python 读入windows 的记事本内容 编码 类别(ANSI,utf-8,Unicode)
- 【PAT】1034. Head of a Gang (30)
- Java并发与线程相关资源汇总
- Oracle取余函数mod
- 深入理解javascript的原型与闭包
- iOS view的指定角设置圆弧
- 如何解决Android 5.0中出现的警告:Service Intent must be explicit
- OGRE SampleBrowser框架解析
- checkbox框的各种状态选择
- android弹出窗口实现