标签:
这道题也可以用深搜做,可以深搜本来就不熟,好久没做早忘了,明天看看咋做的
Description
Input
Output
Sample Input
Sample Output
#include <stdio.h> #include <string.h> bool link[105][105]; int intrest[105], dp[105], last[105], path[105]; int main() { int t, case_num = 1; //freopen("input.txt", "r", stdin); scanf("%d", &t); while(t--) { int n, m, i, j; memset(link, 0, sizeof(link)); memset(dp, 0, sizeof(dp)); last[1] = 0; scanf("%d", &n); if(case_num != 1) printf("\n"); for(i = 1; i <= n; i++) { scanf("%d", &intrest[i]); } intrest[i] = 0; //注意i城市有趣度为0!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! scanf("%d", &m); for(int i = 0; i < m; i++) { int a, b; scanf("%d%d", &a, &b); link[a][b] = link[b][a] = 1; } for(i = 2; i <= n+1; i++) //假设目标城市标号为i(注意到达它之前经过的城市的标号都小于它) { for(j = 1; j < i; j++) //遍历到达i城市之前所在的城市的标号的所有可能性, //更新到达i城的时候的有趣度之和为所有情况中最大的 { if(dp[j]+intrest[i] > dp[i] && link[i][j]) { dp[i] = dp[j]+intrest[i]; last[i] = j; } } } j = 0; i = n+1; while(last[i]) { path[j++] = last[i]; i = last[i]; } printf("CASE %d#\n", case_num++); printf("points : %d\n", dp[n+1]); printf("circuit : "); for(i = j-1; i >= 0; i--)//注意输出的个数并非为城市数目!!!!!!!!!!!!! { printf("%d->", path[i]); } printf("1\n"); } return 0; }
标签:
原文地址:http://www.cnblogs.com/rain-1/p/4893271.html