哈密顿回路

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. IAR工程名修改

    修改.dep..ewd..ewp..eww四个文件的文件名 删除.ewt文件(如果存在) 记事本打开.eww文件,修改<path></path>间的.ewp文件名 打开工程,打 ...

  2. 基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下)

    基于Mysql-Proxy实现Mysql的主从复制以及读写分离(下) 昨天谈到了Mysql实现主从复制,但由于时间原因并未讲有关读写分离的实现,之所以有读写分离,是为了使数据库拥有双机热备功能,至于双 ...

  3. Prolog奇怪奇妙的思考方式

    今天在<七周七语言>中接触到了prolog,发现它的编程模式和思考方式的确比较奇怪,但同时也非常奇妙,值得学习一下. 1. prolog语言介绍     和SQL一样,Prolog基于数据 ...

  4. Ubuntu下使用Git_5

    还欠大家最后一篇Git的学习. Git的下一个内容,标签,标签是为了更方便的参考提交而给他表上通俗易懂的名称 Git可以使用两种标签,轻标签和注解标签,打上的标签是固定的,不能向分支那样可以移动位置, ...

  5. 第五篇Python基本数据类型

    运算符 1. 结果是具体的值:算数运算符和赋值运算符 算数运算符:+.-.*./.**(幂).%(取余).//(取商) print(3-2) # 减法 print(3*2) # 乘法 print(3/ ...

  6. CSP201509-1:数组分段

    引言:CSP(http://www.cspro.org/lead/application/ccf/login.jsp)是由中国计算机学会(CCF)发起的“计算机职业资格认证”考试,针对计算机软件开发. ...

  7. cut 与 awk

    cut和awk都能分割显示需要的内容 但在需要以空格为分隔符的情况下: # free -m|grep Mem Mem: cut是以单一空格为分隔符的: # free -m|grep Mem|cut - ...

  8. Go基础篇【第4篇】: 内置库模块 bufio

    bufio包实现了有缓冲的I/O.它包装一个io.Reader或io.Writer接口对象,创建另一个也实现了该接口,且同时还提供了缓冲和一些文本I/O的帮助函数的对象. 即:为了解决CPU与磁盘IO ...

  9. Elasticsearch 监控和部署

    Elasticsearch: ! [ https://elasticsearch.cn/book/elasticsearch_definitive_guide_2.x/_cluster_health. ...

  10. web online ide &web online editor & web online playground & web online runtime

    web online ide &web online editor web online ide &web online editor & web online playgro ...