HDU 4848 wow! Such conquering! (Violent search + strong pruning)

Source: Internet
Author: User
Tags acos
Wow! Such conquering! Time Limit: 15000/8000 MS (Java/others) memory limit: 32768/32768 K (Java/Others) Total submission (s): 0 accepted submission (s): 0

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 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 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.
 
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: 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.
 
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
40 3 8 64 0 7 47 5 0 26 9 3 030 8 3040 2 3 32 0 3 32 3 0 32 3 3 02 3 3
 
Sample output
36-1Explanation: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.
 
Meaning: There are N points (n <= 30). Starting from the first point, we need to traverse all points and traverse each point before time [I, calculate the sum of the distance to each point.
I would like to thank Daniel SD for his inactive thoughts on life in the illusion.
Train of Thought: 30 points, decisive brute-force search. Based on Time [I] pruning, a strong pruning means walking through an edge and having num points left, the answer is to add edge * num, this is done when the current answer is greater than the answer.
Code 1:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <string>#include <map>#include <stack>#include <vector>#include <set>#include <queue>#define maxn 1005#define MAXN 100005#define mod 100000000#define INF 0x3f3f3f3f#define pi acos(-1.0)#define eps 1e-6typedef long long ll;using namespace std;int n,m,ans,tot;int dist[35][35],time[35],bit[35];void floyd(){    int i,j,k;    for(k=0;k<n;k++)    {        for(i=0;i<n;i++)        {            for(j=0;j<n;j++)            {                dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);            }        }    }}void dfs(int u,int s,int cost,int sum,int num){    if(sum>=ans) return ;    if(s==tot)    {        ans=min(ans,sum);        return ;    }    int i;    for(i=1;i<n;i++)    {        if(s&bit[i]) continue ;        if(cost+dist[u][i]>time[i]) return ;    }    for(i=1;i<n;i++)    {        if(s&bit[i]) continue ;        dfs(i,s|bit[i],cost+dist[u][i],sum+dist[u][i]*num,num-1);    }}int main(){   int i,j,t;   for(i=0;i<=30;i++)   {       bit[i]=(1<<i);   }   while(~scanf("%d",&n))   {       for(i=0;i<n;i++)       {           for(j=0;j<n;j++)           {               scanf("%d",&dist[i][j]);           }       }       for(i=1;i<n;i++)       {           scanf("%d",&time[i]);       }       tot=bit[n]-1;       floyd();       ans=INF;       dfs(0,1,0,0,n-1);       if(ans==INF) ans=-1;       printf("%d\n",ans);   }}


By the way, I learned about the two-way linked list. Code 2: (use a two-way linked list for search)
# Include <iostream> # include <cstdio> # include <cstring> # include <algorithm> # include <cmath> # include <string> # include <map> # include <stack> # include <vector> # include <set> # include <queue> # define maxn 1005 # define maxn 100005 # define mod 100000000 # define INF 0x3f3f3f # define PI ACOs (-1.0) # define EPS 1e-6typedef long ll; using namespace STD; int n, m, ANS, TOT, Len; int Dist [35] [35], time [35], R [35], L [35]; Vo Id Floyd () {int I, j, k; For (k = 1; k <= N; k ++) {for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {Dist [I] [J] = min (Dist [I] [J], dist [I] [k] + dist [k] [J]) ;}}} void DFS (INT U, int cost, int sum, int num) {for (INT I = R [0]; I; I = R [I]) {If (cost + dist [u] [I]> time [I]) return;} If (sum> = ans) return; R [L [u] = R [u]; L [R [u] = L [u]; // Delete u if (R [0] = 0) {ans = min (ANS, sum ); R [L [u] = L [R [u] = u; return;} For (INT I = R [0]; I; I = R [I]) {DFS (I, C Ost + dist [u] [I], sum + dist [u] [I] * num, num-1 );} R [L [u] = L [R [u] = u; // restore u} int main () {int I, j, T; while (~ Scanf ("% d", & N) {for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {scanf ("% d", & Dist [I] [J]) ;}}for (I = 2; I <= N; I ++) {scanf ("% d", & time [I]) ;}floyd (); for (I = 0; I <= N; I ++) // initialize {R [I] = I + 1; L [I] = I-1;} R [N] = 0; L [0] = N; ans = inf; DFS (, 0, n-1); If (ANS = inf) ans =-1; printf ("% d \ n", ANS );}}




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.