poj 2288 Islands and Bridges

发布时间 2023-11-03 18:38:18作者: HL_ZZP
Islands and Bridges
Time Limit: 4000MS   Memory Limit: 65536K
Total Submissions: 15357   Accepted: 4098

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

额,明显的状压,n很小
两个答案应该是要分开计算的
先考虑路径的最大值怎么算

前两个条件其实我感觉已经有一点把贪心给杀绝了,第三个关于环的条件可以说是一边杀贪心一边增加难度了。。
既然有了第三个环的条件,那肯定状态是要记录一个上一个点是哪一个了,这样才能统计答案
额,那想想好像真的没什么难度啊。。
怎么会这样呢?
又是一到入门题

md对拍拍了两万组没有结果。。。
交上去还是wa。。
算了算我A了
代码放这了吧

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <cmath>
#include <math.h>
#include <string.h>
#define ll long long
using namespace std;
inline ll read() {
    char c=getchar();ll a=0,b=1;
    for(;c<'0'||c>'9';c=getchar())if(c=='-')b=-1;
    for(;c>='0'&&c<='9';c=getchar())a=a*10+c-48;return a*b;
}
ll n,m,head[401],tot,a[14],f[(1<<13)][14][14],tur[14][14],dp[(1<<13)][14][14];//״̬£¬µ±Ç°µÄ,ÉÏÒ»¸ö
struct edge
{
    ll next,to;
}e[401];
inline void add(ll i,ll j)
{
    e[++tot].next=head[i];
    e[tot].to=j;
    head[i]=tot;
}
int main()
{
    freopen("1.in","r",stdin);
    freopen("2.out","w",stdout);
    ll T=read();
    while(T--)
    {
        n=read();m=read();
        tot=0;
        memset(head,0,sizeof(head));
        memset(e,0,sizeof(e));
        memset(tur,0,sizeof(tur));
        memset(f,-1,sizeof(f));
        memset(dp,0,sizeof(dp));
        for(ll i=1;i<=n;i++)
        {
            a[i]=read();
        }
        for(ll i=1;i<=m;i++)
        {
            ll x=read(),y=read();
            add(x,y);add(y,x);
            tur[x][y]=1;tur[y][x]=1;
        }
        if(n==1)
        {
            cout<<1<<' '<<a[1]<<endl;
            continue;
        }
        for(ll i=1;i<=n;i++)
        {
            for(ll j=1;j<=n;j++)
            {
                if(tur[i][j]==0||i==j)continue;
                f[(1<<(n-i))|(1<<(n-j))][i][j]=a[i]+a[j]+a[i]*a[j];
                dp[(1<<(n-i))|(1<<(n-j))][i][j]=1;
            }
        }
        for(ll j=0;j<1<<n;j++)
        {
            for(ll fi=1;fi<=n;fi++)
            {
                if((j&(1<<(n-fi)))==0)continue;
                for(ll se=1;se<=n;se++)
                {
                    if((j&(1<<(n-se)))==0)continue;if(f[j][fi][se]==-1)continue;//תÒƲ»ºÏ·¨
                    if(fi==se)continue;if(tur[fi][se]==0)continue;
                    for(ll h=head[fi];h!=0;h=e[h].next)
                    {
                        ll u=e[h].to;
                        if(u==fi||u==se||(j&(1<<(n-u))))continue;
                        if((j&(1<<(n-u)))==0)
                        {
                            if(tur[u][se]==1)
                            {
                                ll temp=f[j][fi][se]+a[u]+a[u]*(a[fi])+(a[u]*a[fi]*a[se]);
                                if(temp>f[j|(1<<(n-u))][u][fi])
                                {
                                    f[j|(1<<(n-u))][u][fi]=temp;
                                    dp[j|(1<<(n-u))][u][fi]=dp[j][fi][se];
                                }
                                else
                                if(temp==f[j|(1<<(n-u))][u][fi])
                                    dp[j|(1<<(n-u))][u][fi]+=dp[j][fi][se];
                            }
                            else
                            {
                                ll temp=f[j][fi][se]+a[u]*(a[fi]+1);
                                if(temp>f[j|(1<<(n-u))][u][fi])
                                {
                                    f[j|(1<<(n-u))][u][fi]=temp;
                                    dp[j|(1<<(n-u))][u][fi]=dp[j][fi][se];
                                }
                                else
                                if(temp==f[j|(1<<(n-u))][u][fi])
                                    dp[j|(1<<(n-u))][u][fi]+=dp[j][fi][se];
                            }
                        }
                    }
                }
            }
        }
        ll ans1=0,ans2=0;
        for(ll i=1;i<=n;i++)
        {
            for(ll j=1;j<=n;j++)
            {
                if(tur[i][j]==0||i==j)continue;
//                cout<<f[((1<<n)-1)][i][j]<<' '<<dp[(1<<n)-1][i][j]<<' '<<i<<' '<<j<<endl;
                if(ans1<f[(1<<n)-1][i][j])
                {
                    ans1=f[(1<<n)-1][i][j];
                    ans2=dp[(1<<n)-1][i][j];
                }
                else
                if(ans1==f[(1<<n)-1][i][j])
                    ans2+=dp[(1<<n)-1][i][j];
            }
        }
        cout<<ans1<<' '<<ans2/2<<endl;
        
    }
    return 0;
}

服了。。感觉什么都没学到。。
浪费了我一个下午时间。。
算是增加熟练度吧。。。