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对进程组进行资源控制的 ...
随机推荐
- dom节点获取文本的方式
1. innerHTML innerHTML可以作为获取文本的方法也可以作为修改文本内容的方法 element.innerHTML 会直接返回element节点下所有的HTML化的文本内容 <b ...
- SAP系统管理中常见问题解答(转载)
1.如何查看SAP系统的位数? system——status看 Platform ID Platform 32-bit 64-bit --------------------------------- ...
- TO_DATS() AS ABAP_DATE
有的时候一个想不到的小问题,,才是致命的问题!
- django+xadmin在线教育平台(一)
大家好,此教程为在慕学网的实战教程Python升级3.6 强力Django+杀手级Xadmin打造在线教育平台的学习笔记,不对望指正! 使用Django+Xadmin打造在线教育平台(Python2, ...
- mount: no medium found on /dev/sr0 找不到介质
在VMware虚拟机中配置yum源时,执行 mount /dev/cdrom /mnt/cdrom 出现 mount: no medium found on /dev/sr0. 首先在/mnt 目录下 ...
- java util - 在java代码中执行javascript代码工具 rhino-1.7.7.jar
需要 rhino-1.7.7.jar 包 代码示例: package cn.java.mozilla.javascript; import org.mozilla.javascript.Context ...
- 04.VUE学习之v-text v-html
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 三次样条插值matlab实现
三次样条插值matlab实现 %三次样条差值-matlab通用程序 - zhangxiaolu2015的专栏 - CSDN博客 https://blog.csdn.net/zhangxiaolu201 ...
- CSU 1326: The contest(分组背包)
http://acm.csu.edu.cn/OnlineJudge/problem.php?id=1326 题意: n个题目,每个题目都有一个价值Pi和相对能力消耗Wi,但是有些题目因为太坑不能同时做 ...
- Assigning Logon Hours
Assigning Logon Hours Updated: March 28, 2003 Applies To: Windows Server 2003, Windows Server 2003 R ...