(简单) 搜索 最短路 HOJ 1121 Erdos Numbers

Erdos Numbers

My Tags  (Edit)
Source : ACM ICPC Mid-Central European 2000
Time limit : 3 secMemory limit : 32 M

Submitted : 84, Accepted : 31

The Hungarian Paul Erdos (1913–1996, pronounced as “Ar-dish”) was not only one of the strangest mathematicians of the 20th century, he was also among the most famous ones. He kept on publishing widely circulated papers up to a very high age, and every mathematician having the honor of being a co-author to Erdos is well respected.

Not everybody got a chance to co-author a paper with Erdos, so many people were content if they managed to publish a paper with somebody who had published a paper with Erdos. This gave rise to the so-called Erdos numbers. An author who has jointly published with Erdos had Erdos number 1. An author who had not published with Erdos but with somebody with Erdos number 1 obtained Erdos number 2, and so on. Today, nearly everybody wants to know what Erdos number he or she has. Your task is to write a program that computes Erdos numbers for a given set of scientists.


Input

The input file contains a sequence of scenarios, each scenario consisting of a paper database and a list of names. A scenario begins with the line “p n”, where p and n are natural numbers with 1<=p<=32000;1<=n<=3000. Following this line are p lines containing descriptions of papers (this is the paper database). A paper is described by a line of the following form:

LastName1, FirstName1, LastName2, Firstname2, . . . : TitleOfThePaper

The names and the title may contain any ASCII characters between 32 and 126 except commas and colons. There will always be exactly one space character following each comma. The first name may be abbreviated, but the same name will always be written in the same way. In particular, Erdos’ name is always written as “Erdos, P.”. (Umlauts like ‘¨o’,‘¨a’,. . . are simply written as ‘o’,‘a’, . . . .)

Example:

Smith, M.N., Martin, G., Erdos, P.: Newtonian forms of prime factors matrices.

After the p papers follow n lines each containing exactly one name in the same format as in the paper database.

The line ‘0 0’ terminates the input.

No name will consist of more than 40 characters. No line in the input file contains more than 250 characters. In each scenario there will be at most 10 000 different authors.


Output

For every scenario first print the number of the scenario in the format shown in the sample output. Then print for every author name in the list of names their Erdos number based on the papers in the paper database of the scenario. The authors should be output in the order given in the input file. Authors that do not have any relation to Erdos via the papers have Erdos number infinity. Adhere to the format shown in the sample output.

Print a blank line after each case.


Sample Input

2 2
Smith, M.N., Martin, G., Erdos, P.: Newtonian forms of prime factors matrices.
Gardner, M., Martin, G.: Commuting Names
Smith, M.N.
Gardner, M.
0 0


Sample Output

Database #1
Smith, M.N.: 1
Gardner, M.: 2 

题意:有一些人发论文,如果与Erdos一起发表同一个论文,那么这些人的number为1,如果又有人发的论文与Erdos不是同批的,如果该批中有人是和Erdos发表论文是同批的那么这个人的number2 以此类推。

思路: 就是求最短路吧,起点是Erdos, 其他全部人是终点。这题字符串处理比较麻烦,然后要注意的是在询问的时候输入的名字可能在之前是没有出现过的!


代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<string>
#include<deque>
#include<queue>
#include<math.h>
#include<vector>
#include<map>
#include<stack>
#include<set>
using namespace std;
#define MAX 10000+10
#define MOD 99997
const int inf = 0xfffffff;


map<string,int> table;
vector<int> G[MAX];
int d[MAX];
bool inq[MAX];
int P,N;
void build_graph()
{
table.clear();
for (int i = 0 ; i < MAX ; ++i)
G[i].clear();
int cnt = 0;
string buffer;
int s;
vector<int> g;
while (P--)
{
g.clear();
getline(cin,buffer);
string name = "";
s = cnt;
int len = buffer.length()-1;
while (buffer[len]!=':') --len;
int k = 0;
for (int i = 0 ; i < len ; ++i)
{
if (buffer[i]==',') ++k;
if (k!=2)
name += buffer[i];
else 
{
k = 0;
if (!table.count(name))
{
table[name] = cnt++;
g.push_back(cnt-1);
}
else 
g.push_back(table[name]);
name = "";
++i;
}
}
if (!table.count(name)) 
{
g.push_back(cnt);
table[name] = cnt++;
}
else 
g.push_back(table[name]);

for (int i = 0 ; i < g.size() ; ++i)
{
for (int j = 0 ; j < g.size() ; ++j)
{
if (g[i]!=g[j])
G[g[i]].push_back(g[j]);
}
}
}
}

void bfs()
{
memset(d,0,sizeof(d));
memset(inq,false,sizeof(inq));
int s = table["Erdos, P."];
for (int i = 0 ; i < table.size() ; ++i)
d[i] = inf;
queue<int> q;
q.push(s);
inq[s] = true;
d[s] = 0;
while (q.size())
{
int x = q.front();
q.pop();
for (int i = 0 ; i < G[x].size() ; ++i)
{
int y = G[x][i];
if (d[y]>d[x]+1)
{
d[y] = d[x]+1;
q.push(y);
}
}
}
string name;
while (N--)
{
getline(cin,name);
cout << name << ": ";
if (!table.count(name) || d[table[name]]==inf)
cout << "infinity" << endl;
else 
cout << d[table[name]] << endl;
}
printf("\n");
}


int main()
{
int cas = 0;
while (scanf("%d%d%*c",&P,&N))
{
if (N==0 && P==0) return 0;
++cas;
printf("Database #%d\n",cas);
build_graph();
bfs();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值