最短路经典例题-紫书11-4 Calling Circles(Floyd传递闭包+map+DFS)

来源:互联网 发布:淘宝上苹果官方旗舰店 编辑:程序博客网 时间:2024/06/10 17:46
Calling Circles
Time Limit: 3000MS Memory Limit: Unknown 64bit IO Format: %lld & %llu

SubmitStatus

Description

If you've seen television commercials for long-distance phone companies lately, you've noticed that many companies have been spending a lot of money trying to convince people that they provide the best service at the lowest cost. One company has calling circles." You provide a list of people that you call most frequently. If you call someone in your calling circle (who is also a customer of the same company), you get bigger discounts than if you call outside your circle. Another company points out that you only get the big discounts for people in your calling circle, and if you change who you call most 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 other companies out of business. LibertyBell has calling circles, but they gure out your calling circle for you. This is how it works. LibertyBell keeps track of all phone calls. In addition to yourself, your calling 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 within the same circle. If Dolly also calls Benedict and Benedict calls Dolly, then Benedict is in the same calling circle as Dolly, Ben, and Alexander. Finally, if Alexander calls Aaron but Aaron doesn't call Alexander, 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 of phone calls between people.

Input
The  input  file  will  contain  one  or  more  data  sets.   Each  data  set  begins  with  a  line  containing  two integers,n and m.The first integer,n, represents the number of different people who are in the data set.  The maximum value for
n is 25.  The remainder of the data set consists of m lines, each representing a phone call.  Each call is represented by two names, separated by a single space.  Names are rst names only (unique within a data set), are case sensitive, and consist of only alphabetic characters; no name is longer than 25 letters.
For example, if Ben called Dolly, it would be represented in the data file as
Ben Dolly
Input 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 calling circle in that data set.  Each calling circle line contains the names of all the people in any order within the circle, separated by comma-space (a comma followed by a space).  Output sets are separated by blank lines.

Sample Input
5 6
Ben Alexander
Alexander Dolly
Dolly Ben
Dolly Benedict
Benedict Dolly
Alexander Aaron
14 34
John Aaron
Aaron Benedict
Betsy John
Betsy Ringo
Ringo Dolly
Benedict Paul
John Betsy
John Aaron
Benedict George
Dolly Ringo
Paul Martha
George Ben
Alexander George
Betsy Ringo
Alexander Stephen
Martha Stephen
Benedict Alexander
Stephen Paul
Betsy Ringo
Quincy Martha
Ben Patrick
Betsy Ringo
Patrick Stephen
Paul Alexander
Patrick Ben
Stephen Quincy
Ringo Betsy
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Betsy Benedict
Quincy Martha
0 0

Sample Output
Calling circles for data set 1:
Ben, Alexander, Dolly, Benedict
Aaron
Calling circles for data set 2:
John, Betsy, Ringo, Dolly
Aaron
Benedict

Paul, George, Martha, Ben, Alexander, Stephen, Quincy, Patrick

题意:

如果两个人互相打电话(直接或者间接),则说他们在同一个电话圈里。例如,a打给b,b打给c,c打给d,d打给a,则这四个人在同一个圈里;如果e打给f,而f不打给e,则不能推出e和f在同一个电话圈。输入n(n<=25)个人的m次电话,找出所有的电话圈。人名只包含字母,不超过25个字符,且不重复。

分析:

是有向图的强连通分量问题(tarjan)。floyd可以用来求任意两个连通点间的最短路的长度。比较简单的应用,可以判断两点间的连通性。

【Floyd循环三次,数据一大就跪了】,这里可以用floyd来求传递闭包,用map标记每一个人对应的序号,最后用DFS找到路线。

注意:

1、思路清晰~~。    2、这题好像也可用tarjan求解~???          3、这里DFS的运用要熟悉喔~

#include <cstdio>#include <iostream>#include <map>#include <cstring>#include <string>using namespace std;    int n, m;int d[30][30];int mark[30];map<string, int> id;map<int, string> name;void dfs(int i, int flag){    if(!flag) printf(", ");    cout << name[i];    mark[i] = false;    for(int j = 1; j <= n; j++)        if(mark[j] && d[j][i] && d[i][j])   { dfs(j, 0);  break;}    return;}int main(){    int cas = 1;    while(scanf("%d%d", &n, &m) && (n || m))    {        id.clear();        name.clear();        int cnt = 1;        memset(d, 0, sizeof(d));        memset(mark, -1, sizeof(mark));        for(int i = 1; i <= m; i++)        {            string str1, str2;            int x, y;            cin >> str1 >> str2;            if(!id.count(str1))            {                x = id[str1] = cnt ++;                name[cnt-1] = str1;            }            else                x = id[str1];            if(!id.count(str2))            {                y = id[str2] = cnt ++;                name[cnt-1] = str2;            }            else                y = id[str2];            d[x][y] = 1;        }        for(int k = 1; k <= n; k++)            for(int i = 1; i <= n; i++)                for(int j = 1; j <= n; j++)                    if(!d[i][j] && d[i][k] && d[k][j])        { d[i][j] = 1; }        printf("Calling circles for data set %d:\n", cas ++);        for(int i = 1; i <= n; i++)            if(mark[i])                {                    dfs(i, 1);                    cout << endl;                }    }    return 0;}


阅读全文
0 0
原创粉丝点击