</pre><h1 style="color:#1a5cc8">Wow! Such Conquering!</h1><span size="+0" style=""><strong><span style="font-family:Arial; color:green; font-size:12px">Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1465 Accepted Submission(s): 439</span></strong></span><div class="panel_title" align="left">Problem Description</div><div class="panel_content">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 T<sub>xy</sub> 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<sub>x</sub>. 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.</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Input</div><div class="panel_content">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 T<sub>xy</sub> . Then follows a single line containing n - 1 integers: Deadline<sub>2</sub> to Deadline<sub>n</sub>.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.</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Output</div><div class="panel_content">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.</div><div class="panel_bottom"> </div><div class="panel_title" align="left">Sample Input</div><div class="panel_title" align="left"><div class="panel_title" align="left">Sample Output</div><div class="panel_content"><pre><div style="font-family:'Courier New',Courier,monospace">36
-1
<div style="border:1px dashed rgb(183,203,255); padding:6px; font-family:'Times New Roman'; font-size:14px; background-color:rgb(244,251,255)"><div style="border-bottom-color:rgb(183,203,255); border-bottom-width:1px; border-bottom-style:dashed; font-family:Arial; color:rgb(124,169,237); font-weight:bold"><em>Hint</em></div><strong>Explanation</strong>:
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.</div><div>
</div></div>
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
//题目要求在给定的行星和行星之间的距离中,求出到达所有行星所走的最短路径。用floyd函数将所有的点和点距排序好,每个点的i,j表示i点到j点之间的距离,
同时floyd中k表示步骤,然后深搜。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const double eps = 1e-6;
const double pi = acos(-1.0);
const int INF = 1e9;
const int MOD = 1e9+7;
#define ll long long
#define CL(a,b) memset(a,b,sizeof(a))
#define lson (i<<1)
#define rson ((i<<1)|1)
#define MAXN 0x3f3f3f3f
int dis[35][35],x[35],n,ans,vis[35];
void floyd()
{
int i,j,k;
for(k=1; k<=n; k++)
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
if(dis[i][j]>dis[i][k]+dis[k][j])
dis[i][j]=dis[i][k]+dis[k][j];
}
//弗洛伊德排好
void DFS(int a,int b,int t,int sum)
{
if(b==n+1)
{
if(ans>sum)
ans=sum;
return;
}
if(ans<sum) return;
for(int i=2; i<=n; i++)
if(!vis[i]&&dis[a][i]+t>x[i])
return;
//边界
for(int i=2; i<=n; i++)
{
if(!vis[i])
{
vis[i]=1;
DFS(i,b+1,t+dis[a][i],sum+dis[a][i]*(n-b+1));
vis[i]=0;
}
}
return;
}
int main()
{
int m,i,j;
while(~scanf("%d",&n))
{
for(i=1; i<=n; i++)
for(j=1; j<=n; j++)
scanf("%d",&dis[i][j]);
for(i=2; i<=n; i++)
scanf("%d",&x[i]);
floyd();
ans=MAXN;
memset(vis,0,sizeof(vis));
DFS(1,2,0,0);
if(ans<MAXN) printf("%d\n",ans);
else printf("-1\n");
}
return 0;
}