HDOJ 2614 Beat

来源:互联网 发布:八实万是网络传销吗 编辑:程序博客网 时间:2024/06/06 05:01

Beat




Problem Description
Zty is a man that always full of enthusiasm. He wants to solve every kind of difficulty ACM problem in the world. And he has a habit that he does not like to solve
a problem that is easy than problem he had solved. Now yifenfei give him n difficulty problems, and tell him their relative time to solve it after solving the other one.
You should help zty to find a order of solving problems to solve more difficulty problem. 
You may sure zty first solve the problem 0 by costing 0 minute. Zty always choose cost more or equal time’s problem to solve.
 

Input
The input contains multiple test cases.
Each test case include, first one integer n ( 2< n < 15).express the number of problem.
Than n lines, each line include n integer Tij ( 0<=Tij<10), the i’s row and j’s col integer Tij express after solving the problem i, will cost Tij minute to solve the problem j.
 

Output
For each test case output the maximum number of problem zty can solved.


 

Sample Input
30 0 01 0 11 0 030 2 21 0 11 1 050 1 2 3 10 0 2 3 10 0 0 3 10 0 0 0 20 0 0 0 0
 

Sample Output
324
题目大意:一个人喜欢解决难题。现在给出一个矩阵T,Tij表示解决完i问题后解决j问题需要的时间,已知他解决0号问题需要0分钟,但是他不喜欢解决耗费时间比已经解决的问题耗费时间少的问题,问他能解决的最多问题数是多少。

解题思路:DFS,利用辅助数组标记问题是否已经被解决。注意要将T00手动设置为0。

代码如下:

#include <cmath>#include <cstdio>#include <algorithm>#include <cstring>using namespace std;const int maxn = 16;int problem[maxn][maxn];bool solved[maxn];int ans;int n;void dfs(int nth,int dep,int time){    for(int i = 0;i < n;i++){        if(solved[i] || problem[nth][i] < time)            continue;        solved[i] = 1;        dfs(i,dep + 1,problem[nth][i]);        solved[i] = 0;    }    ans = max(ans,dep);}int main(){    while(scanf("%d",&n) != EOF){        for(int i = 0;i < n;i++){            for(int j = 0;j < n;j++){                scanf("%d",&problem[i][j]);            }        }        problem[0][0] = 0;        ans = 0;        memset(solved,0,sizeof(solved));        solved[0] = 1;        dfs(0,1,0);        printf("%d\n",ans);    }    return 0;}


0 0
原创粉丝点击