Problem Description There is n Doge planets in the Doge Space. The conqueror of Doge Space is a Super Doge, who's going to inspect he 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 and 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's Doge Planet to be as small as possible. You can assume it takes so little time to inspect he Doge Army that we can ignore it. Input There is multiple test cases. Please process till EOF.
Each test case contains several lines. The first line of all 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, containing n-1 integers:deadline2 to Deadlinen.
All numbers is guaranteed to being non-negative integers smaller than or equal to one million. n is guaranteed to being no less than 3 and no more than 30. output If some deadlines can isn't fulfilled, please OUTPU T "-1" (which means the Super Doge'll say "wow! So slow! Such delay! Much anger! . . . ", but don't need to output it), else output the minimum sum of all arrival time-to-Doge planet. sample in PUT40 3 8 64 0 7 5 0 9 3 030 8 3040 2 3 0 3 3 0 3 3 3 sample output36-1 title: N (n<31) node, numbered 0~n-1. The starting point is 0 nodes, each node can only pass once and to be within the limit of time to arrive, now to reach each point at the moment to add up, to satisfy the minimum time and the condition. Topic Analysis: DFS. The idea is not difficult, but to prune. To summarize, pruning is nothing more than two basis, one is based on the topic (not easy to find) the conditions of pruning, and the second is based on the predicted answer to pruning, that is, according to the current phase of the solution to predict the approximate current scheme of the optimal solution and the best solution to determine whether or not to cut. It should be done this way: 1. If, under the current scheme, the time at which an unreachable point has been exceeded, it is pruned; 2. Based on the current time spent and the number of points that have not been reached, a solution is predicted that is as close as possible to the optimal solution of the current scheme time+time* Num, when the solution is larger than the optimal solution that has been obtained, it is clipped. After feeling: this is a good question, it is worth doing. The code is as follows:
# include<iostream># include<cstdio># include<cstring># include<algorithm>using namespace std;# define LL long longconst int inf=1<<30;int mp[35][35],n,a[35],ans,vis[35];void Floyd () {for (int k=0;k<n; ++K) for (int i=0;i<n;++i) for (int j=0;j<n;++j) mp[i][j]=min (Mp[i][j],mp[i][k]+mp[k] [j]);} void Dfs (int cur,int time,int att,int num) {if (num==n) {ans=min (Ans,att); return; } if (att+ (n-num) *time>=ans) return; for (int i=1;i<n;++i) if (!vis[i]&&time+mp[cur][i]>a[i]) return; for (int i=1;i<n;++i) {if (vis[i]) continue; Vis[i]=1; DFS (I,TIME+MP[CUR][I],ATT+TIME+MP[CUR][I],NUM+1); vis[i]=0; }}int Main () {while (~SCANF ("%d", &n)///Is here if "!=eof" is timed out. {for (Int. i=0;i<n;++i) for (int j=0;j<n;++j) scanf ("%d", &mp[i][j]); Floyd (); a[0]=0; for (intI=1;i<n;++i) scanf ("%d", a+i); memset (vis,0,sizeof (VIS)); Ans=inf; Vis[0]=1; DFS (0,0,0,1); if (ans!=inf) printf ("%d\n", ans); else printf (" -1\n"); } return 0;}
HDU-4848 wow! Such conquering! (Backtracking + pruning)