题目就不透露了
一开始在考虑存储数据结构的时候花费了比较多的时间,结果没来得及写完,考完以后又花了20分钟把程序调试完了
#include <iostream>
#include <vector>
using namespace std;
void visitnode(vector<vector<int>> &relation, vector<int> &nodetime, int a, int b, int N, int &count, int &maxcost, int curcost)
{
if (relation[a][b] == 0)
return;
else //(relation[a][b] == 1)
{
curcost += nodetime[b];
int haschild = 0;
for (int i = 0; i < N; i++)
{
if (relation[b][i] == 1)
{
haschild++;
visitnode(relation, nodetime, b, i, N, count, maxcost, curcost);
}
}
if (haschild == 0)
{
count++;
if (curcost > maxcost)
maxcost = curcost;
}
curcost -= nodetime[b];
}
}
int main()
{
int N,M;
cin >> N>> M;
vector<int> nodetime(N,0);
vector<int> ishead(N, 0);
for (int i = 0; i < N; i++)
{
cin >> nodetime[i];
}
vector<vector<int>> relation(N, vector<int>(N, 0));
for (int i = 0; i < M; i++)
{
int a, b;
cin >> a >> b;
relation[a-1][b-1] = 1;
ishead[a - 1]++;
ishead[b - 1] = -1;
}
int nums = 0;
int maxcost =0;
for (int i = 0; i < N; i++)
{
if (ishead[i] >0)
for (int j = 0; j < N; j++)
visitnode(relation, nodetime, i, j, N, nums, maxcost, nodetime[i]);
}
cout << nums << " " << maxcost;
return 0;
}