1034. Head of a Gang (30)

原创 2015年07月08日 21:10:07

顺序查找一次的话测试点4出错,Gang中有新加入的成员就要再搜索一遍。

代码如下。还可以用并查集的思想,代码就不写了。

#define  _CRT_SECURE_NO_WARNINGS 

#include<string>
#include<string.h>
#include<vector>
#include<map>
#include<stack>
#include<iostream> 
#include<algorithm>
#include<stdio.h> 
using namespace std;
/*
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
*/
struct InPut
{
char name1[4], name2[4];
int Time;
};
struct OutPut
{
char name[4];
int num;
};
struct Member
{
char name[4];
int weight;
};
bool CheckName(vector < Member > &Gang, char *name1, char *name2, int iTime)
{
bool bFind1 = false, bFind2 = false;
vector<Member>::iterator it1 = Gang.begin();
vector<Member>::iterator it2 = Gang.begin();
for (; it1 != Gang.end(); it1++)
{
if (strcmp(name1, it1->name) == 0)
{
bFind1 = true;
break;
}
}
for (; it2 != Gang.end(); it2++)
{
if (strcmp(name2, it2->name) == 0)
{
bFind2 = true;
break;
}
}
if (bFind1&&bFind2)
{
it1->weight += iTime;
it2->weight += iTime;
return true;
}
else if (!bFind1&&bFind2)
{
it2->weight += iTime;
Member mTmp;
strcpy(mTmp.name, name1);
mTmp.weight = iTime;
Gang.push_back(mTmp);
return true;
}
else if (bFind1&&!bFind2)
{
it1->weight += iTime;
Member mTmp;
strcpy(mTmp.name, name2);
mTmp.weight = iTime;
Gang.push_back(mTmp);
return true;
}
return false;
}
bool cmp_mem(Member a,Member c)
{
return a.weight > c.weight;
}
bool cmp_out(OutPut a, OutPut c)
{
return strcmp(a.name, c.name) < 0;
}
bool cmp_name(InPut a, InPut c)
{
return strcmp(a.name1, c.name1) < 0;
}
int main()
{
vector<OutPut> out; 
int out_num = 0;
int N, K;
cin >> N >> K;
vector <InPut> input;
for (int i = 0; i < N; i++)
{
InPut tmp;
scanf("%s %s %d",tmp.name1,tmp.name2,&tmp.Time);
input.push_back(tmp);

sort(input.begin(), input.end(), cmp_name);
while (input.size()>0)
{
int tmp_k = 0;
vector < Member > Gang;
vector<InPut>::iterator it = input.begin();  
tmp_k += it->Time;
Member a, b;
strcpy(a.name, it->name1); strcpy(b.name, it->name2);
a.weight = it->Time; b.weight = it->Time;
Gang.push_back(a); Gang.push_back(b);


it=input.erase(it);
bool chk = false;
do
{
chk = false;
while (it != input.end())
{
//it中的name在Gang中是否出现过
if (CheckName(Gang, it->name1, it->name2, it->Time))
{
chk = true;
tmp_k += it->Time;
//删除这个relation
it = input.erase(it);
}
else
{
it++;
}
}
it = input.begin();
} while (chk);

if (Gang.size() <= 2 || tmp_k<=K)
continue;
sort(Gang.begin(),Gang.end(),cmp_mem);
OutPut out_tmp;
strcpy(out_tmp.name, Gang.begin()->name);
out_tmp.num = Gang.size();
out.push_back(out_tmp);
}
sort(out.begin(),out.end(),cmp_out);
cout << out.size() << endl;
for (int i = 0; i < out.size(); i++)
cout << out[i].name << " " << out[i].num<<endl;
return 0;

相关文章推荐

1034. Head of a Gang (30)

考察并查集,以及每个集合的信息记录 #include #include #include #include #include #include using namespace std; ...

1034. Head of a Gang (30) -string离散化 -map应用 -并查集

在输入姓名时,由于无法判断人数,因此无法立即生成对应关系表,因此在输入时把通话记录暂存在Call结构体中,同时把每个名字都压入set。 在输入结束后,通过set即可判断人数,这时候利用迭代器从头到尾遍...

PAT (Advanced Level) 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 pho...

PAT (Advanced Level) Practise 1034 Head of a Gang (30)

1034. Head of a Gang (30) 时间限制 100 ms 内存限制 65536 kB 代码长度限制 16000 B...

【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 pho...

PAT 1034. Head of a Gang (30)

//1034. Head of a Gang (30) //#include "stdafx.h" //case 3 and case 5一直无法通过:主要是没有对结果进行排序 //比较简单的df...
  • xsloop
  • xsloop
  • 2015年03月14日 11:29
  • 414

【PAT甲级】1034. Head of a Gang (30)

注: Gang的成员数要达到两人以上。#include #include #include #include #include #include #include using names...

PAT A1034. 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 pho...
  • jolivan
  • jolivan
  • 2017年02月26日 22:56
  • 62

1034. Head of a Gang (30)解题报告

#define _CRT_SECURE_NO_WARNINGS #include #include #include #include #include #include #include...
  • chr1991
  • chr1991
  • 2016年10月23日 17:26
  • 44

PAT 1034 Head of a Gang(图遍历+BFS)

题目https://www.patest.cn/contests/pat-a-practise/1034 题意:给出1000条以内的通话记录A B和权值w,如果一个团伙人数超过2人并且通话总权值超...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:1034. Head of a Gang (30)
举报原因:
原因补充:

(最多只允许输入30个字)