1663: [Usaco2006 Open]赶集
Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 384 Solved: 217
[ Submit][ Status][ Discuss]
Description
Every year, Farmer John loves to attend the county fair. The fair has N booths (1 <= N <= 400), and each booth i is planning to give away a fabulous prize at a particular time P(i) (0 <= P(i) <= 1,000,000,000) during the day. Farmer John has heard about this and would like to collect as many fabulous prizes as possible to share with the cows. He would like to show up at a maximum possible number of booths at the exact times the prizes are going to be awarded. Farmer John investigated and has determined the time T(i,j) (always in range 1..1,000,000) that it takes him to walk from booth i to booth j. The county fair's unusual layout means that perhaps FJ could travel from booth i to booth j by a faster route if he were to visit intermediate booths along the way. Being a poor map reader, Farmer John never considers taking such routes -- he will only walk from booth i to booth j in the event that he can actually collect a fabulous prize at booth j, and he never visits intermediate booths along the way. Furthermore, T(i,j) might not have the same value as T(j,i) owing to FJ's slow walking up hills. Farmer John starts at booth #1 at time 0. Help him collect as many fabulous prizes as possible.
Input
* Line 1: A single integer, N.
* Lines 2..1+N: Line i+1 contains a single integer, P(i). * Lines 2+N..1+N+N^2: These N^2 lines each contain a single integer T(i,j) for each pair (i,j) of booths. The first N of these lines respectively contain T(1,1), T(1,2), ..., T(1,N). The next N lines contain T(2,1), T(2,2), ..., T(2,N), and so on. Each T(i,j) value is in the range 1...1,000,000 except for the diagonals T(1,1), T(2,2), ..., T(N,N), which have the value zero.
第1行输入一个正整数N.接下来N行每行一个整数Pi.
接下来N^2行,输入乃,J.先输入T1,1 T1,2 T1,3…T1,N再输入T2,1 T2,2…T2,N依次类推.
Output
* Line 1: A single integer, containing the maximum number of prizes Farmer John can acquire.
输出一个整数,即约翰最多能拿到的礼物的个数
Sample Input
Sample Output
对于点对(i, j),如果拿到i点奖励之后跑到点j还能拿到j点奖励,那么i到j连接一条长度为1的有向边
新建一个节点x,如果一开始直接到第i个点可以拿到i点奖励,那么x到i连接一条长度为1的有向边
这样可以构成一个有向无环图,求出点0到所有点的最长路就是答案
#include<stdio.h>
#include<queue>
#include<vector>
#include<algorithm>
using namespace std;
vector<int> G[405];
queue<int> q;
int t[405][405], val[405], bet[405];
int main(void)
{
int n, i, j, ans, u, v;
scanf("%d", &n);
for(i=1;i<=n;i++)
scanf("%d", &val[i]);
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
scanf("%d", &t[i][j]);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(i==j)
continue;
if(val[i]+t[i][j]<=val[j])
G[i].push_back(j);
}
}
for(i=1;i<=n;i++)
{
if(t[1][i]<=val[i])
G[0].push_back(i);
}
ans = 0;
q.push(0);
while(q.empty()==0)
{
u = q.front();
q.pop();
for(i=0;i<G[u].size();i++)
{
v = G[u][i];
if(bet[u]+1>bet[v])
{
bet[v] = bet[u]+1;
ans = max(ans, bet[v]);
q.push(v);
}
}
}
printf("%d\n", ans);
return 0;
}
/*
4
13
9
19
3
0 10 20 3
4 0 11 2
1 15 0 12
5 5 13 0
*/