UVA - 247 Calling Circles (Floyd算法)

8 篇文章 0 订阅

If you’ve seen television commercials for long-distance phone companies lately, you’ve noticed thatmany companies have been spending a lot of money trying to convince people that they provide thebest service at the lowest cost. One company has “calling circles.” You provide a list of people thatyou call most frequently. If you call someone in your calling circle (who is also a customer of the samecompany), you get bigger discounts than if you call outside your circle. Another company points outthat you only get the big discounts for people in your calling circle, and if you change who you callmost frequently, it’s up to you to add them to your calling circle.

LibertyBell Phone Co. is a new company that thinks they have the calling plan that can put othercompanies out of business. LibertyBell has calling circles, but they figure out your calling circle foryou. This is how it works. LibertyBell keeps track of all phone calls. In addition to yourself, yourcalling circle consists of all people whom you call and who call you, either directly or indirectly.

For example, if Ben calls Alexander, Alexander calls Dolly, and Dolly calls Ben, they are all withinthe same circle. If Dolly also calls Benedict and Benedict calls Dolly, then Benedict is in the samecalling circle as Dolly, Ben, and Alexander. Finally, if Alexander calls Aaron but Aaron doesn’t callAlexander, Ben, Dolly, or Benedict, then Aaron is not in the circle.

You’ve been hired by LibertyBell to write the program to determine calling circles given a log ofphone calls between people.

Input

The input file will contain one or more data sets. Each data set begins with a line containing twointegers, n and m. The first integer, n, represents the number of different people who are in the dataset. The maximum value for n is 25. The remainder of the data set consists of m lines, each representinga phone call. Each call is represented by two names, separated by a single space. Names are first namesonly (unique within a data set), are case sensitive, and consist of only alphabetic characters; no nameis longer than 25 letters.

For example, if Ben called Dolly, it would be represented in the data file as

Ben DollyInput

 is terminated by values of zero (0) for n and m.

Output

For each input set, print a header line with the data set number, followed by a line for each callingcircle in that data set. Each calling circle line contains the names of all the people in any order withinthe circle, separated by comma-space (a comma followed by a space). Output sets are separated byblank lines.

原题链接:点击打开链接


数据量很小,直接套floyd的三重循环,让可以相互沟通的节点连在一起,让后暴力枚举一遍。还是比较简单AC的

#include <iostream>
#include <map>
#include <string>
#include <cstring>
using namespace std;
int m,n,Time=1,pd[30][30],f[30];
string s[30];

int main()
{
    while(cin>>n>>m)
    {
        if(n==0&&m==0)break;
        int cnt=0;
        memset(pd,0,sizeof(pd));                           
        memset(f,0,sizeof(f));
        map<string,int>p;
        for(int i=1;i<=m;i++)
        {
            string s1,s2;
            cin>>s1>>s2;
            if(!p.count(s1)) p[s1]=cnt++;
            if(!p.count(s2)) p[s2]=cnt++;
            int x1=p[s1],x2=p[s2];                                 //把字符串用数字标记,便于后面的使用
            //cout<<x1<<x2<<endl;
            s[x1]=s1,s[x2]=s2;//cout<<1<<endl;
            pd[x1][x2]=1;
        }
        for(int k=0;k<n;k++)                                      //以下是Floyd算法
            for(int i=0;i<n;i++)
                for(int j=0;j<n;j++)
                {
                    if(pd[i][k]==1&&pd[k][j]==1) pd[i][j]=1;      //把相互可以连通的点连通
                }
        cout<<"Calling circles for data set "<<Time<<":"<<endl;
        Time++;
        for(int i=0;i<n;i++)
        {
            if(f[i]==1)continue;                                  //避免同一个电话环变着顺序的输出
            cout<<s[i];
            for(int j=0;j<n;j++)
            {
                if(i==j)continue;
                if(pd[i][j]!=1||pd[j][i]!=1)continue;
                cout<<", "<<s[j];        //逗号后面有个空格,之前没有注意然后WA了一遍,很难受
                f[j]=1;                                          //标记他已经输出过了
            }
            cout<<endl;
        }
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值