hdu 4848 Wow! Such Conquering!(floyd+dfs)

115 篇文章 0 订阅
46 篇文章 0 订阅

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


ps:题意稍不注意就看错0.0,题目要求的是能到达每一个星球的时间点之和(最可耻的是题目的Hint给的还是错误的。。。)

思路:
先用floyd松弛一下点与点之间的最短距离,然后再dfs暴搜最小的和(有两个剪枝,一是每一步的时间和sum必须小于结果ans,二是到达每一步的时间都必须要保证到达后面点的所有时间都来得及)

代码:

#include<stdio.h>

#define min(a,b) (a<b?a:b)
const int inf=0x3f3f3f3f;
int map[31][31],dead[31],vis[31];
int n,ans;

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

int judge(int x,int step)
{
    for(int i=2; i<=n; i++)
        if(!vis[i]&&step+map[x][i]>dead[i])
            return 0;
    return 1;
}

void dfs(int x,int step,int cnt,int sum)
{
    if(step>=ans||sum>=ans||!judge(x,step))
        return ;
    if(cnt==n)
    {
        ans=min(ans,sum);
        return ;
    }
    for(int i=2; i<=n; i++)
    {
        if(!vis[i])
        {
            vis[i]=1;
            dfs(i,step+map[x][i],cnt+1,sum+map[x][i]*(n-cnt));
            vis[i]=0;
        }
    }
}

int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1; i<=n; i++)
            for(int j=1; j<=n; j++)
                scanf("%d",&map[i][j]);
        floyd();
        for(int i=2; i<=n; i++)
            scanf("%d",&dead[i]);
        ans=inf;
        dfs(1,0,1,0);
        if(ans==inf)
            printf("-1\n");
        else
            printf("%d\n",ans);
    }
    return 0;
}



总结:坑题也是好题,又有了提高
一是剪枝没有想全,只是想到要走的每一步必须要≤这一步的截止日期,并没有想到一定要≤后面所有点的截至日期
二是思路太狭隘了,并没有想到要用floyd松弛一下(其实应该可以想到的,只是思路已经被提示的暴搜法局限了0.0),开始还想用不标记的剪枝来做,然后发现根本不可能,后来又加了一些每一次只能走一遍已经走过的路的的剪枝,结果wrong了0.0(还是做题少啊~Orz)

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值