HDU 4848 - Wow! Such Conquering!
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
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.
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.
“-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.
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!的更多相关文章
- hdu 4848 Wow! Such Conquering! (floyd dfs)
Wow! Such Conquering! Problem Description There are n Doge Planets in the Doge Space. The conqueror ...
- HDU-4848 Wow! Such Conquering! 爆搜+剪枝
Wow! Such Conquering! 题意:一个n*n的数字格,Txy表示x到y的时间.最后一行n-1个数字代表分别到2-n的最晚时间,自己在1号点,求到达这些点的时间和的最少值,如果没有满足情 ...
- hdu 4850 Wow! Such String! 欧拉回路
作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4080264.html 题目链接:hdu 4850 Wow! Such String! 欧拉回 ...
- 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区间上的所以数变成 ...
- HDU 4850 Wow! Such String!(欧拉道路)
HDU 4850 Wow! Such String! 题目链接 题意:求50W内的字符串.要求长度大于等于4的子串,仅仅出现一次 思路:须要推理.考虑4个字母的字符串,一共同拥有26^4种,这些由这些 ...
- HDU 4849 Wow! Such City!陕西邀请赛C(最短路)
HDU 4849 Wow! Such City! 题目链接 题意:依照题目中的公式构造出临接矩阵后.求出1到2 - n最短路%M的最小值 思路:就依据题目中方法构造矩阵,然后写一个dijkstra,利 ...
- HDU 4848
http://acm.hdu.edu.cn/showproblem.php?pid=4848 题意:求遍历所有点的最小值(这个答案是加i点到起始点的距离,不是当前点到i的距离),必须在ti[i]前到达 ...
- HDU 4893 Wow! Such Sequence! (线段树)
Wow! Such Sequence! 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4893 Description Recently, Doge ...
- hdu 4848 搜索+剪枝 2014西安邀请赛
http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...
随机推荐
- SQLServer------远程调用失败
1.情况 出现 2.解决方法 打开“控制面板” -> “卸载程序” -> 找到 “Microsoft SQL Server 2016) ExpressLocalDB”将其卸载 -> ...
- Linux环境下MySQL设置gbk编码
1 编辑mysql配置文件 vi /etc/my.cnf 2 创建数据库 CREATE DATABASE `XXX` DEFAULT CHARACTER SET gbk COLLATE gbk_chi ...
- 01python初识—编辑器&版本&变量知识
python2.0和3.0版本变化很大,要跟随脚步,学新的,用新的.3.0 python开发工具pycharm 5.0 python的交互器 python的程序一般放到Linux环境下运行. pyth ...
- 【代码审计】DouPHP_v1.3代码执行漏洞分析
0x00 环境准备 DouPHP官网:http://www.douco.com/ 程序源码下载:http://down.douco.com/DouPHP_1.3_Release_20171002. ...
- 利用Python爆破数据库备份文件
某次测试过程中,发现PHP备份功能代码如下: // 根据时间生成备份文件名 $file_name = 'D' . date('Ymd') . 'T' . date('His'); $sql_file_ ...
- Hadoop核心架构HDFS+MapReduce+Hbase+Hive内部机理详解
转自:http://blog.csdn.net/iamdll/article/details/20998035 分类: 分布式 2014-03-11 10:31 156人阅读 评论(0) 收藏 举报 ...
- LINUX安装中文输入法和那些大坑
明明有很多事要做,却偏偏不知道要做什么,这种感觉,很令人上火. 一.基础知识 在原生ubuntu14.04英文环境系统中只有IBus拼音,真的好难用.由于搜狗输入法确实比Linux系统下其它的中文输入 ...
- 【摘】50个jQuery代码段帮助你成为一个更好的JavaScript开发者
今 天的帖子会给你们展示50个jQuery代码片段,这些代码能够给你的JavaScript项目提供帮助.其中的一些代码段是从jQuery1.4.2才 开始支持的做法,另一些则是真正有用的函数或方法,他 ...
- 客户端远程连接linux下mysql数据库授权
mysql默认状态是只支持localhost连接,这样远程服务器都输入IP地址去连接你的服务器是不可以的,下面我来介绍怎么让mysql允许远程连接配置方法,有需要的朋友可参考. 方法一,直接利用p ...
- 【LeetCode OJ】Longest Palindromic Substring
题目链接:https://leetcode.com/problems/longest-palindromic-substring/ 题目:Given a string S, find the long ...