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个数组成一个环,环内的数字不能重复,左右 ...
随机推荐
- 原 用Tomcat服务器配置https双向认证过程实战
什么是https? 百度百科足够解释它:http://baike.baidu.com/view/14121.htm 工具:keytool (Windows下路径:%JAVA_HOME%/bin/key ...
- 随意给一组数,找出满足一下条件的a[i],a[i]左边的数小于等于a[i],a[i]右边的数大于等于a[i]
使用一个额外数组记录每个数后面的最小值是多少,一个额外数组记录一个数前面的最大值是多少,当然,为了减少空间复杂度,可以使用一个数字记录一个数字前面最大值是多少.算法如下: public List< ...
- 访问Hsql .data数据库文件
一.Hsql简介: hsql数据库是一款纯Java编写的免费数据库,许可是BSD-style的协议. 仅一个hsqldb.jar文件就包括了数据库引擎,数据库驱动,还有其他用户界面操作等内容.下载地址 ...
- pollard_rho 学习总结 Miller_Rabbin 复习总结
吐槽一下名字,泼辣的肉..OwO 我们知道分解出一个整数的所有质因子是O(sqrt(n)/ln(n))的 但是当n=10^18的时候就显得非常无力的 这个算法可以在大概O(n^(1/4))的时间复杂度 ...
- Python Web学习笔记之面试TCP的15个问题
网络协议那么多,为什么面试喜欢问TCP?原因无外乎两个:1.TCP协议直接与进程打交道,写网络程序要用:2.TCP协议设计十分精巧,在一个不可靠的IP网络上实现了可靠传输,因为精巧,掌握TCP的原理自 ...
- 一个改写MBR的例子
前言 想要对MBR类的病毒进行一下研究与学习,在此期间,看了很多资料,其中帮助最大的就是金龟子学姐和willj学长发表的文章.一个从源码与实现角度来讲了一下,另外一个从反病毒角度来分析. 功能描述 ...
- Sql 最简单的Sqlserver连接
string name = txtUserName.Text.Trim();//移除用户名前部和后部的空格 string pwd = txtUserPwd.Text.Trim();//移除密码前部和后 ...
- Git operate
新建远程分支和删除 https://www.jianshu.com/p/ea1dab2de419 使用git branch -a查看所有分支 远程先开好分支然后拉到本地 git checkout -b ...
- MVC---- DataSet 页面遍历
后台代码: public override ActionResult Index() { DataSet ab = custapp.GetCustomerFollows(); ViewData[&qu ...
- 【Django】【六】接口自动化测试框架
我的源码地址:https://github.com/woshixiaoyu202017/djangoTest 详细构建步骤如下 1. 生成新的测试数据库的数据库表结构guest_test 2. 数据库 ...