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对进程组进行资源控制的 ...
随机推荐
- 【linux】CPU,内存对网站的影响
如果读写非常多,建议内存大点 如果涉及到的计算非常多,那就升级CPU
- 以太坊国内节点大全(ropsten)
admin.addPeer('enode://2d1e1f1242c3b54ea56046f74f15943f47ab410e3c0b82bffb501793ebb19e147f8f0e63d01c2 ...
- JZOJ 4742. 单峰
Description Input Output Sample Input 2 Sample Output 2 Data Constraint 做法:打标可以发现这道题是结论题,答案为2^(n-1), ...
- Ecshop里添加多个h1标题
目录 功能: 思路: 效果: pageheader_list.htm里 product_sn_list.htm模板里 控制器里 功能: 点击页面右边的两个按钮,切换下面的<div class=& ...
- Python9-条件-定时器-队列-day40
复习 线程 线程是进程中的执行单位 线程是cpu执行的最小单位 线程之间资源共享 线程的开启和关闭以及切换的时间开销远远小于进程 线程本身可以在同一时间使用多个cpu,python与线程 由于cpyt ...
- Olympic Class Ships【奥林匹克级邮轮】
Olympic Class Ships You probably know about the Titanic, but it was actually just noe of three state ...
- UVA_1025 a Spy in the Metro 有向无环图的动态规划问题
应当认为,有向无环图上的动态规划问题是动态规划的基本模型之一,对于某个模型,如果可以转换为某一有向无环图的最长.最短路径问题,则可以套用动态规划若干方法解决. 原题参见刘汝佳紫薯267页. 在这个题目 ...
- poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7825 Accepted: 3068 Descri ...
- 在 Amazon AWS 搭建及部署网站:(二)安装、配置软件,启动网站
现在,我们已经有了一台EC2主机,具备了基本的硬件环境.下面,开始软件环境的配置. 第一步:连接服务器 后面所有的一切,都需要在SSH终端窗口操作.首先,我们需要一个SSH客户端.PuTTY是很常用的 ...
- C#入门篇5-8:流程控制语句 break语句
#region break语句 public class Breakapp { public static void Fun1() { //计算1+2+…+100的求和程序,打印显示每次循环计算的结果 ...