Question:
Free DIY Tour
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission (s): 1894 Accepted Submission (s): 619
Problem 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 andN + 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 in 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
Analysis and Summary:
It is a bit like the TSP problem, but this question has already told us that we will regard the point that is finally returned to 1 as n + 1. Therefore, the request is calculated as 1 ~ The longest path of n + 1. Save the path for this question.
You can use the single-source shortest path or the Floyd recorded path method you just learned.
Note that when n + 1 is output, convert it to output 1.
Code:
1. SPFA
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <queue>
Using namespace std;
Const int INF = 0 xfffffff;
Const int VN = 105;
Int n, m;
Int w [VN] [VN];
Int point [VN];
Int d [VN];
Int pre [VN];
Bool inq [VN];
Int ans [VN];
Void init (){
Point [n] = 0;
For (int I = 0; I <= n; ++ I ){
W [I] [I] = INF;
For (int j = I + 1; j <= n; ++ j)
W [I] [j] = w [j] [I] = INF;
}
}
Void SPFA (int src ){
Memset (inq, 0, sizeof (inq ));
Memset (pre,-1, sizeof (pre ));
For (int I = 1; I <= n; ++ I) d [I] =-INF;
D [src] = 0;
Queue <int> q;
Q. push (src );
While (! Q. empty ()){
Int u = q. front (); q. pop ();
Inq [u] = false;
For (int v = 1; v <= n; ++ v) if (w [u] [v]! = INF ){
Int tmp = d [u] + w [u] [v];
If (d [v] <tmp ){
D [v] = tmp;
Pre [v] = u;
If (! Inq [v]) {
Inq [v] = true;
Q. push (v );
}
}
}
}
}
Void print_path (int u ){
If (pre [u] =-1 ){
Printf ("1 ");
Return;
}
Print_path (pre [u]);
If (u = n) printf ("-> % d", 1 );
Else printf ("-> % d", u );
}
Int main (){
Int T, u, v, cas = 1;
Scanf ("% d", & T );
While (T --){
Scanf ("% d", & n );
++ N;
Init ();
For (int I = 1; I <n; ++ I)
Scanf ("% d", & point [I]);
Scanf ("% d", & m );
For (int I = 0; I <m; ++ I ){
Scanf ("% d", & u, & v );
W [u] [v] = point [v];
}
SPFA (1 );
If (cas! = 1) puts ("");
Printf ("CASE % d # \ n", cas ++ );
Printf ("points: % d \ n", d [n]);
Printf ("circuit :");
Print_path (n );
Puts ("");
}
Return 0;
}
2. Floyd
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <cstring>
# Include <queue>
Using namespace std;
Const int INF =-0 xfffffff;
Const int VN = 105;
Int n, m;
Int w [VN] [VN];
Int path [VN] [VN];
Int point [VN];
Int d [VN];
Bool inq [VN];
Int ans [VN];
Void init (){
Point [n] = 0;
For (int I = 0; I <= n; ++ I ){
For (int j = 1; j <= n; ++ j ){
If (I = j) w [I] [j] = 0;
Else w [I] [j] = INF;
Path [I] [j] = j;
}
}
}
Void Floyd (){
For (int k = 1; k <= n; ++ k)
For (int I = 1; I <= n; ++ I)
For (int j = 1; j <= n; ++ j) if (w [I] [k]! = INF & w [k] [j]! = INF ){
Int tmp = w [I] [k] + w [k] [j];
If (w [I] [j] <tmp ){
W [I] [j] = tmp;
Path [I] [j] = path [I] [k];
}
}
}
Int main (){
Int T, u, v, cas = 1;
Scanf ("% d", & T );
While (T --){
Scanf ("% d", & n );
++ N;
Init ();
For (int I = 1; I <n; ++ I)
Scanf ("% d", & point [I]);
Scanf ("% d", & m );
For (int I = 0; I <m; ++ I ){
Scanf ("% d", & u, & v );
W [u] [v] = point [v];
}
Floyd ();
If (cas! = 1) puts ("");
Printf ("CASE % d # \ n", cas ++ );
Printf ("points: % d \ n", w [1] [n]);
Printf ("circuit: 1 ");
Int u = 1;
While (u! = N ){
Printf ("-> % d", path [u] [n] = n? 1: path [u] [n]);
U = path [u] [n];
}
Puts ("");
}
Return 0;
}