poj 2288 tsp经典问题
题目链接:http://poj.org/problem?id=2288
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std; const int maxn = ;
typedef long long int ll_int; ll_int dp[maxn][maxn][<<maxn];
ll_int way[maxn][maxn][<<maxn];
int G[maxn][maxn];
int V[maxn]; int main()
{
freopen("E:\\acm\\input.txt","r",stdin);
int T;
cin>>T;
while(T--){
int n,m;
cin>>n>>m;
memset(G,,sizeof(G));
memset(dp,,sizeof(dp));
memset(way,,sizeof(way));
for(int i=;i<n;i++) cin>>V[i];
for(int i=;i<m;i++){
int x,y;
cin>>x>>y;
G[x-][y-] = ;
G[y-][x-] = ;
}
if(n == ){
printf("%d 1\n",V[]);
continue;
}
for(int i=;i<n;i++)
for(int j=i+;j<n;j++){
if(G[i][j]){
dp[i][j][(<<i)|(<<j)] = V[i] + V[j] + V[i]*V[j];
dp[j][i][(<<i)|(<<j)] = V[i] + V[j] + V[i]*V[j]; //先处理相邻的两点的;
way[i][j][(<<i)|(<<j)] = ;
way[j][i][(<<i)|(<<j)] = ;
}
}
int All = (<<n) - ;
for(int S = ;S <= All; S++){ //在每个状态下枚举状态内的点i,j,扩展出节点k.挺暴力的。
for(int i=;i<n;i++){
if(!(<<i & S)) continue;
for(int j=;j<n;j++){
if(!(<<j & S) || i == j || !dp[i][j][S]) continue;
for(int k=;k<n;k++){
if((<<k)&S || !G[j][k]) continue;
int r = S + (<<k);
ll_int q = dp[i][j][S] + V[k] + V[j]*V[k];
if(G[i][k]){
q += V[i]*V[j]*V[k];
}
if(q > dp[j][k][r]){
dp[j][k][r] = q;
way[j][k][r] = way[i][j][S];
}
else if(q == dp[j][k][r]){
way[j][k][r] += way[i][j][S];
}
}
}
}
}
ll_int ans,answay;
ans = ; answay = ;
for(int i=;i<n;i++)
for(int j=;j<n;j++){
if(i!=j){
if(dp[i][j][All] > ans){
ans = dp[i][j][All];
answay = way[i][j][All];
}
else if(dp[i][j][All] == ans ){
answay += way[i][j][All];
}
}
}
if(ans == ) printf("0 0\n");
else
printf("%I64d %I64d\n",ans,answay/); }
}
poj 2288 tsp经典问题的更多相关文章
- poj 2288 Islands and Bridges (状压dp+Tsp问题)
这道题千辛万苦啊! 这道题要涉及到当前点和前面两个点,那就设dp[state][i][j]为当前状态为state,当前点为i,前一个点为j 这个状态表示和之前做炮兵那题很像,就是涉及到三个点时,就多设 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- POJ 2288 Islands and Bridges(状压dp)
http://poj.org/problem?id=2288 题意: 有n个岛屿,每个岛屿有一个权值V,一条哈密顿路径C1,C2,...Cn的值为3部分之和: 第1部分,将路径中每个岛屿的权值累加起来 ...
- poj 2288 Islands and Bridges ——状压DP
题目:http://poj.org/problem?id=2288 状压挺明显的: 一开始写了(记忆化)搜索,但一直T: #include<iostream> #include<cs ...
- poj 2288 Islands and Bridges——状压dp(哈密尔顿回路)
题目:http://poj.org/problem?id=2288 不知为什么记忆化搜索就是WA得不得了! #include<iostream> #include<cstdio> ...
- poj 3311 tsp入门
题意:n+1个点:0--n,找一条路径从0点出发遍历1--n的点再回到0,每个点可经过不止一次,求最短路径 裸的TSP问题,先用Floyd求出各个点之间最短路,再状压dp即可 用n+1位二进制表示状态 ...
- POJ 1182 (经典食物链 /并查集扩展)
(參考他人资料) 向量偏移--由"食物链"引发的总结 http://poj.org/problem?id=1182这道食物链题目是并查集的变型.非常久曾经做的一次是水过的,这次 ...
- poj 2288 Islands and Bridges
题意: 给你一个双向连通图,求 获得权值最大 的 哈密顿通路的 权值 和 这个权值对应的数目: 其中权值计算方法是 列如 ABCD 权值是a+b+c+d+ab+bc+cd 如果 A,B,C 和B ...
- POJ 1201 Intervals (经典) (差分约束)
<题目链接> 题目大意:给你$n$段区间,$a_i,b_i,c_i$ 表示在 $[a_i,b_i]$ 区间内至少要选择$c_i$个点.现在问你在满足这n个条件的情况下,最少要选多少个点? ...
随机推荐
- Local IIS 7.0 - CS0016: Could not write to output file / Microsoft.Net > Framework > v4.0.30319 > Temporary ASP.NET Files
This week I went nuts over my local IIS. I have never swore to a machine that much in my whole life. ...
- hibernate 其中七种关联关系
写的很好 http://blog.csdn.net/qq_27550755/article/details/50070017
- XMPPFramework ios 例子中链接服务器失败,opnefire 服务器链接失败
首先说下上周又做了几天得无用功, 之前一直用的是ejabberd ,这次换了opnefire,有人说opnefire跟新的xmpp协议不兼容,后来又更换成了ejabberd, Github 上得dem ...
- UWP textbox 只能输入数字
private void Testbox_TextChanged(object sender, TextChangedEventArgs e) { var textbox = (TextBox) ...
- 流形(Manifold)初步【转】
转载自:http://blog.csdn.net/wangxiaojun911/article/details/17076465 欧几里得几何学(Euclidean Geometry) 两千三百年前, ...
- Win32中GDI+应用(二)--初始化与清理
GDI+提供了GdiplusStartup和 GdiplusShutdown 函数来进行初始化和完成清理工作.你必须在调用其他的GDI+函数之前,调用GdiplusStartup函数,在完成GDI+工 ...
- django基本命令备忘录
1. 新建一个 django project django-admin.py startproject project-name 新建 app python manage.py startapp ap ...
- .Net Remoting浅释
面试的时候,被问到.Net Remoting 工作方式及它和Web Service的区别,在此做个整理,顺便回顾一下. .Net Remoteing: 我们可以认为.Net Remoting是一种分布 ...
- IntelIoT技术笔记Java/Eclipse
1. 获取最新版本 使用"Team sync perspective",如果想要看到全部工程的差异,选择全部工程,右键-Team-sync with Repository:将会自动 ...
- php的几个版本的区别?
1. VC6与VC9的区别:VC6版本是使用Visual Studio 6编译器编译的,如果你的PHP是用Apache来架设的,那你就选择VC6版本.VC9版本是使用Visual Studio 200 ...