[HDU 4848][2014西安全国邀请赛]Wow! Such Conquering!

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 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.


首先注意题目给出的邻接矩阵并不一定是最短路,所以先用Floyd求出最短路,然后暴力搜索。

注意几个剪枝:

1,由于当前到达时间是递增的,所以若当前的消耗总时间+当前时间x剩余星球数量若大于已求出的最小值,剪枝。

2,若存在星球已超出访问时间限制,剪枝。


代码:

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,d[35][35],lim[35],vis[35],m=0x3ffffff;

void dfs(int deep,int cur,int time,int tot){
    if(tot+(n-deep)*time>m)return;
    if(deep==n){if(m>tot)m=tot;return;}
    int i,temp;
    for(i=2;i<=n;i++)
        if(vis[i]==0&&time>lim[i])return;
    for(i=2;i<=n;i++){
        if(vis[i]||tot+(n-deep)*d[cur][i]>=m)continue ;
        temp=time+d[cur][i];
        if(temp<=lim[i]){
            vis[i]=vis[i]|1;
            dfs(deep+1,i,temp,tot+temp);
            vis[i]=0;
        }
    }
}

int main(){
    while(scanf("%d",&n)>0){
        memset(vis,0,sizeof(vis));
        m=0x3ffffff;
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                scanf("%d",&d[i][j]);
        for(int i=2;i<=n;i++)scanf("%d",&lim[i]);
        for(int k=1;k<=n;k++)
            for(int i=1;i<=n;i++)
                for(int j=1;j<=n;j++)
                    d[i][j]=min(d[i][j],d[i][k]+d[k][j]);

        dfs(1,1,0,0);
        if(m<0x3ffffff)printf("%d\n",m);else printf("%d\n",-1);
    }
}



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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值