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;
}
#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;
}
相关文章推荐
- HDU 1224 Free DIY Tour (DP again!)
- hdu 1224 Free DIY Tour(DP多阶段决策)
- hdu 1224 Free DIY Tour(DP多阶段决策)
- 【数塔DP|最长路】HDU-1224 Free DIY Tour
- HDU 1224 Free DIY Tour--DP--(bug集锦)
- hdu 1224 Free DIY Tour(最长路/dp)
- HDU 1224 Free DIY Tour(DP)
- hdu 1224 Free DIY Tour(dp)
- HDU 1224 Free DIY Tour(简单dp?+记录路径)
- HDU 1224 Free DIY Tour(DP求最短路)
- hdu1224 Free DIY Tour--DP
- HDU 1224 Free DIY Tour(DP)
- HDU 1224 Free DIY Tour 简单DP
- hdu 1224 Free DIY Tour(dp)
- hdu 1224 Free DIY Tour(最长的公路/dp)
- HDU 1224 Free DIY Tour
- hdu 1224 Free DIY Tour 【动态规划】
- hdu 1224 Free DIY Tour(动态规划)
- HDU1224 Free DIY Tour(最短路变形,最长路打印路径,spfa)
- HDU - 1224 Free DIY Tour DP