首页 > 其他分享 >poj 2288 Islands and Bridges

poj 2288 Islands and Bridges

时间:2023-11-03 18:45:08浏览次数:42  
标签:2288 poj Vi each Hamilton path Islands include best

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

Shanghai 2004

额,明显的状压,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;
}

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

 

标签:2288,poj,Vi,each,Hamilton,path,Islands,include,best
From: https://www.cnblogs.com/HLZZPawa/p/17808192.html

相关文章

  • 【POJ 1731】Orders 题解(全排列)
    描述商店经理按照标签的字母顺序对各种商品进行了分类。所有标签以同一字母开头的种类都存放在标有该字母的同一仓库(即在同一建筑物内)。在白天,商店经理接收并预订要从商店发货的货物订单。每个订单只需要一种货物。商店经理按照预订的顺序处理请求。你事先知道今天商店经理必须处......
  • 【POJ 1256】Anagram 题解(全排列)
    描述你要编写一个程序,从一组给定的字母中生成所有可能的单词。示例:给定单词“abc”,您的程序应该-通过探索这三个字母的所有不同组合-输出单词“abc”、“acb”、“bac”、“bca”、“cab”和“cba”。在输入文件中的单词中,某些字母可能出现多次。对于给定的单词,您的程序不应多次......
  • poj1185炮兵阵地
    炮兵阵地TimeLimit:2000MS MemoryLimit:65536KTotalSubmissions:43084 Accepted:16457Description司令部的将军们打算在N*M的网格地图上部署他们的炮兵部队。一个N*M的地图由N行M列组成,地图的每一格可能是山地(用"H"表示),也可能是平原(用"P"表示),如下图。......
  • poj 2411 状压dp入门题
    Mondriaan'sDreamTimeLimit:3000MS MemoryLimit:65536KTotalSubmissions:29096 Accepted:15505DescriptionSquaresandrectanglesfascinatedthefamousDutchpainterPietMondriaan.Onenight,afterproducingthedrawingsinhis�......
  • java 开发中VO、PO、DO、DTO、BO、QO、DAO、POJO各种傻傻分不清
    VO(ValueObject):值对象,主要用于业务层之间的数据传递,是方法返回类型。例如,一个方法需要返回用户的信息,可以创建一个UserVO,包含用户的姓名、年龄等信息。PO(PersistentObject):持久化对象,用于表示数据库中的一条记录,与数据库表一一对应。例如,数据库中有一个用户表,可以创建一个Use......
  • 浅析POJO、DTO、DO、VO、BO、PO、Entity
    名词解释领域模型中的实体类分为四种模型:VO、DTO、DO和PO,各种实体类用于不同业务层次间的交互,并会在层次内实现实体类之间的转化。新项目使用了新的框架和开发规范,特意集体讨论了DTO,DO,VO,BO,POJO,PO和Entity以及DAO、Model和View的基本概念和使用场景,为了深入理解,这里整理为一篇笔记......
  • POJO
    POJO(PlainOrdinaryJavaObject)简单的Java对象,实际就是普通JavaBeans,是为了避免和EJB混淆所创造的简称。使用POJO名称是为了避免和EJB混淆起来,而且简称比较直接.其中有一些属性及其gettersetter方法的类,没有业务逻辑,有时可以作为VO(value-object)或dto(DataTransformObjec......
  • poj 1742 coins
    DescriptionPeopleinSilverlandusecoins.TheyhavecoinsofvalueA1,A2,A3...AnSilverlanddollar.OnedayTonyopenedhismoney-boxandfoundthereweresomecoins.Hedecidedtobuyaverynicewatchinanearbyshop.Hewantedtopaytheexactprice(wi......
  • poj3666
    #include<iostream>#include<stdio.h>#include<algorithm>#include<cmath>#include<math.h>#include<map>#include<string.h>#definelllonglongusingnamespacestd;intn,a[2001],f[2001][2001],b[2001];inli......
  • poj2279
    Mr.Young'sPicturePermutationsTimeLimit:1000MS MemoryLimit:65536KTotalSubmissions:5841 Accepted:1860DescriptionMr.Youngwishestotakeapictureofhisclass.Thestudentswillstandinrowswitheachrownolongerthanth......