poj---Relative Relatives

来源:互联网 发布:09电竞平台 知乎 编辑:程序博客网 时间:2024/05/03 13:11

一道基础的数据结构水题,但是还是不能一y。

总结:调用STL还是不熟练,map.count( string);一种形式。

           使用 memset时有错误,把结构体string初始化时运行错误,修改一下就行了。根据自己试验:

           memse不能被 string 使用,char字符串和数组可以使用。string使用时会运行错误。

           char使用memset 赋值 0 时相当于内存全部赋值为 ‘\0’ 。


http://poj.org/problem?id=2021
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

<span style="font-family:Arial;">/*****21Ted Bill 254Ray James 40James Beelzebub 17Ray Mark 75Ted Ray 20DATASET 1Bill 75DATASET 2Ray 80James 40Beelzebub 23Mark 5********/#include <iostream>#include<map>#include<stdio.h>#include<string.h>#include<queue>#include<algorithm>using namespace std;map<string,int>Mp;int store[150][150],A[150];struct message{    int old,year;    string name;};bool cmp(message a,message b){    if(a.old==b.old)        return a.name<b.name;    return a.old>b.old;}int main(){    int T;    cin>>T;    for(int t=1;t<=T;t++)    {        message pep[100];        memset(store,0,sizeof(store));        memset(A,0,sizeof(A));        Mp.clear();        int n,j=1,f;        scanf("%d",&n);        for(int i=1;i<=n;i++)        {            string str1,str2;            int l;            cin>>str1>>str2>>l;            if(Mp.count(str1)==0)            {                Mp[str1]=j;                pep[j].name=str1;                j+=1;            }            if(Mp.count(str2)==0)            {                Mp[str2]=j;                pep[j].name=str2;                j+=1;            }            int v,u;            v=Mp[str1]; u=Mp[str2];            A[u]=A[u]+1;            store[v][u]=l;        }        for(int i=1;i<j;i++)        {            if(A[i]==0)            {                f=i;            }        }        pep[f].old=100;        pep[f].year=1;        queue<int>q;        q.push(f);        while(!q.empty())        {            f=q.front();            q.pop();            for(int i=1;i<j;i++)            {                if(store[f][i])                {                    pep[i].old=pep[f].old-store[f][i];                    pep[i].year=pep[f].year+1;                    q.push(i);                }            }        }        sort(pep+1,pep+j,cmp);        cout<<"DATASET "<<t<<endl;        for(int i=2;i<j;i++)            cout<<pep[i].name<<" "<<pep[i].old<<endl;    }    return 0;}</span>

队友简单代码:

<span style="font-family:Arial;">#include <iostream>#include <stdio.h>#include <string.h>#include<algorithm>#include <queue>using namespace std;struct node{    char fa[100];    char ch[100];    int age;    int t;} u[105],v;int cmp(node a,node b){    if(a.t>b.t)        return 1;    else if(a.t==b.t)    {        if(strcmp(a.ch,b.ch)<0)            return 1;    }    return 0;}int main(){    int T;    scanf("%d",&T);    int DATASET=1;    while(T--)    {        queue<node>q;        int i,n,k;        scanf("%d",&n);        for(i=0; i<n; i++)        {            scanf("%s %s %d",u[i].fa,u[i].ch,&u[i].age);            if(strcmp(u[i].fa,"Ted")==0) k=i;        }        for(i=0; i<n; i++)        {            u[i].t=0;        }    u[k].t=100-u[k].age;        q.push(u[k]);        //char s[10];        //int l;        for(i=0;i<n;i++){        if(strcmp(u[i].fa,"Ted")==0){        u[i].t=100-u[i].age;        q.push(u[i]);        }        }        while(!q.empty())        {            v=q.front();            q.pop();            for(i=0; i<n; i++)            {            //cout<<u[i].fa<<" "<<v.fa<<endl;                 if(strcmp(u[i].fa,v.ch)==0)                {                u[i].t=v.t-u[i].age;                    q.push(u[i]);                                }            }        }        sort(u,u+n,cmp);        printf("DATASET %d\n",DATASET++);        for(i=0; i<n; i++) printf("%s %d\n",u[i].ch,u[i].t);        //printf("\n");    }    return 0;}</span>


1 0
原创粉丝点击