HDU 4848

       这是一道个人认为很不错的搜索题。没有很难理解的地方,但是有不是很容易做对。

       题目里说要最快到达另一个星球,但是没有要求每个星球只能去一次,所以可以重复经过一些星球以此缩短时间,所以要先用floyd算法求出任意两点间的最短路,以此来代表一个星球到另一个星球的时间。在搜索方面,由于数据量很大,所以肯定要剪枝,可以从两个方面下手:1)如果到达一个星球的时间晚于到达每个星球的最晚时间,也不必往下搜索;2)如果发现搜索到最后的结果一定比当前时间还长,那么也不必往下搜索。

       当时做这题时,虽然两点都想到了,可是还是悲剧的TLE,主要是还有一些没想透。对于刚才说的第一点,应该进行如下处理:搜索到一个节点,遍历这个节点的下一层节点,如果有一个晚于Deadline,那么就不必往下搜索了。而第二点,则要进行预测,以当前节点的到达时间做为剩下待搜索节点的到达时间,计算所有节点到达时间之和,这个结果肯定比真实的到达时间之和小,如果这个结果都小于目前计算出的最短时间,那么就不必继续向下搜索。具体可以参考代码。

      (下面这段大家可以忽略)

       随便说一下题后感,当时对这题太自信,认为一定能找到问题,导致花了很多时间也没做出来,之后又因为在这题上投入太多时间而不舍得放弃,最后导致训练赛结果很惨,这个问题以后得改过来。此外,这道题测试数据不好自己出,遇到问题看了半天也不知道原因,最后通过重新写深搜函数的过程中才发现错误,今后如果遇到类似的情况,要更早重写部分代码用来找出粗心犯下的错误。


代码(G++):

#include <cstdlib>
#include <iostream>

#define MAX 39
using namespace std;

const int INF=0x3f3f3f3f;
int dl[MAX],map[MAX][MAX],art[MAX],ans,n;

void dfs(int u,int d,int sum)
{
     int i;
     if(d==n)
     { 
        ans=min(ans,sum);
        return;
     }
     if(sum+art[u]*(n-d)>ans) return;
     for(i=2;i<=n;i++)
     {
         if((art[i]==-1)&&(art[u]+map[u][i]>dl[i])) return;             
     }
     for(i=2;i<=n;i++) 
     {
         if(art[i]==-1)
         {
             art[i]=art[u]+map[u][i];
             dfs(i,d+1,sum+art[i]);  
             art[i]=-1;
         }           
     }
}

void floyd()
{
     int i,j,k;
     for(k=1;k<=n;k++)
     {
         for(i=1;i<=n;i++)
         {
            for(j=1;j<=n;j++)
            {
                map[i][j]=min(map[i][j],map[i][k]+map[k][j]);             
            }              
         }              
     }
}

int main()
{
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    
    int i,j;
    while(scanf("%d",&n)!=EOF)
    {
        for(i=1;i<=n;i++)
        {
            for(j=1;j<=n;j++)
            {
                scanf("%d",&map[i][j]);           
            }            
        }         
        for(i=2;i<=n;i++) scanf("%d",&dl[i]);
        
        floyd();
        
        memset(art,-1,sizeof(art));
        ans=INF;
        art[1]=0;
                
        dfs(1,1,0); 
        
        if(ans==INF) printf("-1\n"); 
        else printf("%d\n",ans);                    
    }
    //system("PAUSE");
    return 0;
}

题目( http://acm.hdu.edu.cn/showproblem.php?pid=4848):

Wow! Such Conquering!

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


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 T xy 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 Deadline x. 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 T xy . Then follows a single line containing n - 1 integers: Deadline 2 to Deadline n.
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
Hint
Explanation: In case #1: The Super Doge travels to Doge Planet 2 at the time of 8 and to Doge Planet 3 at the time of 12, then to Doge Planet 4 at the time of 16. The minimum sum of all arrival time is 36.

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值