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. 重构第23天 引用参数对象(Introduce Parameter Object)

    理解:有时候我们的一个方法,需要很多个参数,太多参数,不易阅读和理解,我们就可以把多个参数封装成一个对象. 详解: 重构前代码: public class Registration { public ...

  2. 在IIS7中使用ARR(Application Request Routing)反向代理虚拟目录到Nodejs站点

    目标: 1.访问www.arrdemo.com/proxy 跳转到 localhost:8898的Nodejs站点 2.Nodejs站点的页面可以返回到浏览器,包括js,css,图片 3.Nodejs ...

  3. 那些教程没有的php2-对象

    php.net 对象 在类定义内部,可以用 new self 和 new parent 创建新对象. 当把一个对象已经创建的实例赋给一个新变量时,新变量会访问同一个实例,就和用该对象赋值一样.可以用克 ...

  4. [python学习笔记]Day3

    函数 如: def is_leapyear(year): if (year%4 == 0 and year%100 != 0) or (year%400 == 0): return True else ...

  5. 解决SlidingMenu和SwipeBackLayout右滑事件冲突问题

    SwipeBackLayout向右滑动关闭当前Activity,SlidingMenu向右滑动则是打开menu部分.在同一个Activity中,当SlidingMenu处于打开状态时,此时向右滑动,事 ...

  6. PHP redis Api 中文文档

    phpredis是php的一个扩展,效率是相当高有链表排序功能,对创建内存级的模块业务关系 很有用;以下是redis官方提供的命令使用技巧: 下载地址如下: https://github.com/ow ...

  7. RAID选项

    RAID:Redundant Array Independent Disk(独立磁盘构成的具有冗余能力的阵列) 最常见的为RAID类型为:0,1,5和10:3和6很少见,但在某些环境中仍然有用. RA ...

  8. 如何选择RabbitMQ的消息保存方式?

    RabbitMQ对于queue中的message的保存方式有两种方式:disc和ram.如果采用disc,则需要对exchange/queue/delivery mode都要设置成durable模式. ...

  9. J2EE分布式架构及MySQL交流群

    J2EE分布式架构及MySQL交流群:577913057

  10. MySQL到MsSQL的迁移工具——SSMA

    SQL Server迁移助手(SSMA)团队开发了针对MySQL的迁移助手Microsoft SQL Server Migration Assistant 2008 for MySQL.微软同时发布了 ...