zoukankan      html  css  js  c++  java
  • poj2139(Six Degrees of Cowvin Bacon)最短路

    Description

    The cows have been making movies lately, so they are ready to play a variant of the famous game "Six Degrees of Kevin Bacon". 

    The game works like this: each cow is considered to be zero degrees of separation (degrees) away from herself. If two distinct cows have been in a movie together, each is considered to be one 'degree' away from the other. If a two cows have never worked together but have both worked with a third cow, they are considered to be two 'degrees' away from each other (counted as: one degree to the cow they've worked with and one more to the other cow). This scales to the general case. 

    The N (2 <= N <= 300) cows are interested in figuring out which cow has the smallest average degree of separation from all the other cows. excluding herself of course. The cows have made M (1 <= M <= 10000) movies and it is guaranteed that some relationship path exists between every pair of cows. 

    Input

    * Line 1: Two space-separated integers: N and M 

    * Lines 2..M+1: Each input line contains a set of two or more space-separated integers that describes the cows appearing in a single movie. The first integer is the number of cows participating in the described movie, (e.g., Mi); the subsequent Mi integers tell which cows were. 

    Output

    * Line 1: A single integer that is 100 times the shortest mean degree of separation of any of the cows. 

    Sample Input

    4 2
    3 1 2 3
    2 3 4
    

    Sample Output

    100
    

    Hint

    [Cow 3 has worked with all the other cows and thus has degrees of separation: 1, 1, and 1 -- a mean of 1.00 .] 

    题意:牛牛们要拍大电影啦!拍同一部电影的牛距离为1,问你一头牛到其他所有牛距离的平均值的最小值是多少?


    题解:裸的Floyd-Warshall算法,直接做就行了


    #include <iostream>
    #include <sstream>
    #include <fstream>
    #include <string>
    #include <map>
    #include <vector>
    #include <list>
    #include <set>
    #include <stack>
    #include <queue>
    #include <deque>
    #include <algorithm>
    #include <functional>
    #include <iomanip>
    #include <limits>
    #include <new>
    #include <utility>
    #include <iterator>
    #include <cstdio>
    #include <cstdlib>
    #include <cstring>
    #include <cctype>
    #include <cmath>
    #include <ctime>
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    const int maxn = 310;
    
    int d[maxn][maxn];
    int g[maxn];    //记录同一个电影中的牛
    int n, m;
    
    void floyd()
    {
        for (int k = 0; k < n; ++k)
            for (int i = 0; i < n; ++i)
                for (int j = 0; j < n; ++j)
                    d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
    }
    
    int main()
    {
    
        cin >> n >> m;
        for (int i = 0; i < n; ++i)
            for (int j = 0; j < n; ++j)
                d[i][j] = (i == j) ? 0 : INF;
        while (m--)
        {
            int v;
            scanf("%d", &v);
            for (int i = 0; i < v; ++i)
            {
                scanf("%d", &g[i]);
                g[i]--;
            }
            for (int i = 0; i < v; ++i)
                for (int j = i+1; j < v; ++j)
                    d[g[i]][g[j]] = d[g[j]][g[i]] = 1;
        }
        floyd();
        int ans = INF;
        for (int i = 0; i < n; ++i)
        {
            int sum = 0;
            for (int j = 0; j < n; ++j)
                sum += d[i][j];
            ans = min(ans, sum);
        }
        cout << 100 * ans / (n - 1) << endl;
        return 0;
    }
    


  • 相关阅读:
    java如何编写多线程
    Java调用dll动态库
    HashMap源码解析
    LinkedList源码解析
    ArrayList源码解析
    springboot配置cxf
    java生成二维码
    原生js--跨域消息传递
    原生js--应用程序存储和离线web应用
    原生js--userData
  • 原文地址:https://www.cnblogs.com/godweiyang/p/12203953.html
Copyright © 2011-2022 走看看