DFS专题 下沙小面的(2)

这道题目有问题,4WA :

注意:
对于每组测试,Lele都是在站点0拉上乘客的。

最后看了题解,改了初始状态 AC 的。

View Code
# include <cstdio>

# include <cstdlib>

# include <cstring>



# define N 30 + 5



int n, k;

int g[N][N], des[N], min;

bool vis[N];



int cmp(const void *x, const void *y)

{

    return *(int*)x - *(int*)y;

}



void dfs(int cnt, int u, int d)

{

    if (cnt == k && min > d) { min = d; return ;}

    for (int i = 0; i < k; ++i) if (vis[i] == false)

    {

        vis[i] = true;

        dfs(cnt+1, des[i], d+g[u][des[i]]);

        vis[i] = false;

    }

}



void init(void)

{

    for (int i = 0; i < n; ++i)

    for (int j = 0; j < n; ++j)

        scanf("%d", &g[i][j]);



    scanf("%d", &k); //des[0] = 0;

    for (int i = 0; i < k; ++i)

        scanf("%d", &des[i]);

    qsort(des, k, sizeof(des[0]), cmp);



    /*  // 相同的不必搜两次

    int m = k, k = 1;

    for (int i = 1; i <= m; ++i)

        if (des[i] != des[i-1]) des[k++] = des[i];

    */

    memset(vis, false, sizeof(vis));

}



void solve(void)

{

    min = 0x7FFFFFFF;

    dfs(0, 0, 0);

    printf("%d\n", min);

}



int main()

{

    while (1)

    {

        scanf("%d", &n);

        if (!n) break;

        init();

        solve();

    }



    return 0;

}

/**/

你可能感兴趣的:(DFS)