Free DIY Tour
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 1001 Accepted Submission(s): 331
The tour company shows them a new kind of tour circuit - DIY circuit. Each circuit contains some cities which can be selected by tourists themselves. According to the company's statistic, each city has its own interesting point. For instance, Paris has its interesting point of 90, New York has its interesting point of 70, ect. Not any two cities in the world have straight flight so the tour company provide a map to tell its tourists whether they can got a straight flight between any two cities on the map. In order to fly back, the company has made it impossible to make a circle-flight on the half way, using the cities on the map. That is, they marked each city on the map with one number, a city with higher number has no straight flight to a city with lower number.
Note: Weiwei always starts from Hangzhou(in this problem, we assume Hangzhou is always the first city and also the last city, so we mark Hangzhou both1 and N+1), and its interesting point is always 0.
Now as the leader of the team, Weiwei wants to make a tour as interesting as possible. If you were Weiwei, how did you DIY it?
Each case will begin with an integer N(2 ≤ N ≤ 100) which is the number of cities on the map.
Then N integers follows, representing the interesting point list of the cities.
And then it is an integer M followed by M pairs of integers [Ai, Bi] (1 ≤ i ≤ M). Each pair of [Ai, Bi] indicates that a straight flight is available from City Ai to City Bi.
Output a blank line between two cases.
2 3 0 70 90 4 1 2 1 3 2 4 3 4 3 0 90 70 4 1 2 1 3 2 4 3 4
CASE 1# points : 90 circuit : 1->3->1 CASE 2# points : 90 circuit : 1->2->1
/*起初以为这题是用Floyd算法,便起手尝试着做,Floyd的路径保存和输出又不懂,结果一直WA。。
后面才知道原来只要简单的DP就可以求出最长(兴趣点最高)路径,需要注意的还是路径的输出,DP时用pre[x]保存访问顶点x时的前驱,这样就OK了。
if (map[j][i]!=-1&& dp[j] + map[j][i] > dp[i] ) //状态转移要判断从j到i是否可以走通
dp[i] = dp[j] + map[j][i];*/
输出有点不明白!!!!!
#include <stdio.h>
#include <string.h>
int map[110][110];
int dp[110];
int pre[110];
int inst[110];
void output( int x )
{
if ( x == -1)
return;
output(pre[x]);
printf("%d->",x);
}
int main()
{
int T,n,m;
int i,j,k;
int cases = 1;
scanf("%d",&T);
while ( T-- )
{
scanf("%d",&n);
memset(map,-1,sizeof(map));
memset(dp,0,sizeof(dp));
for ( i = 1; i <= n; i++)
scanf("%d",&inst[i]);
inst[n+1] = 0;
scanf("%d",&m);
for ( k = 1; k <= m; k++)
{
scanf("%d%d",&i,&j);
map[i][j] = inst[j];
}
pre[1] = -1;
for ( i = 1; i <= n + 1; i++)
for ( j = 1; j < i; j++)
{
if (map[j][i]!=-1&& dp[j] + map[j][i] > dp[i] )
{
dp[i] = dp[j] + map[j][i];
pre[i] = j;
}
}
if ( cases>1 )printf("\n");
printf("CASE %d#\n",cases++);
printf("points : %d\n",dp[n+1]);
printf("circuit : ");
output(pre[n+1]);
printf("1\n");
}
return 0;
}