HDU-4848 Wow! Such Conquering! (回溯+剪枝)
With the ambition of conquering other spaces, he would like to visit
all Doge Planets as soon as possible. More specifically, he would like
to visit the Doge Planet x at the time no later than Deadlinex.
He also wants the sum of all arrival time of each Doge Planet to be as
small as possible. You can assume it takes so little time to inspect his
Doge Army that we can ignore it.
Each test case contains several lines. The first line of each test
case contains one integer: n, as mentioned above, the number of Doge
Planets. Then follow n lines, each contains n integers, where the y-th
integer in the x-th line is Txy . Then follows a single line containing n - 1 integers: Deadline2 to Deadlinen.
All numbers are guaranteed to be non-negative integers smaller than
or equal to one million. n is guaranteed to be no less than 3 and no
more than 30.
means the Super Doge will say “WOW! So Slow! Such delay! Much Anger! . .
. ” , but you do not need to output it), else output the minimum sum of
all arrival time to each Doge Planet.
# include<iostream>
# include<cstdio>
# include<cstring>
# include<algorithm>
using namespace std;
# define LL long long
const int INF=1<<30;
int mp[35][35],n,a[35],ans,vis[35];
void floyd()
{
for(int k=0;k<n;++k)
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
mp[i][j]=min(mp[i][j],mp[i][k]+mp[k][j]);
}
void dfs(int cur,int time,int att,int num)
{
if(num==n){
ans=min(ans,att);
return ;
}
if(att+(n-num)*time>=ans)
return ;
for(int i=1;i<n;++i)
if(!vis[i]&&time+mp[cur][i]>a[i])
return ;
for(int i=1;i<n;++i){
if(vis[i])
continue;
vis[i]=1;
dfs(i,time+mp[cur][i],att+time+mp[cur][i],num+1);
vis[i]=0;
}
}
int main()
{
while(~scanf("%d",&n))///在这里如果用“!=EOF”会超时。
{
for(int i=0;i<n;++i)
for(int j=0;j<n;++j)
scanf("%d",&mp[i][j]);
floyd();
a[0]=0;
for(int i=1;i<n;++i)
scanf("%d",a+i);
memset(vis,0,sizeof(vis));
ans=INF;
vis[0]=1;
dfs(0,0,0,1);
if(ans!=INF)
printf("%d\n",ans);
else
printf("-1\n");
}
return 0;
}
HDU-4848 Wow! Such Conquering! (回溯+剪枝)的更多相关文章
- hdu 4848 Wow! Such Conquering! (floyd dfs)
Wow! Such Conquering! Problem Description There are n Doge Planets in the Doge Space. The conqueror ...
- HDU 4848 - Wow! Such Conquering!
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descriptio ...
- HDU 5113 Black And White 回溯+剪枝
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...
- HDU 2553 N皇后问题(回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398797 题意: 在N*N(N <= 10)的方格棋盘放置了N个皇后,使得它们不相互攻击(即 ...
- HDU4848 Wow! Such Conquering! —— dfs + 剪枝
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4848 题解: 一开始读错题目.以为每个点只能访问一遍.其实只要每个点都有被访问就可以了. 首先是用弗洛 ...
- HDU-4848 Wow! Such Conquering! 爆搜+剪枝
Wow! Such Conquering! 题意:一个n*n的数字格,Txy表示x到y的时间.最后一行n-1个数字代表分别到2-n的最晚时间,自己在1号点,求到达这些点的时间和的最少值,如果没有满足情 ...
- hdu 4850 Wow! Such String! 欧拉回路
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4080264.html 题目链接:hdu 4850 Wow! Such String! 欧拉回 ...
- hdu 4893 Wow! Such Sequence!(线段树)
题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 改动k的为值添加d 2 l r, 查询l到r的区间和 3 l r. 间l到r区间上的所以数变成 ...
- HDU1010 Tempter of the Bone(回溯 + 剪枝)
本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...
- HDU1016 Prime Ring Problem (回溯 + 剪枝)
本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...
随机推荐
- python之路----面向对象中的内置函数
property属性 什么是特性property property是一种特殊的属性,访问它时会执行一段功能(函数)然后返回值 例一:BMI指数(bmi是计算而来的,但很明显它听起来像是一个属性而非方法 ...
- MySQL-MHA集群部署(binlog复制)
MHA的理论知识网上有很多教程,这里不会说明:仅推荐博客链接! MHA的理论说明:http://www.ywnds.com/?p=8094 MHA的安装包需要在google上面下载,或者就是csdn上 ...
- python模块-json、pickle、shelve
json模块 用于文件处理时其他数据类型与js字符串之间转换.在将其他数据类型转换为js字符串时(dump方法),首先将前者内部所有的单引号变为双引号,再整体加上引号(单或双)转换为js字符串:再使用 ...
- xargs 原理&使用
1. 简介 之所以能用到这个命令,是由于很多 linux 命令不支持用管道传递参数,例如 find /sbin -perm +700 | ls -l 这个命令是错误的 find /sbin -perm ...
- 使用liner、feather、multiband对已经拼接的数据进行融合
所谓"blend",英文解释为“vt. 混合vi. 混合:协调n. 混合:掺合物”这里应该理解为是图像数据的融合.这是“识别->对准->融合”的最后一步.融合是决定拼接 ...
- Email移动的原理
1.从数据库中得到被移动邮件的uid: 2.选择移动邮件所属folder,即SelectFolder; 3.调用copymessage(path,vmime::net::messageset::byU ...
- JAVA I/O(三)内存映射文件
<Java编程思想>中对内存映射文件有详细的介绍,此处仅做简单记录和总结.内存映射文件允许创建和修改因为太大而不能放入内存的文件. 1. 内存映射文件简单实例 import java.io ...
- 在Android studio中到入Eclipse
由于无法在AS中直接导入Eclipse的原始包,所以需要先把Eclipse的包导出成Gradle包,这个Gradle包可以别两个环境识别. 1.在Eclipse中导出Gradle包.选择需要从Ecli ...
- python_paramiko_SSHException Invalid requirement, parse error at
不加sleep(0.5)会出现SSHException: Invalid requirement, parse error at " '' "问题,原因暂时未知. 结论如下 如果不 ...
- Centos7.2 修改网卡名称
查看ip [root@localhost network-scripts]# ip addr : lo: <LOOPBACK,UP,LOWER_UP> mtu qdisc noqueue ...