05-图3. 六度空间 (30) floyd bfs

05-图3. 六度空间 (30) 题目地址

floyd 最后一个case 超时 (想想也应该是超时的)

/*
05-图3. 六度空间 (30)
http://www.patest.cn/contests/mooc-ds/05-%E5%9B%BE3
floyd() 算所有两点之间的距离 
运行超时
*/
#include   

#define N 10001
#define INF 9999999

int mp[N][N];
int num[N];
int n, m;

void floyd()
{
    int i, j, k;
    for (i = 1; i <= n; i++)
    {
        for (j = 1; j <= n; j++)
        {
            for (k = 1; k <= n; k++)
            {
                if (mp[i][k] + mp[k][j] < mp[i][j])
                {
                    mp[i][j] = mp[i][k] + mp[k][j];
                }
            }
            if (mp[i][j] <= 6)
            {
                num[i] ++;
            }
        }
    }
    /*for (i = 1; i <= n; i++)
    {
    for (j = 1; j <= n; j++)
    {
    if (mp[i][j] <= 6)

    }
    }*/
}

int main()
{
    //freopen("in", "r", stdin);
    while (scanf("%d%d", &n, &m) != EOF)
    {
        int i, j;
        for (i = 1; i <= n; i++)
        {
            for (j = 1; j <= n; j++)
            {
                mp[i][j] = INF;
            }
            mp[i][i] = 0;
            num[i] = 0;
        }

        int u, v;
        for (i = 0; i < m; i++)
        {
            scanf("%d%d", &u, &v);
            mp[u][v] = 1;
            mp[v][u] = 1;
        }
        floyd();

        for (i = 1; i <= n; i++)
        {
            printf("%d: ", i);
            double sum = num[i] * 1.0 / n;
            printf("%.2f%%\n", sum*100);
        }
        //printf("\n");
    }
    return 0;
}

采用bfs 层次遍历可以 ac

/*
05-图3. 六度空间 (30)
http://www.patest.cn/contests/mooc-ds/05-%E5%9B%BE3
bfs()层次遍历  利用queue 和 lastVal 变量来标记每一层
直接ac
*/
#include   
#include 

using namespace std;

#define N 10001

int n, m;

vector<int> mp[N]; // mp[i] i 可到达的点的vector<> 遍历vector 就可以取得相应的点

bool visit[N];

int bfs(int sta)
{
    int i, j, k;
    for (i = 1; i <= n; i++)
    {
        visit[i] = false; // visit 初始化
    }
    queue<int> que;
    que.push(sta);
    visit[sta] = true;
    int lastVal = sta;
    int num = 0;

    int count = 0;

    while (num <= 6 && !que.empty())
    {
        vector<int> v; // 用来得到当前层数的所有元素
        v.clear();
        int tmp = que.front();

        while (tmp != lastVal)
        {
            v.push_back(tmp); 
            que.pop();
            if (que.empty())
            {
                break;
            }
            else{
                tmp = que.front();
            }
        }
        if (tmp == lastVal)
        {
            v.push_back(tmp);
            que.pop();
        }
        int len = v.size(); // 当前层数的元素个数  
        if (len <= 0) // 若为0 说明 已经不需要查找下去 直接退出
        {
            break;
        }
        count += len; // 记录数加上1 
        num++; // 层数加上 1

        for (i = 0; i < len; i++)
        {
            int nowVal = v[i];
            int nowValLinkLen = mp[nowVal].size();
            for (j = 0; j < nowValLinkLen; j++)
            {
                if (!visit[mp[nowVal][j]])
                {
                    que.push(mp[nowVal][j]);
                    lastVal = mp[nowVal][j];
                    visit[mp[nowVal][j]] = true;
                }
            }
        }

    }
    return count;
}

int main()
{
    //freopen("in", "r", stdin);
    while (scanf("%d%d", &n, &m) != EOF)
    {
        int i, j;
        for (i = 1; i <= n; i++)
        {
            mp[i].clear();
        }

        int u, v;
        for (i = 0; i < m; i++)
        {
            scanf("%d%d", &u, &v);
            mp[u].push_back(v);
            mp[v].push_back(u);
        }

        for (i = 1; i <= n; i++)
        {
            printf("%d: ", i);
            double sum = bfs(i) * 1.0 / n;
            printf("%.2f%%\n", sum*100);
        }
        //printf("\n");
    }
    return 0;
}

你可能感兴趣的:(#,图论-BFS-DFS应用,#,Dijkstra,Floyd)