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 Deadline x. 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: Deadline 2 to Deadline n.
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
写题就是暴力搜,从区域赛A题搜到WF Z题,写到出题人质疑人生。什么最短路,什么MST,什么网络流二分匹配,统统都不会,就是搜。
除了题目中自带的剪枝,还要加上几个常用的。
- 判断未访问节点截至时间已经小于当前时间
- 预判走过当前节点后,总花费是否一定大于已知的最优解。
因为要求到每个节点的时间之和,那么如果到当前时间为t,总耗时为sum,还有n个节点没有访问到,因为后续节点都将是在这个节点的基础上去访问的,所以最后得到的最优解一定大于 t*n+sum
#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>
#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return
#define getLen(name,index) name[index].size()
#define mem(a,b) memset(a,b,sizeof(a))
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
int N,ans;
int dis[32][32];
int dT[32];
bool vis[32];
void dfs(int v,int sum,int t,int dep);
int main(){
while(~scanf("%d",&N)){
rep(i,1,N+1)
rep(j,1,N+1)
scanf("%d",&dis[i][j]);
rep(i,1,N+1)
rep(j,1,N+1)
rep(k,1,N+1)
dis[j][k]=min(dis[j][k],dis[j][i]+dis[i][k]);
rep(i,2,N+1){
scanf("%d",&dT[i]);
}
mem(vis,false);
ans=INT_MAX;
vis[1]=true;
dfs(1,0,0,1);
if(ans==INT_MAX)
ans=-1;
printf("%d\n",ans);
}
re 0;
}
void dfs(int v,int sum,int t,int dep){
if(sum>ans || t>dT[v])
re ;
rep(i,2,N+1){
if(!vis[i] && (t>dT[i] || t+dis[v][i]>dT[i]))
re;
}
if(dep==N){
ans=sum;
re ;
}
rep(i,2,N+1){
if(!vis[i] && i!=v){
vis[i]=true;
if(sum+(N-dep)*(t+dis[v][i])<=ans)
dfs(i,sum+t+dis[v][i],t+dis[v][i],dep+1);
vis[i]=false;
}
}
}