Wow! Such Conquering!
Time Limit: 15000/8000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1841 Accepted Submission(s): 535
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 T
xy 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.
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 T xy . 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.
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 xy . 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 -1HintExplanation: 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
Recommend
liuyiding
读不懂题QAQ, 题意是给出星球之间的关系,和访问该星球最迟不能超过的时间,求1到所有星球所需时间总和的最小值,
用floyd计算每个星球之间的最短距离,暴力搜索加剪枝即可
#include <iostream>
#include <iomanip>
#include<stdio.h>
#include<string.h>
#include<stack>
#include<stdlib.h>
#include<queue>
#include<map>
#include<math.h>
#include<algorithm>
#include<vector>
#define mem(a,b) memset(a,b,sizeof(a))
#define inf 0x3f3f3f3f
using namespace std;
int n,ans;
int Time[35];
int e[35][35];
int 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++)
{
if(e[i][j]>e[i][k]+e[k][j])
e[i][j]=e[i][k]+e[k][j];
}
}
}
}
void dfs(int u,int s,int cost,int sum)
{
if(sum>=ans)return;
if(s==n)
{
ans=min(ans,sum);
return;
}
int i;
for(i=1;i<n;i++)
{
//如果该星球没被访问过,且到达该星球的时间大于最迟到达的时间,剪枝
if(!vis[i]&&e[u][i]+cost>Time[i])
{
return;
}
}
for(i=1;i<n;i++)
{
if(!vis[i])
{
vis[i]=1;
//求1到每个星球的总和,则每一条松弛过的边需要加n-s次
dfs(i,s+1,cost+e[u][i],sum+e[u][i]*(n-s));
vis[i]=0;
}
}
}
int main()
{
while(~scanf("%d",&n))
{
for(int i=0;i<n;i++)
{
for(int j=0;j<n;j++)
{
scanf("%d",&e[i][j]);
}
}
for(int i=1;i<n;i++)
{
scanf("%d",&Time[i]);
}
floyd();
ans=inf;
mem(vis,0);
dfs(0,1,0,0);
if(ans==inf)ans=-1;
printf("%d\n",ans);
}
}