Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

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
 
Hint

Explanation:
In case #1: The Super Doge travels to Doge Planet 2 at the time of 8 and to Doge Planet 3 at the time of 12,
then to Doge Planet 4 at the time of 16.
The minimum sum of all arrival time is 36.

首先Floyd算法得到任意两点间的最短时间;

然后之间进行DFS,剪枝优化时间。

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 33
#define INF 0x3f3f3f3f
using namespace std;
int d[MAXN][MAXN],ans;
int n,deadline[MAXN],arrival_time[MAXN];
bool vis[MAXN];
void floyd()
{
for(int k=;k<=n;k++)
{
for(int i=;i<=n;i++)
{
for(int j=;j<=n;j++) d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
}
}
}
void dfs(int now,int cur_time,int sum_time,int cnt)
{
if(cnt==n)//走完了n个星球
{
ans=min(ans,sum_time);//尝试更新答案,使得答案是到目前为止的最优解
return;
} if(sum_time + cur_time * (n-cnt) >= ans) return;
//最优性剪枝:当前到达时间和 + 当前时间 * 还没去的星球 >= 到目前为止的最优解,剪掉
//      因为,到目前为止,还没去的星球,到达那里的时间必然大于等于当前时间 for(int i=;i<=n;i++) if(!vis[i] && cur_time>deadline[i]) return;
//可行性剪枝:到目前为止,现在的时间大于未到达的星球的deadline,已经不满足要求,剪掉 for(int next=;next<=n;next++)
{
if(now!=next && !vis[next] && cur_time+d[now][next]<=deadline[next])
{
vis[next]=;
dfs(next,cur_time+d[now][next],sum_time+cur_time+d[now][next],cnt+);
vis[next]=;
}
} }
int main()
{
while(scanf("%d",&n)!=EOF)
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
scanf("%d",&d[i][j]);
} for(int i=;i<=n;i++) scanf("%d",&deadline[i]);
deadline[]=INF; floyd(); ans=INF;
vis[]=;
dfs(,,,);
printf("%d\n", ((ans==INF)?-:ans) );
}
}

HDU 4848 - Wow! Such Conquering!的更多相关文章

  1. hdu 4848 Wow! Such Conquering! (floyd dfs)

    Wow! Such Conquering! Problem Description There are n Doge Planets in the Doge Space. The conqueror ...

  2. HDU-4848 Wow! Such Conquering! 爆搜+剪枝

    Wow! Such Conquering! 题意:一个n*n的数字格,Txy表示x到y的时间.最后一行n-1个数字代表分别到2-n的最晚时间,自己在1号点,求到达这些点的时间和的最少值,如果没有满足情 ...

  3. hdu 4850 Wow! Such String! 欧拉回路

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4080264.html 题目链接:hdu 4850 Wow! Such String! 欧拉回 ...

  4. hdu 4893 Wow! Such Sequence!(线段树)

    题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 改动k的为值添加d 2 l r, 查询l到r的区间和 3 l r. 间l到r区间上的所以数变成 ...

  5. HDU 4850 Wow! Such String!(欧拉道路)

    HDU 4850 Wow! Such String! 题目链接 题意:求50W内的字符串.要求长度大于等于4的子串,仅仅出现一次 思路:须要推理.考虑4个字母的字符串,一共同拥有26^4种,这些由这些 ...

  6. HDU 4849 Wow! Such City!陕西邀请赛C(最短路)

    HDU 4849 Wow! Such City! 题目链接 题意:依照题目中的公式构造出临接矩阵后.求出1到2 - n最短路%M的最小值 思路:就依据题目中方法构造矩阵,然后写一个dijkstra,利 ...

  7. HDU 4848

    http://acm.hdu.edu.cn/showproblem.php?pid=4848 题意:求遍历所有点的最小值(这个答案是加i点到起始点的距离,不是当前点到i的距离),必须在ti[i]前到达 ...

  8. HDU 4893 Wow! Such Sequence! (线段树)

    Wow! Such Sequence! 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4893 Description Recently, Doge ...

  9. hdu 4848 搜索+剪枝 2014西安邀请赛

    http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...

随机推荐

  1. 7 -- Spring的基本用法 -- 7... 创建Bean的3种方式

    7.7 创建Bean的3种方式 ① 调用构造器创建Bean. ② 调用静态工厂方法创建Bean. ③ 调用实例工厂方法创建Bean. 7.7.1 使用构造器创建Bean实例. 使用构造器来创建Bean ...

  2. iOS 关于信鸽推送点击推送通知的处理

    最近的项目中使用了推送模块,使用的是企鹅帝国的信鸽推送服务,关于具体怎么推送的,证书如何设置,我不再赘述,一来开发文档中已经讲的非常清楚,二来在网上一搜的话也能搜到一大堆:在这里主要写下关于推送的通知 ...

  3. python线程池(threadpool)

    一.安装 pip install threadpool 二.使用介绍 (1)引入threadpool模块 (2)定义线程函数 (3)创建线程 池threadpool.ThreadPool() (4)创 ...

  4. 给sharepoint某列表项单独赋予权限

    /// <summary> /// 列表项事件 /// </summary> public class EventReceiver2 : SPItemEventReceiver ...

  5. sql 替换字符串

    MSSQL替换语句: )),'abc.com','123.com') 例如: update PE_Article set Content=replace(cast(Content as varchar ...

  6. VS调试DLL项目代码

    如果DLL有对应的lib文件,并且dll工程和调用它的exe属于同一个解决方案,直接打断点调试就可以.例如OSG解决方案中的例子程序可以直接调试osgUtil模块的代码. 如果A.exe项目和A.dl ...

  7. Vim 的 Python 编辑器详细配置过程 (Based on Ubuntu 12.04 LTS)

    为什么要用vim编辑py文件? 因为在Linux命令行中,缺少图形界面的IDE,vim是最佳的文本编辑器,而为了更好的编辑py文本,所以配置vim. 1. 安装完整版vim vi和vim的区别? 在L ...

  8. openldap slapd.conf参数

    已安装系统的 /etc/openldap/slapd.conf 中包含 LDAP 服务器的完整配置文件.在此简述了其中的各个项并说明了必要的调整.以符号 (#) 为前缀的项处于非活动状态.必须取消这个 ...

  9. 《转》python学习(11)-表达式和语句

    转自 http://www.cnblogs.com/BeginMan/p/3164600.html 一.Python语句 if语句.else语句.elif语句.条件表达式.while语句.for语句. ...

  10. linux下的shell操作mysql

    (1)MySQL的启动 重启了一次服务器后,使用> mysql -u root -p登陆是出现下面的错误: ERROR 2002 (HY000): Can't connect to local ...