Wow! Such Conquering! HDU - 4848

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

大概题意

一共有n个星球,给出你两两星球之间要达到的距离,每个星球有一个最晚到达的时间期限,问你从第一个星球开始,在每个星球最晚到达时间期限之内,到达每个星球的时间和是多少,如果到不了输出-1。

先用最短路跑出来两两星球到达的最小时间是多少,然后开始爆搜。。。。直接搜的话肯定超时,所以要加上剪枝。。。。
根据我的代码:time是到达u点所需要的时间,alltime是当前的总时间。
1)因为要算的是时间和,所以在深搜里,比如:(经过a点到到a+1,a+2…n点的时间都要加上1-a的时间,也就是(n-a)*time),如果alltime是搜到a点时之前的总时间,a点之后必须要有时间是(n-a)*time,所以alltime+(n-a)*time>sum的话,就没有搜下去的必要的,一定会超过现在的最优时间。
2)因为每个星球都有一个最后的时间期限,那么如果超过这个时间期限的话,就一定要剪枝。(这个具体看代码)

#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
int e[45][45],n,ti[45];
int vis[45];
int sum;
void floyd()//求两两星球的最短时间
{
    for(int k=0; k<n; k++)
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
            {
                if(e[i][j]>e[i][k]+e[k][j])
                    e[i][j]=e[i][k]+e[k][j];
            }
}
void dfs(int cnt,int time,int alltime,int u)//time是当前到达u的时间,alltime是到达u时所有的总时间
{
    if(cnt==n)//搜到了n个点,取最优简化
    {
        sum=min(sum,alltime);
        return;
    }
    if(alltime+(n-cnt)*time>=sum)//剪枝
        return ;
    for(int i=1;i<n;i++)//剪枝
    {
        if(!vis[i])
        {
            if(time+e[u][i]>ti[i])//如果i点还未到达,time是1-u的所需时间,
                                 //通过u到达i的时间超过了i的最后期限,就没有必要搜下去了。
            return ;
        }
    }
    for(int i=1;i<n;i++)
    {
        if(!vis[i])
        {
            vis[i]=1;
            dfs(cnt+1,time+e[u][i],alltime+time+e[u][i],i);
            vis[i]=0;
        }
    }
}
int main()
{
    while(~scanf("%d",&n))
    {
        memset(vis,0,sizeof(vis));
        for(int i=0; i<n; i++)
            for(int j=0; j<n; j++)
                scanf("%d",&e[i][j]);
        for(int i=1;i<n;i++)
            scanf("%d",&ti[i]);
        floyd();
        sum=0x3f3f3f3f;
        vis[0]=1;
        dfs(1,0,0,0);
        if(sum<0x3f3f3f3f)
           printf("%d\n",sum);
        else
            printf("-1\n");
    }
    return 0;
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值