Hdu1224_free DIY Tour "DP" "backtracking"

Source: Internet
Author: User

Free DIY Tour

Time limit:2000/1000 MS (java/others) Memory limit:65536/32768 K (java/others) total submission (s): 4336 Accepted S Ubmission (s): 1391
Problem 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 1 and n+1), and their 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
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 #
Points:90
Circuit:1->3->1

Case
Points:90
Circuit:1->2->1

Author

Jgshining (Aurora Dazzle)


A DIY trip, a total of n cities, each city has a

interesting values, but there is not necessarily a direct flight between cities. Give you city flights .

Case Each time from the first city, and finally back to the first city, forming a ring

Lu. Ask what makes the most interesting value the best loop and what its interesting value is.

Idea: Set Dp[i] for the largest interesting value obtained from City 1 to the first city, and update dp[i]

, use Pre[i] to record the front of the current node, and then backtrack forward and record the path.

Then output the path.


#include <stdio.h> #include <string.h>int point[110],map[110][110],pre[110],dp[110],ans[110];int main ()    {int t,n,m,kase = 1;    scanf ("%d", &t);        while (t--) {memset (point,0,sizeof (point));        memset (map,0,sizeof (map));        memset (pre,0,sizeof (pre));        Memset (Dp,0,sizeof (DP));        scanf ("%d", &n);        for (int i = 1; I <= N; i++) scanf ("%d", &point[i]);        Point[n+1] = 0;        scanf ("%d", &m);        int x, y;            for (int i = 0; i < M; i++) {scanf ("%d%d", &x,&y);        Map[x][y] = 1;        } Pre[1] =-1; for (int i = 1, i <= n+1; i++) {for (int j = 1; j < I; J + +) {if (map[j][i                    ] && Dp[i] < Dp[j] + Point[i]) {dp[i] = Dp[j] + point[i];                Pre[i] = j;        }}} printf ("Case%d#\n", kase++); printf ("Points:%d\n", dp[n+1]);        printf ("Circuit:");        int i = n+1;        int j = 0;            while (Pre[i]!=-1) {ans[j++] = Pre[i];        i = pre[i];        } for (int i = j-1; I >= 0; i--) printf ("%d->", Ans[i]);        printf ("1\n");    if (t!=0) printf ("\ n"); } return 0;}



Hdu1224_free DIY Tour "DP" "backtracking"

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.