哈密顿回路

Time Limit: 15 Sec  Memory Limit: 256 MB

Description

  

Input

  

Output

  

Sample Input

  4 10
  0 3 2 1
  3 0 1 3
  2 1 0 2
  1 3 2 0

Sample Output

  possible
  

HINT

  

Main idea

  判断能否找到一条长度为L的哈密顿回路。

Solution

  我们直接使用Meet in middle,记录M[t][opt]表示以 t 结尾,到的点为 opt 的长度集合。然后暴力合并即可。

Code

 #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cmath>
#include<vector>
using namespace std;
typedef long long s64; const int ONE = ;
const int Base = ;
const int INF = ; int n;
int lenA,lenB;
s64 E[][];
int Num[ONE],vis[ONE],a[ONE];
s64 Ans,L; vector <s64> M[][<<]; int get()
{
int res=,Q=; char c;
while( (c=getchar())< || c>)
if(c=='-')Q=-;
if(Q) res=c-;
while((c=getchar())>= && c<=)
res=res*+c-;
return res*Q;
} void Dfs(int u,int opt,int T,s64 Val)
{
if( Val > L) return;
if( T==lenA || T==lenB ) M[u][opt].push_back(Val);
if( T==max(lenA,lenB) ) return;
for(int v=; v<=n; v++)
{
int now = opt | (<<v-);
if(now != opt) Dfs(v,now,T+,Val+E[u][v]);
}
} int main()
{
n=get(); cin>>L;
lenA = (n+)/; lenB = (n+)-lenA;
for(int i=; i<=n; i++)
for(int j=; j<=n; j++)
{
scanf("%lld",&E[i][j]);
} Dfs(,,,); int All = (<<n)-; for(int u=;u<=All;u++)
for(int t=;t<=n;t++)
sort(M[t][u].begin(),M[t][u].end()); for(int u=;u<=All;u++)
for(int t=;t<=n;t++)
{
if(! (u&(<<t-)) ) continue;
int v = All^u | | (<<t-);
int A_size = M[t][u].size(), B_size = M[t][v].size()-;
for(int i=;i<A_size;i++)
{
s64 A = M[t][u][i]; while(B_size >= && M[t][v][B_size] + A > L) B_size--;
if(B_size < ) break;
if(M[t][v][B_size] + A == L)
{
printf("possible");
exit();
}
}
} printf("impossible");
}

【Foreign】哈密顿回路 [MIM]的更多相关文章

  1. Constraint6:更新外键约束(Foreign Key Constraint)的引用列

    在SQL Server中,表之间存在引用关系,引用关系通过创建外键约束(Foreign Key Constraint)实现.如果一个Table中的column被其他Table引用,那么该表是参考表,或 ...

  2. MySQL主从复制中断,报“Error on master: message (format)='Cannot delete or update a parent row: a foreign key constraint fails' error code=1217” 错误

    前几天,发现从库挂了,具体报错信息如下: 分析思路 1. 因为我采用的是选择性复制,只针对以下几个库进行复制: card,upay,deal,monitor,collect.所以,不太可能出现对于sa ...

  3. ORA-02266: unique/primary keys in table referenced by enabled foreign keys

    在数据库里面使用TRUNCATE命令截断一个表的数据时,遇到如下错误 SQL >TRUNCATE TABLE ESCMOWNER.SUBX_ITEM ORA-02266: unique/prim ...

  4. SQL Server 2008 R2——TRUNCATE TABLE 无法截断表 该表正由 FOREIGN KEY 约束引用

    =================================版权声明================================= 版权声明:原创文章 禁止转载  请通过右侧公告中的“联系邮 ...

  5. 【MySQL】Create table 以及 foreign key 删表顺序考究。

    1.以下是直接从数据库导出的建表语句. 1 -- ---------------------------- 2 -- Table structure for files 3 -- ---------- ...

  6. MYSQL外键(Foreign Key)的使用

    在MySQL 3.23.44版本后,InnoDB引擎类型的表支持了外键约束.外键的使用条件:1.两个表必须是InnoDB表,MyISAM表暂时不支持外键(据说以后的版本有可能支持,但至少目前不支持): ...

  7. SQL PRIMARY KEY 约束\SQL FOREIGN KEY 约束\SQL CHECK 约束

    SQL PRIMARY KEY 约束 PRIMARY KEY 约束唯一标识数据库表中的每条记录. 主键必须包含唯一的值. 主键列不能包含 NULL 值. 每个表都应该有一个主键,并且每个表只能有一个主 ...

  8. Netsuite > Foreign Currency Revaluation 外币评估

    MENU: Transactions > Financial > Revalue Open Currency Balances 使用频率: - 每个月月底,结账前, 手工操作. - 或者在 ...

  9. 无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用。

    在删除northwindcs表时,发生报错,消息 3726,级别 16,状态 1,第 2 行,无法删除对象 '产品',因为该对象正由一个 FOREIGN KEY 约束引用.此时判断是因为有其他表的外键 ...

随机推荐

  1. java中array,arrayList,iterator;

    Array        String []a = new String[10] ;  a[0] = "test" ; String []a = new String[]{&quo ...

  2. Android ANR 分析

    首先贴一下trace 文件 Process: com.oppo.reader PID: 20358 Time: 2933175644_1545041895232 Flags: 0x38d83e44 P ...

  3. spring读取properties和其他配置文件的几种方式

    1.因为spring容器的一些机制,在读取配置文件进行数据库的配置等等是很有必要的,所以我们要考虑配置文件的的读取方式以及各个方式的实用性 2.配置文件的读取方式我这里介绍2种,目的是掌握这2种就可以 ...

  4. 【多校联合】(HDU6045)Is Derek lying?

    分析 之前没有想到题目解法,看了题解才会,记录一下思考过程. 这条题目的实质是,在满足合法的情况下,有没有a和d的可行解?也就是说,不要仅仅附在表面的思考逻辑条件,而是要思考实际的数学表达. 转化为数 ...

  5. Spring框架中ModelAndView、Model、ModelMap的区别

    转自:http://blog.csdn.net/liujiakunit/article/details/51733211 1. Model Model 是一个接口, 其实现类为ExtendedMode ...

  6. 类和实例属性的查找顺序 mro查找

    如果多个类继承父类,然后又被多个类继承这种复杂的问题,可以使用 mro方法 例如: class A: pass class C(D): pass class B(D): pass class A(B, ...

  7. CentOs 版本名字说明

    What images are in this directory CentOS-6.3-x86_64-netinstall.iso This is the network install and r ...

  8. Mysql性能优化一:SQL语句性能优化

    这里总结了52条对sql的查询优化,下面详细来看看,希望能帮助到你 1, 对查询进行优化,应尽量避免全表扫描,首先应考虑在 where 及 order by 涉及的列上建立索引. 2,应尽量避免在 w ...

  9. git 创建分支并提交到服务器对应的新分支

    1.切换到源分支 git checkout test 2.在源分支的基础上创建新分支 git branch test1 3.提交到远程分支 git pull 会自动提示下面的命令 git pull - ...

  10. css3弹性盒子模型之box-flex

    css3弹性盒子模型之box-flex 浏览器支持 目前没有浏览器支持 box-flex 属性. Firefox 支持替代的 -moz-box-flex 属性. Safari.Opera 以及 Chr ...