您的位置:首页 > 其它

HDU 1224 Free DIY Tour——DP

2017-08-16 22:39 477 查看
题目虽然长,但实际上就是让你找1到n+1的一条权值最大的路径,并输出这条路径(从1到n+1,并且n+1输出时视为1)

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

int n, a[110], dp[110], graph[110][110], pre[110];

void print(int x) {
if (x == 1) {
printf("1");
return;
}
print(pre[x]);
printf("->%d", x);
}

int main()
{
int T; scanf("%d", &T);
for (int kase = 1; kase <= T; kase++) {
if (kase != 1) printf("\n");
memset(dp, 0, sizeof(dp));
memset(pre, 0, sizeof(pre));
memset(graph, 0, sizeof(graph));
int n; scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &a[i]);
}
a[n + 1] = 0;
int m; scanf("%d", &m);
while (m--) {
int a, b; scanf("%d %d", &a, &b);
if (a > b) swap(a, b);
graph[b][a] = 1;
}
for (int i = 1; i <= n + 1; i++) {
for (int j = 1; j < i; j++) {
if (graph[i][j] && dp[j] + a[i] > dp[i]) {
dp[i] = dp[j] + a[i];
pre[i] = j;
}
}
}
printf("CASE %d#\npoints : %d\n", kase, dp[n + 1]);
printf("circuit : ");
print(pre[n + 1]);
printf("->%d\n", 1);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: