Wow! Such Conquering!

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
暴力搜索,剪枝

view code#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
#define REP(i,n) for(int i=0; i<(n); i++)
#define FOR(i,s,t) for(int i=(s); i<=(t); i++)
const int INF = 1<<30;
const int N = 35;
int dist[N][N], tm[N], n;
int bit[N], tot, ans; void floyd()
{
REP(k,n) REP(i,n) REP(j,n) dist[i][j] = min(dist[i][j], dist[i][k]+dist[k][j]);
} void dfs(int u, int s, int cost, int sum, int num)
{
if(sum>=ans) return ;
if(s == tot){
ans = min(ans, sum);
return ;
}
FOR(i,1,n-1){
if(s&bit[i]) continue;
if(cost+dist[u][i]>tm[i]) return ;
}
FOR(i,1,n-1){
if(s&bit[i]) continue;
dfs(i, s|bit[i], cost+dist[u][i], sum+num*dist[u][i], num-1);
}
} void solve()
{
REP(i,n) REP(j,n) scanf("%d", &dist[i][j]);
floyd();
FOR(i,1,n-1) scanf("%d", tm+i);
ans = INF, tot = bit[n]-1;
dfs(0, 1, 0, 0, n-1);
if(ans==INF) ans=-1;
printf("%d\n",ans);
} int main()
{
// freopen("in.txt", "r", stdin);
REP(i,31) bit[i] = 1<<i;
while(scanf("%d", &n)>0) solve();
return 0;
}

hdu 4848 Wow! Such Conquering! (floyd dfs)的更多相关文章

  1. HDU-4848 Wow! Such Conquering! (回溯+剪枝)

    Problem Description There are n Doge Planets in the Doge Space. The conqueror of Doge Space is Super ...

  2. HDU 4848 - Wow! Such Conquering!

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

  3. 【BZOJ】1612: [Usaco2008 Jan]Cow Contest奶牛的比赛(floyd/dfs)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1612 赢+输==n-1 则一定确定 dfs和floyd都行(dfs我不确定,因为我没提交,权限还没开 ...

  4. hdu 1385 Minimum Transport Cost (floyd算法)

    貌似···················· 这个算法深的东西还是很不熟悉!继续学习!!!! ++++++++++++++++++++++++++++ ======================== ...

  5. HDU 1044 Collect More Jewels(BFS+DFS)

    Collect More Jewels Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Othe ...

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

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

  7. HDU 1312 Red and Black(经典DFS)

    嗯... 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1312 一道很经典的dfs,设置上下左右四个方向,读入时记下起点,然后跑dfs即可...最后答 ...

  8. 题解报告:hdu 1312 Red and Black(简单dfs)

    Problem Description There is a rectangular room, covered with square tiles. Each tile is colored eit ...

  9. HDU 2489 Minimal Ratio Tree(prim+DFS)

    Minimal Ratio Tree Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

随机推荐

  1. 设计模式--简单工厂(Factory)模式

    温故而知新,看了以前写的博文<Asp.net读取Excel文件 2>http://www.cnblogs.com/insus/archive/2011/05/05/2037808.html ...

  2. IIS 503日志文件在哪

    概述  503:“服务不可用”错误是一个非自定义的错误,该错误表示服务器当前无法处理该请求. 可能原因:1.管理员可能关闭应用程序池以执行维护.2.当请求到达时应用程序池队列已满.3.应用程序池标识没 ...

  3. 在phpwind内容页使用百度分享进行图片分享

    在phpwind内容页使用百度分享进行图片分享时,百度分享默认提取到的图片不一定是主题正文内容中的图片,需要使用百度提供的配置机制自行调整. 整个代码添加的位置在此不论,主要原理是在主题正文区域提取图 ...

  4. tomcat+webservice实现简单的web服务远程调用接口

    1,准备工作:    ①需要使用到jaxws的一系列架包,网址:http://jax-ws.java.net,我下的是比较新的一个版本(下载好以后吧架包发在lib下),②webservice的一个工具 ...

  5. 在MVC中应用百度富文本编辑器

    1.下载.NET版本的百度富文本编辑器,前往 下载.NET版本百度富文本框 2.解压下载的.zip压缩包,将utf8-.net文件夹名称改为:ueditor,复制到MVC根目录下面.结构如下: App ...

  6. 内核移植和文件系统制作(2):linux内核最小系统和initramfs文件系统

    linux内核最小系统,使用内核版本:https://www.kernel.org/pub/linux/kernel/v3.0/linux-3.8.1.tar.bz2 1,FL2440板子的基本硬件: ...

  7. Linux修改命令提示符(关于环境参量PS1)

    关乎环境参量的四个文件/etc/profile  /etc/bashrc ~/.bashrc  ~/.bash_profile $$$:/etc/profile:此文件为系统的每个用户设置环境信息,当 ...

  8. TCMalloc优化MySQL、Nginx内存管理

    TCMalloc的全称为Thread-Caching Malloc,是谷歌开发的开源工具google-perftools中的一个成员. 与标准的glibc库的Malloc相比,TCMalloc库在内存 ...

  9. 【Effective Java】3、避免创建不必要的对象

    创建对象的时候,有些变量可以一直保持的时候,可以不必要每次实例化对象的时候都把这些变量初始化一遍,可以使用静态和静态块的方式把这些变量的数据固定下来 package cn.xf.cp.ch02.ite ...

  10. css中的定位和框模型问题

    和定位有关的元素属性如下 position  元素的定位类型   绝对定位会相对于最近定位的祖先元素的位置来定位,而不会影响其他框的位置 固定定位 相对定位 z-index   元素的堆叠顺序 值越大 ...