HDU3538 A sample Hamilton path
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 503 Accepted Submission(s): 200
The next n line show you the graph, each line has n integers.
The
jth integers means the length to city j.if the number is -1 means there
is no way. If i==j the number must be -1.You can assume that the length
will not larger than 10000
Next m lines,each line has two integers a,b (0<=a,b<n) means the path must visit city a first.
The input end with EOF.
If you could not find a path, output -1
-1 2 4
-1 -1 2
1 3 -1
4 3
-1 2 -1 1
2 -1 2 1
4 3 -1 1
3 2 3 -1
1 3
0 1
2 3
5
I think that all of you know that a!=b and b!=0 =。=
/**/
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int INF=1e6;
const int mxn=;//2^22
int dp[mxn][];//[遍历状态][最后到达点]=最短路径
int dis[][];
int pre[];//每个点的前驱要求
int n,m;
int xn;
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
memset(pre,,sizeof pre);
int i,j;
xn=<<n;
for(i=;i<xn;i++)
for(j=;j<n;j++){
dp[i][j]=INF;
}
//init
for(i=;i<n;i++)
for(j=;j<n;j++){
scanf("%d",&dis[i][j]);
if(dis[i][j]==-)dis[i][j]=INF;
}
int u,v;
for(i=;i<=m;i++){//保存前驱要求
scanf("%d%d",&u,&v);
pre[v]|=(<<u);
}
dp[][]=;
for(i=;i<xn;i++){
for(j=;j<n;j++){
if(dp[i][j]==INF)continue;//i状态之前没走到
for(int k=;k<n;k++){
if(!(i&(<<j)))continue;//j不在已走过的集合中
if(i&(<<k))continue;//k在走过的集合中
if(pre[k]!=(i&pre[k]))continue;//k点前驱要求未满足
dp[i|(<<k)][k]=min(dp[i|(<<k)][k],dp[i][j]+dis[j][k]);
}
}
}
int ans=INF;
for(i=;i<n;i++)ans=min(ans,dp[xn-][i]);
if(ans>=INF) printf("-1\n");
else printf("%d\n",ans);
}
return ;
}
HDU3538 A sample Hamilton path的更多相关文章
- AGC018D Tree and Hamilton Path(树+树的重心)
题目大意: 给你一棵n个结点树,然后根据这棵树构造一个完全图,求完全图的一条最长的哈密顿路径. 构造方式是,完全图中的dis(u, v)就等于树上的u和v的距离. 题解: 这...这..不就是杜教的那 ...
- AtCoder Grand Contest 018 D - Tree and Hamilton Path
题目传送门:https://agc018.contest.atcoder.jp/tasks/agc018_d 题目大意: 给定一棵\(N\)个点的带权树,求最长哈密顿路径(不重不漏经过每个点一次,两点 ...
- D - Tree and Hamilton Path
题意 给一棵树,问一个排列,使得按顺序走过这些点的路径最长. N<=100000 解法 为了能让每条边被经过的次数达到上界, 我们首先找出重心, 然后容易得出一种排列方案,使得答案为以重心为根的 ...
- POJ2288 Islands and Bridges
Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...
- 【状压dp】Islands and Bridges
Islands and Bridges Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 11034 Accepted: 2 ...
- [poj2288] Islands and Bridges (状压dp)
Description Given a map of islands and bridges that connect these islands, a Hamilton path, as we al ...
- HDU 1668 Islands and Bridges
Islands and Bridges Time Limit: 4000ms Memory Limit: 65536KB This problem will be judged on HDU. Ori ...
- Python的平凡之路(5)
一.模块介绍 定义: 模块--用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名test.py,模块名test) 包—用来从逻辑上组织 ...
- centos7.2上实践cgoup
基本介绍 CGroups 是一种对进程资源管理和控制的统一框架,它提供的是一种机制,而具体的策略(Policy)是通过子系统(subsystem)来完成的.子系统是CGroups对进程组进行资源控制的 ...
随机推荐
- Python基础-Python注释
一.什么是注释.特性 1.一段文字性的描述,通过注释,可以解释和明确Python代码的功能,并记录将来要修改的地方. 2.当程序处理时,Python解释器会自动忽略,不会被当做代码进行处理 二.注释的 ...
- Mysql--7种Join查询
0.sql的执行顺序 手写顺序 机读顺序 总结 ①From:对from左边的表和右边的表计算笛卡尔积,产生虚拟表c1 ②On:对c1中的数据进行on过滤,只有符合过滤条件的数据记录才会记录在虚拟表c2 ...
- Ansible学习 Inventory文件
Ansible可同时操作属于一个组的多台主机,组与主机之间关系配置在inventory文件中,inventory默认的配置文件是/etc/ansible/hosts 1.在/etc/ansible/h ...
- 【PHP】php中json_decode()和json_encode()
1.json_decode() json_decode (PHP 5 >= 5.2.0, PECL json >= 1.2.0) json_decode — 对 JSON 格式的字符串进行 ...
- 50 道 CSS 基础面试题及答案
1 介绍一下标准的CSS的盒子模型?与低版本IE的盒子模型有什么不同的? 标准盒子模型:宽度=内容的宽度(content)+ border + padding + margin 低版本IE盒子模型:宽 ...
- php扩展开发-函数
我们首先找到快速上手文章里面关于函数定义的代码,以此说明然后开发PHP的函数 //php_myext.h PHP_FUNCTION(myext_hello);//函数申明,所有在myext.c文件定义 ...
- Matlab数据转化至python端,并写入数据库
因工作原因,一些获取的行业数据以已知的结构体存储在.mat文件中, 现需要将其存储在数据库中并且能够灵活调用至python dataframe里进行操作 原数据的一个例子如下 目标如上: 然后是转化代 ...
- vscode运行C/C++程序及配置
安装vscdoe,安装tdm-gcc-64编译器,这样可以自动把mingw的目录添加到环境变量中,其实安装其他编译器本版都可以,只要手动添加环境变量即可.平台win10-64位.此文参考了哔哩哔哩的配 ...
- 【Distinct Subsequences】cpp
题目: Given a string S and a string T, count the number of distinct subsequences of T in S. A subseque ...
- Android 数据存储-文件读写操作
本来已经写了一部分,后来发现这篇博客写的比我的好,就直接引用一下: https://www.cnblogs.com/LiHuiGe8/p/5604725.html