Wow! Such Conquering!
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 846 Accepted Submission(s): 255
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.
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.
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
36 -1题意:n个点,要求从1点开始跑,遍历所有点,要求到达每个点的时间<=time[i],求到达每个点时间的总和最小。爆搜+最优性剪枝+可行性剪枝(参考别人的)#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <cctype> #include <cmath> #include <cstdlib> #include <vector> #include <queue> #include <set> #include <map> #include <list> #define L long long using namespace std; const int INF=0x3f3f3f3f; const int maxn=32; int dis[maxn][maxn]; int n,tim[maxn]; void Floyd() { for(int k=1;k<=n;k++) for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]); } int vis[maxn],ans; void dfs(int src,int cur_time,int sum_time,int num) { if(num==n) { ans=min(ans,sum_time); return ; } //最优性剪枝:当前总时间+当前时间*剩余点数(因为往后到达的点的时间总是小于等于当前时间的)>=当前最优解 剪掉 if(sum_time+cur_time*(n-num)>=ans)return ; //可行性剪枝:如果当前时间有超Deadtime的,剪掉 for(int i=2;i<=n;i++) if(!vis[i]&&cur_time>tim[i]) return ; for(int i=2;i<=n;i++) { if(src!=i&&!vis[i]&&cur_time+dis[src][i]<=tim[i]) { vis[i]=1; dfs(i,cur_time+dis[src][i],sum_time+cur_time+dis[src][i],num+1); vis[i]=0; } } } int main() { while(~scanf("%d",&n)) { memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) scanf("%d",&dis[i][j]); tim[1]=INF; for(int i=2;i<=n;i++) scanf("%d",&tim[i]); Floyd(); vis[1]=1; ans=INF; dfs(1,0,0,1); if(ans==INF)ans=-1; printf("%d\n",ans); } return 0; }
2075


&spm=1001.2101.3001.11974&articleId=40181917&d=1&t=3&u=deb38951be0f4085826d42d8906cc0c6)

被折叠的 条评论
为什么被折叠?



