HDU 4848 Wow! Such Conquering! (搜索)

HDU4848

Wow! Such Conquering!

Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 290 Accepted Submission(s): 92

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 -136
-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.

Source

2014西安全国邀请赛

题意很清楚,也比较简单。 就是从1这点出发,要经过2~n的点,每个点要在截止时间前到达。 求最后总到达时间的和最小。 直接暴力搜索就可以了,简单剪枝。很水的题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/* ***************

Author :kuangbin

Created Time :2014/7/16 18:15:26

File Name :E:\2014ACM\比赛\2014西安邀请赛\B.cpp

************************************************ */

#include <stdio.h>

#include <string.h>

#include <iostream>

#include <algorithm>

#include <vector>

#include <queue>

#include <set>

#include <map>

#include <string>

#include <math.h>

#include <stdlib.h>

#include <time.h>

using namespace std;

const int INF = 0x3f3f3f3f;

int a[100][100];

int dt[100];

int n;

int ans;

bool used[100];

void dfs(int u,int now,int tot,int cnt)

{

if(cnt == n-1)

{

if(tot < ans)ans = tot;

return;

}

if(tot + (n-1-cnt)*now >= ans)return;

for(int i = 1;i < n;i++)

if(!used[i] && now+a[u][i] > dt[i])

return;

for(int i = 1;i < n;i++)

if(!used[i])

{

used[i] = true;

dfs(i,now+a[u][i],tot+now+a[u][i],cnt+1);

used[i] = false;

}

}

int main()

{

//freopen("in.txt","r",stdin);

//freopen("out.txt","w",stdout);

while(scanf("%d",&n) == 1)

{

for(int i = 0;i < n;i++)

for(int j = 0;j < n;j++)

scanf("%d",&a[i][j]);

for(int k = 0;k < n;k++)

for(int i = 0;i < n;i++)

for(int j = 0;j < n;j++)

if(a[i][k]+a[k][j] < a[i][j])

a[i][j] = a[i][k]+a[k][j];

for(int i = 1;i < n;i++)

scanf("%d",&dt[i]);

ans = INF;

memset(used,false,sizeof(used));

dfs(0,0,0,0);

if(ans == INF)ans = -1;

printf("%d\n",ans);

}

return 0;

}
------ 本文结束------
  • 本文作者: kuangbin
  • 本文链接: 260.html
  • 版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
0%