【状压dp】Islands and Bridges
| Time Limit: 4000MS | Memory Limit: 65536K | |
| Total Submissions: 11034 | Accepted: 2866 |
Description
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
Output
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
#include<iostream>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<stack>
#include<algorithm>
using namespace std;
#define LL long long
inline LL read(){
LL x=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) x=x*10+c-'0';
return x*f;
} const int MAXN=100001;
const int INF=999999;
int N,M;
int T;
LL C[101];
bool e[101][101];
LL dp[9001][15][15];
LL num[9001][15][15]; int main(){
T=read();
while(T--){
LL ans=0;
memset(e,false,sizeof(e));
memset(dp,-1,sizeof(dp));
memset(num,0,sizeof(num));
N=read(),M=read();
for(int i=1;i<=N;i++) C[i]=read();
for(int i=1;i<=M;i++){
int u=read(),v=read();
e[u][v]=e[v][u]=true;
}
if(N==1){
printf("%lld %lld\n",C[1],1);
continue;
}
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
if(i!=j&&e[i][j]) {
dp[(1<<(i-1))+(1<<(j-1))][i][j]=(long long)C[i]+C[j]+C[i]*C[j];
num[(1<<(i-1))+(1<<(j-1))][i][j]=1;
}
for(int i=0;i<(1<<N);i++){
for(int j=1;j<=N;j++){
if(!((i>>(j-1))&1)) continue;
for(int k=1;k<=N;k++){
if(k==j||!e[k][j]||!((i>>(k-1))&1)) continue;
for(int p=1;p<=N;p++){
if(!((i>>(p-1))&1)||p==k||p==j||!e[p][k]) continue;
if(dp[i-(1<<(j-1))][k][p]==-1) continue;
LL t1=(long long)dp[i-(1<<(j-1))][k][p]+C[j]*C[k]+C[j];
if(e[p][j]) t1=(long long)dp[i-(1<<(j-1))][k][p]+C[j]*C[k]+C[j]*C[k]*C[p]+C[j];
if(t1>dp[i][j][k]){
dp[i][j][k]=t1;
num[i][j][k]=num[i-(1<<(j-1))][k][p];
}
else if(dp[i][j][k]==t1) num[i][j][k]+=num[i-(1<<(j-1))][k][p];
}
}
}
}
LL tmp=0;
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++)
if(i!=j)
if(ans<dp[(1<<N)-1][i][j]) ans=dp[(1<<N)-1][i][j],tmp=num[(1<<N)-1][i][j];
else if(ans==dp[(1<<N)-1][i][j]) tmp+=num[(1<<N)-1][i][j];
printf("%lld %lld\n",ans,tmp/2);
}
}
【状压dp】Islands and Bridges的更多相关文章
- Islands and Bridges(POJ2288+状压dp+Hamilton 回路)
题目链接:http://poj.org/problem?id=2288 题目: 题意:求Hamilton 路径权值的最大值,且求出有多少条权值这么大的Hamilton路径. 思路:状压dp,dp[i] ...
- CH0103最短Hamilton路径 & poj2288 Islands and Brigdes【状压DP】
虐狗宝典学习笔记: 取出整数\(n\)在二进制表示下的第\(k\)位 \((n >> ...
- 状压DP天秀
状压DP,依靠的是把状态用某种压缩方式表示出来进而DP,大多数时候是二进制状压. 直接看例题吧. 一双木棋 九尾狐吃棉花糖 islands and bridges 愤怒的小鸟 芯片 ...
- BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS
BZOJ_3049_[Usaco2013 Jan]Island Travels _状压DP+BFS Description Farmer John has taken the cows to a va ...
- BZOJ 1087: [SCOI2005]互不侵犯King [状压DP]
1087: [SCOI2005]互不侵犯King Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 3336 Solved: 1936[Submit][ ...
- nefu1109 游戏争霸赛(状压dp)
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=1109 //我们校赛的一个题,状压dp,还在的人用1表示,被淘汰 ...
- poj3311 TSP经典状压dp(Traveling Saleman Problem)
题目链接:http://poj.org/problem?id=3311 题意:一个人到一些地方送披萨,要求找到一条路径能够遍历每一个城市后返回出发点,并且路径距离最短.最后输出最短距离即可.注意:每一 ...
- [NOIP2016]愤怒的小鸟 D2 T3 状压DP
[NOIP2016]愤怒的小鸟 D2 T3 Description Kiana最近沉迷于一款神奇的游戏无法自拔. 简单来说,这款游戏是在一个平面上进行的. 有一架弹弓位于(0,0)处,每次Kiana可 ...
- 【BZOJ2073】[POI2004]PRZ 状压DP
[BZOJ2073][POI2004]PRZ Description 一只队伍在爬山时碰到了雪崩,他们在逃跑时遇到了一座桥,他们要尽快的过桥. 桥已经很旧了, 所以它不能承受太重的东西. 任何时候队伍 ...
- bzoj3380: [Usaco2004 Open]Cave Cows 1 洞穴里的牛之一(spfa+状压DP)
数据最多14个有宝藏的地方,所以可以想到用状压dp 可以先预处理出每个i到j的路径中最小权值的最大值dis[i][j] 本来想用Floyd写,无奈太弱调不出来..后来改用spfa 然后进行dp,这基本 ...
随机推荐
- elementui table 多选 获取id
//多选相关方法 toggleSelection(rows) { if (rows) { rows.forEach(row => { this.$refs.multipleTable.toggl ...
- 头像截取 图片上传 js插件
先看一下整体效果 页面html <div class="row"> <div class="tabs-container"> <u ...
- vs调试 配置IISExpress允许局域网内部访问
内网可访问后,本机不能使用localhost 1.找到IISExpress的配置文件,位于 <文档>/IISExpress/config文件夹下,打开applicationhost.c ...
- 玩一下易语言 "和"字有多种读音,注定了它的重要性!!
变量名 类型 静态 数组 备注 拼音 文本型 0 测试的汉字 文本型 有几种发音 整数型 i 整数型 测试用的汉字 = “和” 有几种发音 = 取发音数目 ...
- Quartz的Properties文件解析
将可变信息放在properties文件是使配置更加灵活. 1.文档位置和加载顺序 1. StdSchedulerFactory默认加载quartz包下的quartz.properties文件,如果我们 ...
- 关于EditText.setText()无法显示的问题
将EditText在初始化后调用EditText.setSaveEnabled(false); 让Android 系统不保存值,这样就不会恢复了.
- 获取file中字段,写入到TXT文件中
一下代码省略了很多,哈哈哈 a.txt文件 uid,type,pointx,pointy,name1,9,911233763,543857286,区间测速起点3,9,906371086,5453354 ...
- vue-router 基础
安装 NPM npm install vue-router 如果在一个模块化工程中使用它,必须要通过 Vue.use() 明确地安装路由功能: import Vue from 'vue' import ...
- MyBatis批量插入数据(MySql)
由于项目需要生成多条数据,并保存到数据库当中,在程序中封装了一个List集合对象,然后需要把该集合中的实体插入到数据库中,项目使用了Spring+MyBatis,所以打算使用MyBatis批量插入,应 ...
- 关于ASP .NET Core在跨平台的linux ubuntun,SUSE ,Mac OS的发布的相关平台操作
https://www.microsoft.com/net/learn/get-started/linuxopensuse