zoukankan      html  css  js  c++  java
  • [POJ2288] Islands and Bridges

    Description

    Given a map of islands and bridges that connect these islands, a Hamilton path, as we all know, is a path along the bridges such that it visits each island exactly once. On our map, there is also a positive integer value associated with each island. We call a Hamilton path the best triangular Hamilton path if it maximizes the value described below.

    Suppose there are n islands. The value of a Hamilton path C1C2...Cn is calculated as the sum of three parts. Let Vi be the value for the island Ci. As the first part, we sum over all the Vi values for each island in the path. For the second part, for each edge CiCi+1 in the path, we add the product Vi*Vi+1. And for the third part, whenever three consecutive islands CiCi+1Ci+2 in the path forms a triangle in the map, i.e. there is a bridge between Ci and Ci+2, we add the product Vi*Vi+1*Vi+2.

    Most likely but not necessarily, the best triangular Hamilton path you are going to find contains many triangles. It is quite possible that there might be more than one best triangular Hamilton paths; your second task is to find the number of such paths.

    Input

    The input file starts with a number q (q<=20) on the first line, which is the number of test cases. Each test case starts with a line with two integers n and m, which are the number of islands and the number of bridges in the map, respectively. The next line contains n positive integers, the i-th number being the Vi value of island i. Each value is no more than 100. The following m lines are in the form x y, which indicates there is a (two way) bridge between island x and island y. Islands are numbered from 1 to n. You may assume there will be no more than 13 islands.

    Output

    For each test case, output a line with two numbers, separated by a space. The first number is the maximum value of a best triangular Hamilton path; the second number should be the number of different best triangular Hamilton paths. If the test case does not contain a Hamilton path, the output must be `0 0'.

    Note: A path may be written down in the reversed order. We still think it is the same path.

    Sample Input

    2
    3 3
    2 2 2
    1 2
    2 3
    3 1
    4 6
    1 2 3 4
    1 2
    1 3
    1 4
    2 3
    2 4
    3 4
    

    Sample Output

    22 3
    69 1
    

    Source

     

     
     
    状态压缩DP。
    设$large f[S][i][j]$为路过城市的状态为$large S$,最后一个经过$large j$,倒数第二个经过$large i$的最大权值。
    $large g[S][i][j]$为$large f[S][i][j]$取最大值时候的方案总数。
    然后随便转移就行了。
     

     
    #include <iostream>
    #include <cstdio>
    #include <cstring>
    using namespace std;
    #define reg register
    inline char gc() {
        static const int BS = 1 << 22;
        static unsigned char buf[BS], *st, *ed;
        if (st == ed) ed = buf + fread(st = buf, 1, BS, stdin);
        return st == ed ? EOF : *st++;
    }
    #define gc getchar
    inline int read() {
        int res = 0;char ch=gc();
        while(!isdigit(ch)) ch=gc();
        while(isdigit(ch)) res=(res<<3)+(res<<1)+(ch^48), ch=gc();
        return res;
    }
    #define ll long long 
    int T;
    int n, m;
    int G[15][15];
    int f[1<<13][15][15];
    ll g[1<<13][15][15];
    int bin[15];
    int val[15];
    int ans1;
    ll ans2;
    
    int main() 
    {
        bin[0] = 1;for (int i = 1 ; i <= 14 ; i ++) bin[i] = bin[i-1] << 1;
        T = read();
        while(T--)
        {
            n = read(), m = read();
            memset(G, 0, sizeof G);
            memset(g, 0, sizeof g);
            memset(f, -1, sizeof f);
            for (reg int i = 1 ; i <= n ; i ++) val[i] = read();
            for (reg int i = 1 ; i <= m ; i ++)
            {
                int x = read(), y = read();
                G[x][y] = G[y][x] = 1;
            }
            if (n == 1) {
                printf("%d 1
    ", val[1]);
                continue;
            }
            for (reg int i = 1 ; i <= n ; i ++)
                for (reg int j = 1 ; j <= n ; j ++)
                    if (G[i][j] and i != j)
                        f[bin[i-1]+bin[j-1]][i][j] = val[i] + val[j] + val[i] * val[j], g[bin[i-1]+bin[j-1]][i][j] = 1;
            for (reg int S = 0 ; S <= (1 << n) - 1 ; S ++)
            {
                for (reg int i = 1 ; i <= n ; i ++)
                {
                    if (!(S&bin[i-1])) continue;
                    for (reg int j = 1 ; j <= n ; j ++)
                    {
                        if (!(S&bin[j-1]) or j == i or !G[i][j] or f[S][i][j] == -1) continue;
                        for (reg int k = 1 ; k <= n ; k ++)
                        {
                            if ((S&bin[k-1]) or k == i or k == j or !G[k][j]) continue;
                            int tmp = f[S][i][j] + val[k] + val[k] * val[j];
                            if (G[k][i]) tmp += val[k] * val[j] * val[i];
                            if (f[S|bin[k-1]][j][k] < tmp) {
                                f[S|bin[k-1]][j][k] = tmp;
                                g[S|bin[k-1]][j][k] = g[S][i][j];
                            } else if (f[S|bin[k-1]][j][k] == tmp) {
                                g[S|bin[k-1]][j][k] += g[S][i][j];
                            }
                        }
                    }
                }
            }
            ans1 = 0, ans2 = 0;
            for (reg int i = 1 ; i <= n ; i ++)
                for (reg int j = 1 ; j <= n ; j ++)
                    if (i != j and G[i][j]) {
                        if (ans1 < f[(1<<n)-1][i][j]) {
                            ans1 = f[(1<<n)-1][i][j], ans2 = g[(1<<n)-1][i][j];
                        } else if (ans1 == f[(1<<n)-1][i][j]) ans2 += g[(1<<n)-1][i][j];
                    }
            printf("%d %lld
    ", ans1, ans2 / 2);
        }
        return 0;
    }
  • 相关阅读:
    C#调用Exe文件的方法及如何判断程序调用的exe已结束(转)
    C# Color (转)
    【666】语义分割减少过拟合
    【665】构建多损失函数
    libc timer
    分支管理
    MULLS:论文阅读
    微信支付宝整合支付开发中的常见问题
    IIS8中安装和使用URL重写工具(URL Rewrite)的方法
    通过Java 技术手段,检查你自己是不是被绿了...
  • 原文地址:https://www.cnblogs.com/BriMon/p/9554207.html
Copyright © 2011-2022 走看看