HDU-4848 Wow! Such Conquering! (回溯+剪枝)

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;
}

  

时间: 2024-10-28 06:06:47

HDU-4848 Wow! Such Conquering! (回溯+剪枝)的相关文章

hdu 4848 Wow! Such Conquering! (暴搜+强剪枝)

Wow! Such Conquering! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 0    Accepted Submission(s): 0 Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space

hdu 4848 Wow! Such Conquering!

Wow! Such Conquering! 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 Olymp

HDOJ 4848 Wow! Such Conquering!

dfs+减枝.... Wow! Such Conquering! Time Limit: 15000/8000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 651    Accepted Submission(s): 195 Problem Description There are n Doge Planets in the Doge Space. The conquero

hdu 4770 Lights Against Dudely(回溯)

题目链接:hdu 4770 Lights Against Dudely 题目大意:在一个N*M的银行里,有N*M个房间,'#'代表坚固的房间,'.'代表的是脆弱的房间,脆弱的房间个数不会超过15个,现在为了确保安全,要在若干个脆弱的房间上装灯,普通的灯是照亮{0, 0}, {-1, 0}, {0, 1}(和题目中坐标有点出入),然后可以装一个特殊的,可以照射 { {0, 0}, {0, 1}, {1, 0} }, { {0, 0}, {-1, 0}, {0, -1} }, { {0, 0}, {

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区间上的所以数变成最近的斐波那契数,相等的话取向下取. 解题思路:线段树,对于每个节点新增一个bool表示该节点以下的位置是否都是斐波那契数. #include <cstdio> #include <cstring> #include <cstdlib> #include <algor

HDU 4893 Wow! Such Sequence! 水线段树

思路: 线段树走起.. 写完这题就退役T^T 单点更新的时候直接找到这个点的最近fib,然后维护当前和 和 fib的和 #include<stdio.h> #include<string.h> #include<iostream> #include<math.h> #include<algorithm> #include<queue> #include<map> #include<set> #include&l

hdu 4893 Wow! Such Sequence!(线段树功能:单点更新,区间更新相邻较小斐波那契数)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4893 --------------------------------------------------------------------------------------------------------------------------------------------

hdu 4850 Wow! Such String!

这是当时西安邀请赛的题目,也就是因为这道题,我们无缘银牌... 其实这道题的大致想法当时我想出来了,就是我是想找到一条通过所有顶点的通路,顶点是所有的长度是4的子串.但是当时没有尝试搜索,以为不会这么简单就找到那条路.但是现在明白了,对于所有顶点度数为偶数的图,一定可以找到这样一条路的. 下面是代码 #include <iostream> #include <cstdio> #include <cstring> using namespace std; const in

HDU 4893 Wow! Such Sequence! (线段树)

Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 838    Accepted Submission(s): 245 Problem Description Recently, Doge got a funny birthday present from his new friend, Prot

hdu 4849 Wow! Such City!(dijstra)

题目链接:hdu 4849 Wow! Such City! 题目大意:有N个城市,给定计算两两城市距离的公式,然后求0到1~N-1的城市中,最短路径模掉M的最小值. 解题思路:先根据公式求出距离C矩阵,注意中间连乘3次的可能爆long long,然后用裸的dijstra算法求最短路. #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using name