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对进程组进行资源控制的 ...
随机推荐
- STL笔记(に)--vector容器
Vector 1.可变长的动态数组 2.需包含头文件#include<vector> (当然,如果用了万能头文件#include<bits/stdc++.h>则可忽略) 3.支 ...
- Dart Socket 与Java Socket连接
------------------------------------------------------------- Dart SocketClient----------------- ...
- php微信分享demo
php微信分享demo //定义JSSDK类 <?php class JSSDK { private $appId; private $appSecret; private $redis; pu ...
- batch-normalization为什么效果好
batch-normalization为什么效果好 深度学习中 Batch Normalization为什么效果好? - 龙鹏-言有三的回答 - 知乎 https://www.zhihu.com/qu ...
- PSTR、LPSTR等宏原型
1.首先介绍char.wchar_t ,宽字符wchar_t和窄字符char. 窄字符char了,大家都很清楚,就是8bit表示的byte,长度固定.char字符只能表示ASII码表中的256个字符, ...
- Codeforces Round #459 (Div. 2)-A. Eleven
A. Eleven time limit per test1 second memory limit per test256 megabytes Problem Description Eleven ...
- DFS:Prime Ring Problem(素数环)
解体心得: 1.一个回溯法,可以参考八皇后问题. 2.题目要求按照字典序输出,其实在按照回溯法得到的答案是很正常的字典序.不用去特意排序. 3.输出有个坑,就是在输出一串的最后不能有空格,不然要PE, ...
- [Hdu1693]Eat the Trees(插头DP)
Description 题意:在n*m(1<=N, M<=11 )的矩阵中,有些格子有树,没有树的格子不能到达,找一条或多条回路,吃完所有的树,求有多少种方法. Solution 插头DP ...
- 洛谷 P1736 创意吃鱼法(多维DP)
题目描述 回到家中的猫猫把三桶鱼全部转移到了她那长方形大池子中,然后开始思考:到底要以何种方法吃鱼呢(猫猫就是这么可爱,吃鱼也要想好吃法 ^_*).她发现,把大池子视为01矩阵(0表示对应位置无鱼,1 ...
- P1309 瑞士轮
题目背景 在双人对决的竞技性比赛,如乒乓球.羽毛球.国际象棋中,最常见的赛制是淘汰赛和循环赛.前者的特点是比赛场数少,每场都紧张刺激,但偶然性较高.后者的特点是较为公平,偶然性较低,但比赛过程往往十分 ...