ZOJ 3532 Kevin Bacon 最短路

Kevin Bacon

Time Limit: 1 Second       Memory Limit: 65536 KB

Kevin Norwood Bacon (born July 8, 1958) is an American film and theater actor whose notable roles include Animal House, Diner, Footloose, Flatliners, Wild Things, A Few Good Men, Apollo 13, Mystic River, The Woodsman, Trapped, Friday the 13th, Hollow Man, Tremors, Death Sentence, Frost/Nixon, Crazy, Stupid, Love and X-Men: First Class.

Bacon has won Golden Globe and Screen Actors Guild Awards, was nominated for an Emmy Award, and was named by The Guardian as one of the best actors never to have received an Academy Award nomination.

In 2003, Bacon received a star on the Hollywood Walk of Fame.

He was good at playing various roles, and every actor or actress having the honor of being a partner to Bacon is well respected.

Not everybody got a chance to be a partner with Bacon, so many people were content if they managed to play a movie with somebody who had played a movie with Bacon. This gave rise to the so-called Bacon numbers. Nowadays, an advanced Bacon numbers have been invented, it also refer to the box office, when box office is larger, the number k is smaller. For example, An actor who has play in a movie with Bacon, with the number about this movie is k, had advanced Bacon number k. An actor who had not played with Bacon but with somebody(the movie they played together has a number k2) with Bacon number k1 obtained Bacon number k1+k2, and so on. Today, nearly every actor wants to know what the smallest advanced Bacon number he or she has. Your task is to write a program that computes advanced Bacon numbers for a given set of actor.

Input

The input file contains a sequence of actors, each scenario consisting of a movie 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.1 <= k <= 100 000. Following this line are p lines containing descriptions of movies (this is the movie database). A movie is played by a line of the following form:

k LastName1, FirstName1, LastName2, Firstname2, . . . : TitleOfTheMovie

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.

Example:

John, Belushi, Karen, Allen, Kevin, Bacon: Animal House

After the p movies follow n lines each containing exactly one name in the same format as in the movie 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 1100 different actors.

Output

For every scenario first print the number of the scenario in the format shown in the sample output. Then print for every actor name in the list of names their advanced Bacon number based on the movies in the movie database of the scenario. The actors should be output in the order given in the input file. Actors that do not have any relation to Bacon via the movies have advanced Bacon number infinity. Adhere to the format shown in the sample output.

Print a blank line after each case.

Sample Input
3 3
3 John, Belushi, Karen, Allen, Kevin, Bacon: Animal House
2 Tom, Hulce, Karen, Allen: Starting Over
1 Hill, Ken, Bob, Stock: kick the ball
John, Belushi
Tom, Hulce
Bob, Stock
0 0
Sample Output
Database 1
John, Belushi: 3
Tom, Hulce: 5
Bob, Stock: infinity
 

Author:  CHEN, Yusen

Contest: ZOJ Monthly, September 2011


读完题,就知道是最短路了,可是找不到一种很好的构图方法,一开始用的set把所有的演员先存下,然后再转成char字符数组,这样每个人就有固定的索引了,然后再用strtsr()扫一遍p部电影,根据索引构图,然后WA了= =

一上午一直在WA,自己出的数据都能过,无奈之下,百度了下,看人用map存,感觉这个方法很好用,然后直接全部重写,过了= =

以后要多学学STL= =


#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>

using namespace std;

#define PB push_back
#define MP make_pair
#define REP(i,n) for(int i=0;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define DWN(i,h,l) for(int i=(h);i>=(l);--i)
#define CLR(vis) memset(vis,0,sizeof(vis))
#define MST(vis,pos) memset(vis,pos,sizeof(vis))
#define MAX3(a,b,c) max(a,max(b,c))
#define MAX4(a,b,c,d) max(max(a,b),max(c,d))
#define MIN3(a,b,c) min(a,min(b,c))
#define MIN4(a,b,c,d) min(min(a,b),min(c,d))
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LINF 1000000000000000000LL
#define eps 1e-8

typedef long long ll;

const int maxn=1111;


char s[3333];


map<string,int> c;
map<string,int>::iterator it;

vector<int> oo;

struct node{
    int to;
    int w;
    int u;
    int next;
}a[1000015];

int p,n,m,nn;
int dis[maxn*2];
int head[maxn*2],vis[maxn*2];
int edge;
queue<int>q;

int ans;

void inint()
{
    edge=0;
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int w)
{
    a[edge].u=u;
    a[edge].to=v;
    a[edge].w=w;
    a[edge].next=head[u];
    head[u]=edge++;
}

bool spfa(int s)
{
    int v,i;
    while(!q.empty()) q.pop();
    for(i=1;i<ans;i++)
    dis[i]=(i==s?0:INF);
    memset(vis,0,sizeof(vis));
    vis[s]=1;
    q.push(s);
    while(!q.empty())
    {
        int tmp=q.front();
        q.pop();
        for(i=head[tmp];i!=-1;i=a[i].next)
        {
            if(dis[tmp]+a[i].w<dis[v=a[i].to])
            {
                dis[v]=dis[tmp]+a[i].w;
                if(!vis[v])
                {
                    vis[v]=true;
                    q.push(v);
                }
            }
        }
        vis[tmp]=false;
    }
    return true;
}


void add(int k)
{
    for(int i=0;i<oo.size();i++)
    {
        for(int j=i+1;j<oo.size();j++)
        {
            addedge(oo[i],oo[j],k);
            addedge(oo[j],oo[i],k);
        }
    }
}


int main()
{
    int cnt=1;
    int k;
    while(cin>>p>>n)
    {
        if(p==0 && n==0)
            break;

        inint();
        c.clear();

        printf("Database %d\n",cnt++);

        ans=0;

        string s1="Kevin, Bacon";

        c[s1]=ans++;

        REP(i,p)
        {
            oo.clear();
            scanf("%d",&k);
            CLR(s);
            gets(s);
            int len=strlen(s);
            int res=0;
            s1="";
            REP(i,len)
            {
                if(s[i]==' ')
                    continue;
                if(s[i]==':')
                {
                    it=c.find(s1);
                    if(it==c.end())
                    {
                        c[s1]=ans++;
                    }
                    oo.PB(c[s1]);
                    add(k);
                    break;
                }
                if(s[i]==',')
                {
                    res++;
                    if(res%2==0)
                    {
                        it=c.find(s1);
                        if(it==c.end())
                        {
                          c[s1]=ans++;
                        }
                        oo.PB(c[s1]);
                        s1="";
                    }
                    else
                        s1+=", ";
                }
                else
                    s1+=s[i];
            }
        }

        spfa(0);

        string s2;

        REP(i,n)
        {
            getline(cin,s2);

            cout<<s2<<": ";

            it=c.find(s2);

            if(it==c.end())
            {
                cout<<"infinity"<<endl;
                continue;
            }

            int pos=c[s2];

            if(dis[pos]==INF)
               cout<<"infinity"<<endl;
            else
            {
                cout<<dis[pos]<<endl;
            }
        }

       cout<<endl;

    }
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值