您的位置:首页 > 其它

HDU - 2614 Beat

2016-07-26 15:25 465 查看
巨坑!

题目:

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

3
0 0 0
1 0 1
1 0 0
3
0 2 2
1 0 1
1 1 0
5
0 1 2 3 1
0 0 2 3 1
0 0 0 3 1
0 0 0 0 2
0 0 0 0 0  

Sample Output

3
2
4

Hint

Hint: sample one, as we know zty always solve problem 0 by costing 0 minute. So after solving problem 0, he can choose problem 1 and problem 2, because T01 >=0 and T02>=0. But if zty chooses to solve problem 1, he can not solve problem 2, because T12 < T01. So zty can choose solve the problem 2 second, than solve the problem 1.


且不说Hint写的是错的,因为这并没有影响我理解题意。

但是!

对角线根本就没有意义,然后看那三组输入示例,我下意识的以为输入的对角线应该都是0吧。

10点39第一次提交就是Wrong Answer,后面一直是错的,然后中午睡了会,下午继续分析,

代码被化简到了非常恐怖的地步,然而还是Wrong Answer。

然后和同学讨论题意,看是不是我理解错了。但是没有!题意就是我理解的那样!

讨论的过程中无意间想到,输入的对角线不一定都是0,这个地方会导致我的计算错误。

我的代码是:

#include<iostream>
using namespace std;

int n;
int list[17][17];
bool f[17];

int dfs(int a, int b)
{
f[b] = false;
int s = 0;
for (int i = 0; i < n; i++)if (f[i] && list[b][i] >= list[a][b] && s < dfs(b, i) + 1)s = dfs(b, i) + 1;
f[b] = true;
return s;
}

int main()
{
while (cin >> n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)cin >> list[i][j];
f[i] = true;
}
list[0][0] = 0;
cout << dfs(0, 0) + 1 << endl;
}
return 0;
}


之前就是因为main函数里面少了一句list[0][0]=0;
所以就错了。

超级坑爹啊啊啊啊。。。。

当然,这个代码还有点问题,时间代价是正常代码的指数级倍。运行时间是1326 ms

稍作修改即可得到运行时间为62 ms的代码:

#include<iostream>
using namespace std;

int n;
int list[17][17];
bool f[17];
int t;

int dfs(int a, int b)
{
f[b] = false;
int s = 0;
for (int i = 0; i < n; i++)
if (f[i] && list[b][i] >= list[a][b])
{
t = dfs(b, i) + 1;
if(s<t)s =t;
}
f[b] = true;
return s;
}

int main()
{
while (cin >> n)
{
for (int i = 0; i < n; i++)
{
for (int j = 0; j < n; j++)cin >> list[i][j];
f[i] = true;
}
list[0][0] = 0;
cout << dfs(0, 0) + 1 << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: