Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

记忆化搜索dp居然 0ms AC了,神奇[附代码]

Posted by On18 at 2020-04-22 23:13:10 on Problem 1695
#include<iostream>
#include<memory.h>
using namespace std;

int d[32][32];
int dp[32][32][32];
int n;

int dfs(int i, int j, int k, int m)
{
    if(dp[i][j][k] != 0)
        return dp[i][j][k];
    if(m == n)
        return 0;
    int ans = 999999;
    ans = dfs(m + 1, j, k, m + 1) + d[i][m + 1];
    ans = min(ans, dfs(i, m + 1, k, m + 1) + d[j][m + 1]);
    ans = min(ans, dfs(i, j, m + 1, m + 1) + d[k][m + 1]);
    dp[i][j][k] = ans;
    return ans;
}

int main()
{
    int caseNum;
    cin >> caseNum;
    for (int ca = 0; ca < caseNum;++ca)
    {
        cin >> n;
        memset(dp, 0, sizeof(dp));
        for (int i = 1; i <= n;++i)
            for (int j = i+1; j <= n;++j)
                scanf("%d", &d[i][j]);
        int res = dfs(1, 1, 1, 1);
        cout << res << endl;
    }
}


Followed by:

Post your reply here:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator