Free DIY Tour

Description

Weiwei is a software engineer of ShiningSoft. He has just excellently fulfilled a software project with his fellow workers. His boss is 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 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 both 1 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? 
 

Input

The input will contain several cases. The first line is an integer T which suggests the number of cases. Then T cases follows.  
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

For each case, your task is to output the maximal summation of interesting points Weiwei and his fellow workers can get through optimal DIYing and the optimal circuit. The format is as the sample. You may assume that there is only one optimal circuit.  

Output a blank line between two cases. 
 

Sample Input

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
 

Sample Output

CASE 1#
points : 90
circuit : 1->3->1
 
CASE 2#
points : 90
circuit : 1->2->1
 
 
       题意:给一个数t,表示有t个例子。每个例子先给一数n,表示n座城市,在给n个数,表示n个城市的风景值。给个数m,再给m对数,没对数表示那俩个城市连通,求从第1城市出发,到n+1城市能看到的最大风景值。
       英文题,一定要看清楚细节。他只能从小城市飞往大城市,不能从大城市飞往小城市。还有从1城市出发,最后回到的是n+1城市。
       这题用dfs和dp都能做。
     
      dfs:
 
 
 1 #include<cstdio>

 2 #include<cstring>

 3 using namespace std;

 4 int s[110],s1[110][110],x[110],bj[110],n,m,max;

 5 int jl[110],cd;

 6 void dfs(int p,int sum,int cut)

 7 {

 8     int i,j;

 9     for (i=cut+1;i<=n;i++)

10     {

11         if (s1[cut][i]==1&&bj[i]==0)

12         {

13             x[p]=i;

14             bj[i]=1;

15             if (s1[i][n+1]==1)

16             {

17                 x[p+1]=1;

18                 if (sum+s[i]>max)

19                 {

20                     max=sum+s[i];

21                     cd=p+1;

22                     for( j=0;j<=cd;j++) jl[j]=x[j];

23                 }

24             }

25             dfs(p+1,sum+s[i],i);

26             bj[i]=0;

27         }

28     }

29 }

30 int main()

31 {

32     int t,i,a,b,k=0;

33     scanf("%d",&t);

34     while (t--)

35     {

36         k++;

37         if (k!=1) printf("\n");

38         max=0;

39         cd=1;

40         memset(bj,0,sizeof(bj));

41         memset(s1,0,sizeof(s1));

42         scanf("%d",&n);

43         for (i=1;i<=n;i++) scanf("%d",&s[i]);

44         scanf("%d",&m);

45         for (i=1;i<=m;i++)

46         {

47             scanf("%d%d",&a,&b);

48             s1[a][b]=1;

49             s1[b][a]=1;

50         }

51         x[0]=1;

52         dfs(1,0,1);

53         printf("CASE %d#\n",k);

54         printf("points : %d\n",max);

55         printf("circuit : ");

56         if (cd!=1){

57         for (i=0;i<cd;i++) printf("%d->",jl[i]);

58         printf("%d\n",jl[cd]);

59         }

60     }

61     return 0;

62 }

 

    dp

 

 

 1 #include<cstdio>

 2 #include<cstring>

 3 using namespace std;

 4 int s[110],s1[110][110],dp[110],x[110],n,m;

 5 int main()

 6 {

 7     int t,i,j,a,b,k=0;

 8     scanf("%d",&t);

 9     while (t--)

10     {

11         k++;

12         x[1]=0;

13         if (k!=1) printf("\n");

14         memset(s1,0,sizeof(s1));

15         memset(dp,0,sizeof(dp));

16         scanf("%d",&n);

17         for (i=1;i<=n;i++) scanf("%d",&s[i]);

18         s[i]=0;

19         scanf("%d",&m);

20         for (i=1;i<=m;i++)

21         {

22             scanf("%d%d",&a,&b);

23             s1[a][b]=1;

24             s1[b][a]=1;

25         }

26         for(i=2;i<=n+1;i++)

27         {

28             for (j=1;j<i;j++)

29             {

30                 if (dp[j]+s[i]>dp[i]&&s1[j][i]==1)

31                 {

32                     dp[i]=dp[j]+s[i];

33                     x[i]=j;

34                 }

35             }

36         }

37         i=n+1;

38         j=0;

39         while (x[i])

40         {

41            s[j]=x[i];

42            i=x[i];

43            j++;

44         }

45         printf("CASE %d#\n",k);

46         printf("points : %d\n",dp[n+1]);

47         printf("circuit : ");

48         if (j!=0){

49         for (j=j-1;j>=0;j--) printf("%d->",s[j]);

50         printf("1\n");

51         }

52     }

53     return 0;

54 }

 

你可能感兴趣的:(free)