现在的位置: 首页 > 综合 > 正文

POJ 2021 BFS+大量STL。。

2017年11月22日 ⁄ 综合 ⁄ 共 3326字 ⁄ 字号 评论关闭
Relative Relatives
Time Limit: 1000MS   Memory Limit: 30000K
Total Submissions: 2806   Accepted: 1210

Description

Today is Ted's 100th birthday. A few weeks ago, you were selected by the family to contact all of Ted's descendants and organize a surprise party. To make this task easier, you created an age-prioritized list of everyone descended
from Ted. Descendants of the same age are listed in dictionary order.

The only materials you had to aid you were birth certificates. Oddly enough, these birth certificates were not dated. They simply listed the father's name, the child's name, and the father's exact age when the baby was born.

Input

Input to this problem will begin with line containing a single integer n indicating the number of data sets. Each data set will be formatted according to the following description.

A single data set has 2 components:

  1. Descendant Count - A line containing a single integer X (where 0 < X < 100) indicating the number of Ted's descendants.
  2. Birth Certificate List - Data for X birth certificates, with one certificate's data per line. Each certificate's data will be of the format "FNAME CNAME FAGE" where:
    • FNAME is the father's name.
    • CNAME is the child's name.
    • FAGE is the integer age of the father on the date of CNAMEs birth.

Note:

  • Names are unique identifiers of individuals and contain no embedded white space.
  • All of Ted's descendants share Ted's birthday. Therefore, the age difference between any two is an integer number of years. (For those of you that are really picky, assume they were all born at the exact same hour, minute, second, etc... of their birth
    year.)
  • You have a birth certificate for all of Ted's descendants (a complete collection).

Output

For each data set, there will be X+1 lines of output. The first will read, "DATASET Y", where Y is 1 for the first data set, 2 for the second, etc. The subsequent X lines constitute your age-prioritized list of Ted's descendants
along with their ages using the format "NAME AGE". Descendants of the same age will be listed in dictionary order.

Sample Input

2
1
Ted Bill 25
4
Ray James 40
James Beelzebub 17
Ray Mark 75
Ted Ray 20

Sample Output

DATASET 1
Bill 75
DATASET 2
Ray 80
James 40
Beelzebub 23
Mark 5

Source

 
简单说下题意:(每次说这句话的时候就想起了我的英语四级。。泪奔~~)
一个叫Ted的人,现在100岁了,然后他有很多后裔,某一天,你要把这些后裔排个序。。
现在我们不知道每个人的具体年龄,但是知道每个人跟他父亲的年龄差,现在要求我们按年龄排序,年龄一样的按名字的字典序排序。。
 
思路:BFS+大量STL。。
#pragma warning(disable:4786)
#include<stdio.h>
#include<string.h>
#include<string>
#include<map>
#include<queue>
#include<vector>

using namespace std;

struct node
{
	char name[30];
	int age;
};

struct que
{
	char name1[30];
	char name2[30];
	int age;
};

struct math
{
	int v;
	int dis;
};
que q[300];
node person[300];
map<string,int>my;
map<string,bool>kk;
vector<math>bing[300];

void bfs(int root)
{
	int i,now,v;
	queue<int>q;
	q.push(root);
	while(!q.empty())
	{
		now=q.front();
		q.pop();
		for(i=0;i<bing[now].size();i++)
		{
			v=bing[now][i].v;
			person[v].age=person[now].age-bing[now][i].dis;
			q.push(v);
		}
	}
}

bool cmp(node a,node b)
{
	if((a.age>b.age)||((a.age==b.age)&&(strcmp(a.name,b.name)<0)))
		return true;
	return false;
}

int main()
{
	int i,t,n,T,cc,root;
	scanf("%d",&T);
	for(t=1;t<=T;t++)
	{
		my.clear();
		kk.clear();
		cc=0;
		for(i=0;i<300;i++)
			bing[i].clear();
		scanf("%d",&n);
		for(i=1;i<=n;i++)
		{
			scanf("%s%s%d",q[i].name1,q[i].name2,&q[i].age);
			if(!kk[q[i].name1])
			{
				kk[q[i].name1]=true;
				strcpy(person[cc].name,q[i].name1);
				my[q[i].name1]=cc++;
			}
			if(!kk[q[i].name2])
			{
				kk[q[i].name2]=true;
				strcpy(person[cc].name,q[i].name2);
				my[q[i].name2]=cc++;
			}
			if(strcmp(q[i].name1,"Ted")==0)
			{
				root=my[q[i].name1];
				person[root].age=100;
			}
			math p;
			p.v=my[q[i].name2];
			p.dis=q[i].age;
			bing[my[q[i].name1]].push_back(p);
		}
		bfs(root);
		sort(person,person+cc,cmp);
		printf("DATASET %d/n",t);
		for(i=1;i<cc;i++)
			printf("%s %d/n",person[i].name,person[i].age);
	}
	return 0;
}

抱歉!评论已关闭.