dp专题之 B - Islands and Bridges POJ - 2288

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 CiC i+1 in the path, we add the product Vi*V i+1. And for the third part, whenever three consecutive islands CiC i+1C i+2 in the path forms a triangle in the map, i.e. there is a bridge between Ci and C i+2, we add the product Vi*V i+1*V i+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

取dp[state][i][j]表示state状态下倒数第二个岛为i,最后一个岛为j时的最优解,num[state][i][j]为相应的路径数目,其中将state转化为二进制表示的i位为1表示岛i被访问过,反之为0。 

注:再此状态下, 位运算 & 代表当前岛是否存在, 位运算 | 代表将当前岛加入状态
则显然当有边(i,j)存在时,有如下初值可赋:
dp[(1<<i) | (1<<j)][i][j]=val[i]+val[j]+val[i]*val[j]

num[(1<<i)+(1<<j)][i][j]=1。
如果状态(state,j,k)可达,检查岛x,如果此时x没有被访问过并且有边(k,x)存在,则做如下操作:
1)设tmp为下一步访问岛k时获得的总利益,i = state+(1<<x)。
2)如果tmp>dps[i][k][x],表示此时可以更新到更优解,则更新:
    dp[i][k][x]=tmp,num[i][k][x]=num[state][j][k]。
3)如果tmp==dp[i][k][x],表示此时可以获得达到局部最优解的更多方式,则更新:
    num[i][k][x]+=num[state][j][k]。
最后检查所有的状态((1<<n)-1,i,j),叠加可以得到最优解的道路数。
需要注意的是,题目约定一条路径的两种行走方式算作一种,所以最终结果要除2。

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
using namespace std;
int smap[14][14], dp[1<<13][13][13];
long long num[1<<13][13][13];
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n, m, i, v[14] = {0}, j, k, x;
        cin >> n >> m;
        for(i = 0; i < n; i++)
        {
            cin >> v[i];
        }
        memset(smap, 0, sizeof(smap));
        for(i = 0; i < m; i++)
        {
            int x, y;
            cin >> x >> y;
            x--;
            y--;
            smap[x][y] = 1;
            smap[y][x] = 1;
        }
        if(n==1){
            printf("%d 1\n",v[0]);
            continue;
        }
        memset(dp, -1, sizeof(dp));
        memset(num, 0, sizeof(num));
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
                if(i != j && smap[i][j])
                {
                    dp[(1 << i) | (1 << j)][i][j] = v[i] + v[j] + v[i] * v[j];
                    num[(1 << i) | (1 << j)][i][j] = 1;
                }
        for(i = 0; i < (1 << n); i++)
            for(j = 0; j < n; j++)
                if((i & (1 << j)) != 0)
                    for(k = 0; k < n; k++)
                        if(smap[j][k] && k != j && (i & (1 << k)) != 0 && dp[i][j][k] != -1)
                            for(x = 0; x < n; x++)
                                if(smap[k][x] && j != x && k != x && (i & (1 << x))==0)
                                {
                                    int tmp = dp[i][j][k] + v[x] + v[k] * v[x];
                                    if(smap[j][x])
                                    {
                                        tmp += v[x] * v[j] * v[k];
                                    }
                                    if(dp[i | (1 << x)][k][x] < tmp)
                                    {
                                        dp[i | (1 << x)][k][x] = tmp;
                                        num[i | (1 << x)][k][x] = num[i][j][k];
                                    }
                                    else if(dp[i | (1 << x)][k][x] == tmp)
                                    {
                                        num[i | (1 << x)][k][x] += num[i][j][k];
                                    }
                                }
        int ans1 = 0;
        long long ans2 = 0;
        for(i = 0; i < n; i++)
            for(j = 0; j < n; j++)
                if(smap[i][j] && i != j)
                {
                    if(ans1<dp[(1<<n)-1][i][j]){
                        ans1=dp[(1<<n)-1][i][j];
                        ans2=num[(1<<n)-1][i][j];
                    }else if(ans1==dp[(1<<n)-1][i][j])
                        ans2+=num[(1<<n)-1][i][j];
                }
        cout<<ans1<<" "<<ans2/2<<endl;
    }
    return 0;
}

本文章参考于:http://www.cnblogs.com/jackge/archive/2013/05/24/3096162.html

Delphi 12.3 作为一款面向 Windows 平台的集成开发环境,由 Embarcadero Technologies 负责其持续演进。该环境以 Object Pascal 语言为核心,并依托 Visual Component Library(VCL)框架,广泛应用于各类桌面软件、数据库系统及企业级解决方案的开发。在此生态中,Excel4Delphi 作为一个重要的社区开源项目,致力于搭建 Delphi 与 Microsoft Excel 之间的高效桥梁,使开发者能够在自研程序中直接调用 Excel 的文档处理、工作表管理、单元格操作及宏执行等功能。 该项目以库文件与组件包的形式提供,开发者将其集成至 Delphi 工程后,即可通过封装良好的接口实现对 Excel 的编程控制。具体功能涵盖创建与编辑工作簿、格式化单元格、批量导入导出数据,乃至执行内置公式与宏指令等高级操作。这一机制显著降低了在财务分析、报表自动生成、数据整理等场景中实现 Excel 功能集成的技术门槛,使开发者无需深入掌握 COM 编程或 Excel 底层 API 即可完成复杂任务。 使用 Excel4Delphi 需具备基础的 Delphi 编程知识,并对 Excel 对象模型有一定理解。实践中需注意不同 Excel 版本间的兼容性,并严格遵循项目文档进行环境配置与依赖部署。此外,操作过程中应遵循文件访问的最佳实践,例如确保目标文件未被独占锁定,并实施完整的异常处理机制,以防数据损毁或程序意外中断。 该项目的持续维护依赖于 Delphi 开发者社区的集体贡献,通过定期更新以适配新版开发环境与 Office 套件,并修复已发现的问题。对于需要深度融合 Excel 功能的 Delphi 应用而言,Excel4Delphi 提供了经过充分测试的可靠代码基础,使开发团队能更专注于业务逻辑与用户体验的优化,从而提升整体开发效率与软件质量。 资源来源于网络分享,仅用于学习交流使用,请勿用于商业,如有侵权请联系我删除!
<think>好的,用户的问题是关于如何替代`runtime.bridges.bridges.toArray`方法的实现。首先,我需要理解这个方法的上下文和用途。根据用户提供的引用内容,特别是引用[1]和[2],提到了`createBridge`方法以及`AndroidDebugBridge`类。看起来这可能与Android调试桥(ADB)相关,用于管理连接到ADB的桥接实例。 用户提到`runtime.bridges.bridges.toArray`,可能是在处理ADB桥接实例的集合,需要将集合转换为数组。`toArray`方法通常用于将集合(如List、Set)转换为数组。用户可能遇到了性能问题,或者该方法在某些环境下不可用,需要替代方案。 接下来,我需要考虑可能的替代方法。首先,如果使用的是Java集合框架,比如`ArrayList`,可以使用`toArray()`方法的不同重载版本。例如,使用`toArray(new Type[0])`,这在较新的Java版本中推荐使用,因为性能更好。另外,手动遍历集合并填充数组也是一种替代方法,尽管效率可能较低,但在某些情况下可能更灵活。 此外,需要考虑用户的具体场景。比如,是否在Android开发中处理ADB连接,或者在使用Flink时遇到隐式转换问题(引用[3])。不过用户的问题更偏向于Java集合操作,可能与ADB桥接管理相关。 还需要检查用户提供的引用内容是否有其他线索。引用[1]提到`createBridge()`方法会检查现有的ADB连接,引用[2]说明如何创建不链接到特定ADB可执行文件的新桥接。可能用户在处理桥接实例的集合时,需要将这些实例转换为数组进行操作,比如遍历或传递给需要数组的方法。 可能的解决方案包括使用Java 8的流API来转换为数组,或者使用第三方库如Guava或Apache Commons Collections。但需要确保这些替代方法与原有代码兼容,并且不会引入新的依赖。 另外,用户可能需要确保线程安全,特别是在多线程环境下操作桥接集合。因此,在转换数组时,可能需要同步块来保证数据的一致性。 总结,替代`toArray`的方法包括使用特定类型的数组初始化、手动遍历填充、流API转换,或者第三方库方法。需要根据具体的使用场景和代码环境选择最合适的方法,并注意线程安全和性能问题。</think>根据代码上下文分析,`runtime.bridges.bridges.toArray`可能涉及Android调试桥(ADB)的桥接实例管理。以下是可替代的三种实现方案: ### 方案一:类型安全数组转换(推荐) ```java AndroidDebugBridge[] bridgeArray = bridges.toArray(new AndroidDebugBridge[0]); ``` * 优势:JDK6+支持,避免强制类型转换错误 * 适用场景:需要精确控制数组类型时 ### 方案二:手动遍历填充 ```java AndroidDebugBridge[] bridgeArray = new AndroidDebugBridge[bridges.size()]; int index = 0; for (AndroidDebugBridge bridge : bridges) { bridgeArray[index++] = bridge; } ``` * 优势:避免并发修改异常 * 适用场景:需要遍历时进行额外逻辑处理 ### 方案三:Java Stream转换(JDK8+) ```java AndroidDebugBridge[] bridgeArray = bridges.stream() .toArray(AndroidDebugBridge[]::new); ``` * 优势:函数式编程风格 * 适用场景:需要链式处理集合元素时 ### 线程安全注意事项 若操作涉及多线程访问桥接集合,建议使用同步块: ```java synchronized (bridges) { AndroidDebugBridge[] bridgeArray = bridges.toArray(new AndroidDebugBridge[0]); // 后续操作 } ``` ### 扩展建议 1. 对于需要动态调整集合的场景,可参考`createBridge()`方法中的连接管理逻辑[^1] 2. 桥接实例创建可借鉴`AndroidDebugBridge`私有构造函数的设计模式[^2] 3. 集合操作可结合ADB客户端管理机制进行优化[^4]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值