Free DIY Tour
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5744 Accepted Submission(s): 1835
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 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
用DFS直接搜索最长路径
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>
using namespace std;
const int INF = 0x7fffffff;
int N, M, maxn;
//dis记录距离,pre记录某个点的前驱,mapp记录可达性
//ans记录最后的路径
int dis[105], pre[105], mapp[105][105], ans[105], len;
void init() {
for (int i = 1; i <= N; i++) {
pre[i] = i;
}
memset(mapp, 0, sizeof(mapp));
memset(ans, 0, sizeof(ans));
maxn = -INF;
}
void dfs(int pos, int sum) {
if (pos == N + 1) {
if (maxn < sum) {
maxn = sum;
ans[0] = 1;
len = 1;
while (pos != pre[pos]) {
ans[len++] = pre[pos];
pos = pre[pos];
}
}
return;
}
for (int i = 1; i <= N + 1; i++) {
if (mapp[pos][i]) {
pre[i] = pos;
dfs(i, sum + dis[i]);
pre[i] = i;
}
}
}
int main()
{
int T;
scanf("%d", &T);
for (int t = 1; t <= T; t++) {
scanf("%d", &N);
init();
for (int i = 1; i <= N; i++) {
scanf("%d", dis + i);
}
dis[N + 1] = 0;
scanf("%d", &M);
for (int i = 1; i <= M; i++) {
int a, b;
scanf("%d%d", &a, &b);
mapp[a][b] = 1;
}
dfs(1, dis[1]);
printf("CASE %d#\n", t);
printf("points : %d\n", maxn);
printf("circuit : ");
for (int i = len - 1; i >= 0; i--) {
printf(i ? "%d->" : "%d\n", ans[i]);
}
if (t != T) puts("");
}
return 0;
}
dp做法
#include <cstdio>
#include <cstring>
#include <iostream>
#include <stack>
using namespace std;
int a[105], dp[105], pre[105], N, M;
int mapp[105][105];
void init() {
memset(a, 0, sizeof(a));
memset(dp, 0, sizeof(dp));
memset(mapp, 0, sizeof(mapp));
for (int i = 1; i <= N; i++) {
pre[i] = i;
}
}
int main()
{
int T;
scanf("%d", &T);
for (int t = 1; t <= T; t++) {
scanf("%d", &N);
init();
for (int i = 1; i <= N; i++) {
scanf("%d", &a[i]);
}
a[N + 1] = 0;
scanf("%d", &M);
for (int i = 1; i <= M; i++) {
int a, b;
scanf("%d%d", &a, &b);
mapp[a][b] = 1;
}
for (int i = 1; i <= N + 1; i++) {
for (int j = 1; j < i; j++) {
if (mapp[j][i]) {
//dp更新最优解
if (dp[j] + a[i] > dp[i]) {
dp[i] = dp[j] + a[i];
pre[i] = j;
}
}
}
}
printf("CASE %d#\n", t);
printf("points : %d\n", dp[N + 1]);
printf("circuit : ");
int ans[105], len = 0;
int pos = N + 1;
ans[0] = 1; len = 1;
while (pos != pre[pos]) {
ans[len++] = pre[pos];
pos = pre[pos];
}
for (int i = len - 1; i >= 0; i--) {
printf(i ? "%d->" : "%d\n", ans[i]);
}
if (t != T) puts("");
}
return 0;
}