Problem Description
There are n Doge Planets in the Doge Space. The conqueror of Doge Space is Super Doge, who is going to inspect his Doge Army on all Doge Planets. The inspection starts from Doge Planet 1 where DOS (Doge Olympic Statue) was built. It takes Super Doge exactly Txy time to travel from Doge Planet x to Doge Planet y.
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.
 
Input
There are multiple test cases. Please process till EOF.
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.
 
Output
If some Deadlines can not be fulfilled, please output “-1” (which
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.
 
Sample Input
4
0 3 8 6
4 0 7 4
7 5 0 2
6 9 3 0
30 8 30
4
0 2 3 3
2 0 3 3
2 3 0 3
2 3 3 0
2 3 3
 
Sample Output
36
-1
 
题目大意:n(n<31)个节点,编号为0~n-1。起点是0节点,每个节点只能经过一次并且要在限制内的时间到达,现在要把到达每个点的时刻都加起来,求满足条件的最小时刻和。
题目分析:DFS。思路不难,但要剪枝。总结一下,剪枝无非有两条依据,一是根据题目中(不易发现)的条件剪枝,二是根据预测出的答案来剪枝,即根据通过当前的阶段解预测出来的近似当前方案最优解的解与已知最优解的优劣,来决定是否剪去。落实到这道题上来应该这么剪:1.如果在当前方案下,到达某个未到达过的点时的时刻已经超出时间限制,则剪枝;2.根据当前已用时间time和未到达过的点数num来预测出一个尽可能接近当前方案最优解的一个解time+time*num,当这个解比已经得到的最优解ans大时,则剪去。
 
做后感:这是一道好题,值得一做。
 
代码如下:
# 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! (回溯+剪枝)的更多相关文章

  1. hdu 4848 Wow! Such Conquering! (floyd dfs)

    Wow! Such Conquering! Problem Description There are n Doge Planets in the Doge Space. The conqueror ...

  2. HDU 4848 - Wow! Such Conquering!

    Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem Descriptio ...

  3. HDU 5113 Black And White 回溯+剪枝

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5113 Black And White Time Limit: 2000/2000 MS (Java/ ...

  4. HDU 2553 N皇后问题(回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398797 题意: 在N*N(N <= 10)的方格棋盘放置了N个皇后,使得它们不相互攻击(即 ...

  5. HDU4848 Wow! Such Conquering! —— dfs + 剪枝

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4848 题解: 一开始读错题目.以为每个点只能访问一遍.其实只要每个点都有被访问就可以了. 首先是用弗洛 ...

  6. HDU-4848 Wow! Such Conquering! 爆搜+剪枝

    Wow! Such Conquering! 题意:一个n*n的数字格,Txy表示x到y的时间.最后一行n-1个数字代表分别到2-n的最晚时间,自己在1号点,求到达这些点的时间和的最少值,如果没有满足情 ...

  7. hdu 4850 Wow! Such String! 欧拉回路

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4080264.html 题目链接:hdu 4850 Wow! Such String! 欧拉回 ...

  8. 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区间上的所以数变成 ...

  9. HDU1010 Tempter of the Bone(回溯 + 剪枝)

    本文链接:http://i.cnblogs.com/EditPosts.aspx?postid=5398734 题意: 输入一个 N * M的迷宫,这个迷宫里'S'代表小狗的位置,'X'代表陷阱,‘D ...

  10. HDU1016 Prime Ring Problem (回溯 + 剪枝)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5398684.html 题意: 给你一个数字N(N <= 20),要求你把这N个数组成一个环,环内的数字不能重复,左右 ...

随机推荐

  1. spring中对象转json过滤(jackson)

    spring自带的json解析器是jackson jackson注解 @JsonIgnore 此注解用于属性上,作用是进行JSON操作时忽略该属性. @JsonFormat 此注解用于属性上,作用是把 ...

  2. C++提供了四个转换运算符

    const_cast <new_type> (expression) static_cast <new_type> (expression) reinterpret_cast ...

  3. iOS xcode创建静态库封装自己的SDK及使用

    https://www.cnblogs.com/JustForHappy/p/5773039.html 一,静态库和动态库的区别在这里就不说了,个人感觉如果是自己封装提供别人下载的话应该是静态库比较方 ...

  4. Google's Machine Learning Crash Course #03# Reducing Loss

    Goal of training a model is to find a set of weights and biases that have low loss, on average, acro ...

  5. 在Android studio中到入Eclipse

    由于无法在AS中直接导入Eclipse的原始包,所以需要先把Eclipse的包导出成Gradle包,这个Gradle包可以别两个环境识别. 1.在Eclipse中导出Gradle包.选择需要从Ecli ...

  6. shell下如何删除文件的某一列

    答:cat file | awk '{$1=null;print $0}' (删除第一列)

  7. POJ 3630 Phone List(字符串前缀重复)题解

    Description Given a list of phone numbers, determine if it is consistent in the sense that no number ...

  8. Pairs Forming LCM (LCM+ 唯一分解定理)题解

    Pairs Forming LCM Find the result of the following code: ; i <= n; i++ )        for( int j = i; j ...

  9. C# 新Form各事件执行顺序

    1. 构造函数 2. Load() 3. Show() 4. Acticated()

  10. POJ 1625 Censored!(AC自动机+高精度+dp)

    http://poj.org/problem?id=1625 题意: 给出一些单词,求长度为m的串不包含这些单词的个数. 思路: 这道题和HDU 2243和POJ 2778是一样的,不同的是这道题不取 ...