POJ 2288 Islands and Bridges
来源:互联网 发布:淘宝分销 编辑:程序博客网 时间:2024/06/03 23:04
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.
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.
Note: A path may be written down in the reversed order. We still think it is the same path.
Sample Input
23 32 2 21 22 33 14 61 2 3 41 21 31 42 32 43 4
Sample Output
22 369 1
Source
Shanghai 2004
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
状压DP~
和前面两道思路差不多,但是因为多了一个三角形,所以要枚举前面两个点的情况,也就是要把f[k][i]变成f[k][i][j],这样每次用f[k][j][z]更新f[k][i][j]的时候就能判断z和i是否相连。
坑点QAQ:
1.结果可能是long long型;
2.走的两个方向算作一种,所以数量要除以二输出;
3.同一个f可能有多种走法,所以更新f的时候要同时记录num;
4.1要特判.
#include<cstdio>#include<cstring>#include<iostream>using namespace std;#define inf 999999999#define ll long longint t,n,m,x,y,tot;ll dis[14][14],a[14],f[8200][14][14],ans,num[8200][14][14],totnum;int main(){scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);tot=(1<<n)-1;ans=-1;totnum=0;memset(f,-1,sizeof(f));memset(num,0,sizeof(num));for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i][j]=inf;for(int i=1;i<=n;i++) scanf("%lld",&a[i]);if(n==1){printf("%lld 1\n",a[1]);continue;}while(m--){scanf("%d%d",&x,&y);dis[x][y]=dis[y][x]=a[x]*a[y];num[(1<<(x-1))+(1<<(y-1))][x][y]=num[(1<<(x-1))+(1<<(y-1))][y][x]=1;f[(1<<(x-1))+(1<<(y-1))][x][y]=f[(1<<(x-1))+(1<<(y-1))][y][x]=dis[x][y]+a[x]+a[y];}for(int k=1;k<=tot;k++) for(int i=1;i<=n;i++) if(k&(1<<(i-1))) { for(int j=1;j<=n;j++) if(k&(1<<(j-1)) && dis[i][j]!=inf) { for(int z=1;z<=n;z++) if(k&(1<<(z-1)) && dis[z][j]!=inf && z!=i && f[k^(1<<(i-1))][j][z]!=-1) { int kkz=f[k^(1<<(i-1))][j][z]+dis[i][j]+a[i]; if(dis[z][i]!=inf) kkz+=a[i]*a[j]*a[z]; if(kkz>f[k][i][j]) f[k][i][j]=kkz,num[k][i][j]=num[k^(1<<(i-1))][j][z]; else if(kkz==f[k][i][j]) num[k][i][j]+=num[k^(1<<(i-1))][j][z]; } }}for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) if(i!=j) if(f[tot][i][j]>ans) ans=f[tot][i][j],totnum=num[tot][i][j]; else if(f[tot][i][j]==ans) totnum+=num[tot][i][j];if(ans==-1) printf("0 0\n");else printf("%lld %lld\n",ans,totnum/2);}return 0;}
1 0
- poj 2288 Islands and Bridges
- poj 2288 Islands and Bridges
- poj 2288 Islands and Bridges
- poj 2288 Islands and Bridges
- POJ 2288 Islands and Bridges
- POJ 2288 Islands and Bridges
- poj -- 2288 Islands and Bridges
- POJ 2288 Islands and Bridges
- POJ 2288 Islands and Bridges
- POJ 2288 Islands and Bridges
- poj 2288Islands and Bridges
- POJ 2288 Islands and Bridges
- POJ 2288 Islands and Bridges
- POJ 2288 Islands and Bridges
- HDU 1668 POJ 2288 Islands and Bridges
- Islands and Bridges - POJ 2288 状压dp
- POJ 2288 Islands and Bridges(状态压缩)
- 状压DP poj 2288 Islands and Bridges
- jsp页面跳转用法和新窗口打开
- Ftp常用命令
- MySQL之存储过程
- FreeMacker使用总结,很全【转载】
- 第一行代码笔记 Notification通知
- POJ 2288 Islands and Bridges
- html-4
- 基于R语言的分类算法之决策树
- Android Studio 打包Apk文件,更改默认打包apk文件名称
- 对线性回归,logistic回归和一般回归的认识
- Linux网卡问题,
- 蓝桥杯 2n皇后问题
- Hessian学习(1)
- bzoj2440: [中山市选2011]完全平方数