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

来源:互联网 发布:王兆山 真相 知乎 编辑:程序博客网 时间:2024/04/30 16:54
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.


首先注意题目给出的邻接矩阵并不一定是最短路,所以先用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