zoukankan      html  css  js  c++  java
  • 数据结构 娱乐中心选址 (Floyd+暴力)

    Description

    有n个地点编号1~n,在其中一个地点建立娱乐中心,要求该点距其它各地点的最长往返路程最短,相同条件下总的往返路程和越短越好,如果仍相同,取编号最小的地点,问娱乐中心应选址何处?

    Input

    第一行输入测试数据组数。每组数据第一行输入地点数n(2≤n≤300),路径数m(0≤m≤10000)。接下来m行,每行有一条有向边,输入起终点u、v(1≤u,v≤n),路径长度d(0≤d≤30000)。

    Output

    对每一个测试样例输出两行。第一行输出测试样例组数。第二行,若能找到可以到达所有地点的选址,输出最佳选址点的编号、最长往返路程、往返路程和;否则,输出-1。每两组测试数据输出一个空行。

    Sample Input

    2
    3 4
    1 2 1
    2 1 2
    2 3 10
    3 123
    2 1
    1 2 1

    Sample Output

    Case #1:
    1 34 37
     
    Case #2:
    -1

    HINT

    考察知识点:图的Floyd算法,时间复杂度O(n^3),空间复杂度O(m*n)


    Append Code

    析:由于 n 比较小,我们先用Floyd把任意两个结点的最短路长度示求出来,然后再遍历所有任意两个结点,去枚举哪个点做娱乐地址,求出最优的。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #define debug puts("+++++")
    //#include <tr1/unordered_map>
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    //using namespace std :: tr1;
     
    typedef long long LL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const double inf = 0x3f3f3f3f3f3f;
    const LL LNF = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 300 + 5;
    const LL mod = 1e9 + 7;
    const int N = 1e6 + 5;
    const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
    const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
    const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    inline LL gcd(LL a, LL b){  return b == 0 ? a : gcd(b, a%b); }
    inline int gcd(int a, int b){  return b == 0 ? a : gcd(b, a%b); }
    inline int lcm(int a, int b){  return a * b / gcd(a, b); }
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline int Min(int a, int b){ return a < b ? a : b; }
    inline int Max(int a, int b){ return a > b ? a : b; }
    inline LL Min(LL a, LL b){ return a < b ? a : b; }
    inline LL Max(LL a, LL b){ return a > b ? a : b; }
    inline bool is_in(int r, int c){
        return r >= 0 && r < n && c >= 0 && c < m;
    }
    int dp[maxn][maxn];
     
    int main(){
        int T;  scanf("%d", &T);
        for(int kase = 1; kase <= T; ++kase){
            scanf("%d %d", &n, &m);
            for(int i = 1; i <= n; ++i)
                for(int j = 1; j <= n; ++j)
                    dp[i][j] = i == j ? 0 : INF;
            int u, v, d;
            for(int i = 0; i < m; ++i){
                scanf("%d %d %d", &u, &v, &d);
                if(d < dp[u][v])  dp[u][v] = d;
            }
     
            for(int k = 1; k <= n; ++k)
                for(int i = 1; i <= n; ++i)
                    for(int j = 1; j <= n; ++j)
                            if(dp[i][j] > dp[i][k] + dp[k][j]) dp[i][j] = dp[i][k] + dp[k][j];
            int ans = -1, ans1 = INF, ans2 = INF;
            for(int i = 1; i <= n; ++i){
                int sum1 = 0, sum2 = 0;
                for(int j = 1; j <= n; ++j){
                    if(sum1 > ans1) break;
                    int tmp = dp[i][j] + dp[j][i];
                    if(sum1 < tmp)  sum1 = tmp;
                    sum2 += tmp;
                }
                if(sum1 < ans1 || (sum1 == ans1 && sum2 < ans2)){
                    ans1 = sum1;
                    ans2 = sum2;
                    ans = i;
                }
            }
     
            if(kase != 1)  printf("
    ");
            printf("Case #%d:
    ", kase);
            if(ans == -1)  printf("-1
    ");
            else printf("%d %d %d
    ", ans, ans1, ans2);
        }
        return 0;
    }
    
  • 相关阅读:
    大数据分析谨慎对待
    大数据时代下,市场研究何去何从
    大数据时代下,市场研究何去何从
    浅谈数据挖掘与数据分析?
    浅谈数据挖掘与数据分析?
    大数据的七种商业化模式
    大数据的七种商业化模式
    栈的应用---编译器左右符号的语法匹配
    Python 对Twitter tweet的元素 (Word, Screen Name, Hash Tag)的频率分析
    东华软件笔试
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/5990790.html
Copyright © 2011-2022 走看看