Free DIY Tour
problem DescriptionWeiwei is a software engineer of Shiningsoft. He has just excellently fulfilled a software project with his fellow workers. His boss was so satisfied with their job, that he decide to provide them a free tour around the world. It ' s a good chance to relax themselves. To most of them, it's the first time to go abroad so they decide to make a collective tour.
The tour company shows them a new Kind's tour circuit-diy circuit. Each circuit contains some cities which can is selected by tourists themselves. According to the company's statistic, each of the city have its own interesting point. For instance, Paris have its interesting point of a, New York have its interesting point of a, ect. Not any of cities in the world has straight flight so the tour company provide a map to the IT tourists whether they c An got a straight flight between any and cities on the map. In order to fly back, the company had made it impossible to make a circle-flight on the half, using the cities on the Map. That's, they marked, the map with one number, a city with higher number have no straight flight to a city with Lower number.
Note:weiwei always starts from Hangzhou (in this problem, we assume Hangzhou are always the first city and also the Las T city, so we mark Hangzhou both
1 and
n+1), and it interesting point are always 0.
Now as the leader of the team, Weiwei wants to make a tour as interesting as possible. If You were Weiwei, what did do you DIY it?
InputThe input would contain several cases. The first line was an integer T which suggests the number of cases. Then T cases follows.
Each case would begin with a 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's an integer M followed by M pairs of integers [Ai, Bi] (1≤i≤m). Each pair of [AI, Bi] indicates this a straight flight is available from the city Ai to City Bi.
Outputfor each case, your task was to output the maximal summation of interesting points Weiwei and his fellow workers can ge T through optimal diying and the optimal circuit. The format is as the sample. You may assume this there is only one optimal circuit.
Output a blank line between cases.
Sample Input2304121 32 43 43041 21 32 43 4
Sample Output Case #points:90circuit:1->3->1 Casepoints: -circuit:1->2->1Test instructions: N City, numbered 1 to N, then gives an interesting value for each city, which stipulates 1 is 0give some more directions,
ask now which road to travel from 1, and then go back to 1, the most interesting value. Moreover, the title stipulates that the number of cities cannot be returned to small cities(just start to look at the interesting value of the big can not be interesting value small)So, the problem is much easier. suppose not to return to 1, but to go to a city, for N+1, whose value is INFis to find a subsequence that makes the sequence and the maximumthe final and minus INFcondition: There is a way Dp[i] Represents the maximum interesting value from 1 to i
1#include <iostream>2#include <cstring>3#include <algorithm>4 5 using namespacestd;6 7 Const intmaxn=104;8 Const intinf=0x3f3f3f;9 Ten One BOOLVIS[MAXN][MAXN];//Mark 2 points if there is a way A intDP[MAXN];//represents the maximum value from 1 to i - intPRE[MAXN]; - intPOI[MAXN]; the - voidOutputintCNT) - { - if(pre[cnt]!=-1) + output (pre[cnt]); -cout<<cnt<<" -"; + } A at intMain () - { - inttest; - intcas=1; - -Cin>>test; in - while(test--) to { + intN; -Cin>>N; the * for(intI=1; i<=n;i++) $Cin>>Poi[i];Panax Notoginseng - intm; theCin>>m; + Amemset (Vis,false,sizeof(Vis)); the + for(intI=1; i<=m;i++) - { $ intu,v; $Cin>>u>>v; - -vis[u][v]=vis[v][u]=true; the } - Wuyipoi[n+1]=inf; theMemset (DP,0,sizeof(DP)); -memset (pre,-1,sizeof(pre)); Wu - for(intI=1; i<=n+1; i++) About for(intj=1; j<i;j++) $ { - if(vis[i][j]&&dp[i]<dp[j]+Poi[i]) - { -dp[i]=dp[j]+Poi[i]; Apre[i]=J; + } the } - $ if(cas>1) thecout<<"\ n"; the thecout<<" Case"<<cas++<<"#"<<Endl; thecout<<"points:"<<dp[n+1]-poi[n+1]<<Endl; -cout<<"Circuit:"; inOutput (pre[n+1]); thecout<<1<<Endl; the } About return 0; the}
View Code
HDU 1224 free DIY Tour simple DP