F-free DIY Tour (Dynamic planning)

Source: Internet
Author: User

This problem can also use deep search to do, can deep search is not familiar, long time did not do early forget, tomorrow to see how to do

Description

Weiwei 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 last CI Ty, so we mark Hangzhou both 1and n+1), and its 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?

Input

The 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.

Output

For each case, your task was to output the maximal summation of interesting points Weiwei and his fellow workers can get th Rough 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 Input

230 70 9041 21 32 43 430 90 7041 21 32 43 4

Sample Output

Case 1#points:90circuit:1->3->1case 2#points:90circuit:1->2->1 Topic source Reference Blog Question: Enter the number of test cases T Enter the number of cities n sequentially enter The interesting degree of each city in cities from 1 to n enter the number of roads m input m group data (A, B) indicates that A and B cities are connected known: The starting city is labeled 1 and n+1, because it is not only the starting point, but also the end point,its interesting degree is 0 (must remember when initializing, otherwise it will be very miserable ah ah ...)Only from the small city to the marking of the city wrong answer reason: the initialization of the time ignored intrest[n+1] = 0; The output path of the time note is not through all the city, so the number of cities output is not equal to the total number of city error points: Note that there is a line between each case, there is no empty line analysis: with LINK[I][J] storage I city and J City is connected, Intrest[i] Storage city I the fun degree of dp[i] store the maximum fun time to reach I city dp[i] = max (Dp[j]+intrest[i], dp[i]) before reaching I city can be in any city except I city and starting point, traverse the city before I city for any city, put to Up I city when the fun update for one of the largest of the numbers in the code specifically said
#include <stdio.h>#include<string.h>BOOLlink[ the][ the];intintrest[ the], dp[ the], last[ the], path[ the];intMain () {intT, Case_num =1; //freopen ("Input.txt", "R", stdin);scanf"%d", &t);  while(t--)    {        intN, M, I, J; memset (Link,0,sizeof(link)); Memset (DP,0,sizeof(DP)); last[1] =0; scanf ("%d", &N); if(Case_num! =1) printf ("\ n");  for(i =1; I <= N; i++) {scanf ("%d", &Intrest[i]); } Intrest[i]=0;//Note I the City fun degree is 0!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!scanf"%d", &m);  for(inti =0; I < m; i++)        {            intA, B; scanf ("%d%d", &a, &b); LINK[A][B]= Link[b][a] =1; }         for(i =2; I <= n+1; i++)//assume that the target city is labeled I (note that the mark of the city passing before it reaches it is smaller than it)        {             for(j =1; J < I; J + +)                //all possibilities of traversing the marking of the city where I was before the city was reached,//Update to the I city when the fun of the sum for the largest in all cases            {                if(Dp[j]+intrest[i] > Dp[i] &&Link[i][j]) {Dp[i]= dp[j]+Intrest[i]; Last[i]=J; } }} J=0; I= n+1;  while(Last[i]) {path[j++] =Last[i]; I=Last[i]; } printf ("Case %d#\n", case_num++); printf ("points:%d\n", dp[n+1]); printf ("Circuit:");  for(i = j-1; I >=0; i--)//Note that the number of outputs is not the number of cities!!!!!!!!!!!!! {printf ("%d->", Path[i]); } printf ("1\n"); }    return 0;}

F-free DIY Tour (Dynamic planning)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.