关闭

hdu4848 暴搜+剪枝

121人阅读 评论(0) 收藏 举报
分类:

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

题意:

给一个有向图,要求从起点出发,把图上所有点都遍历一遍时,每一个点到达的最早时间之和.(每一个节点都有一个deadline必须在这之前或正好到达).

思路:

就是一个暴搜+剪枝,就看有没有勇气写了。然而像我这种渣渣就算写了搜索,搜索没写好,也还是tle。这道题预先要用floyd算法预处理出每两点之间的最短路。然后每次搜索的时候都选一个没有选过的走过去。然后就是正常的剪枝:一个是最优解剪枝,另一个是超过deadline剪枝。
还有一个地方很关键(maps[cur][i]*(n-num-1)+sum)时间增加的写法,就是可以快速地增加sum的值,进行最优解剪枝。(没有就超时!!!)。

代码:

#include <iostream>
#include <stdio.h>
#include <limits.h>
#include <algorithm>
#include <string.h>
#define inf 0x3f3f3f3f
using namespace std;
int maps[40][40];
int dead[40];
bool vis[40];
int minn=inf,n;
void floyd()
{
    for(int k=1;k<=n;k++)
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                    maps[i][j]=(maps[i][j]<maps[i][k]+maps[k][j]?maps[i][j]:maps[i][k]+maps[k][j]);
}
void dfs(int cur,int num,int time,int sum)
{
    if(num==n)
    {
        minn=(sum<minn?sum:minn);
        return;
    }
    if(minn<sum)//最优剪枝
        return ;
    for(int i=2;i<=n;i++)//超时剪枝
        if(!vis[i]&&maps[cur][i]+time>dead[i])
            return ;
    for(int i=2;i<=n;i++)
        if(!vis[i])
        {
            vis[i]=true;
            dfs(i,num+1,time+maps[cur][i],maps[cur][i]*(n-cur)+sum);
            vis[i]=false;
        }
}
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        minn=INT_MAX;
        memset(maps,0,sizeof(maps));
        memset(dead,0,sizeof(dead));
        memset(vis,0,sizeof(vis));
        for(int i=1;i<=n;i++)
            for(int j=1;j<=n;j++)
                scanf("%d",&maps[i][j]);
        for(int i=2;i<=n;i++)
            scanf("%d",&dead[i]);
        floyd();
        vis[1]=true;
        dfs(1,1,0,0);
        if(minn==INT_MAX)
            printf("-1\n");
        else
            printf("%d\n",minn);
    }
    return 0;
}

P.S.我在floyd时还记录了路径,想的是保证是不是每次走的路径上除了终点都是已经走过的,以保证floyd算法不会导致因路上的点没有走过而受影响,但想了想那样肯定不是最优解啊,可能因为每次都要判断反而慢了吧。。。

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3015次
    • 积分:227
    • 等级:
    • 排名:千里之外
    • 原创:20篇
    • 转载:0篇
    • 译文:0篇
    • 评论:6条
    文章分类