hdu 1224(最长路)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1224
我感觉就是求最长上升上升序列
View Code
1 #include<iostream> 2 #include<cstring> 3 using namespace std; 4 const int MAXN=110; 5 int n,m; 6 int dp[MAXN]; 7 int value[MAXN]; 8 int path[MAXN]; 9 bool map[MAXN][MAXN]; 10 11 void Print(int u){ 12 if(u==-1)return ; 13 Print(path[u]); 14 printf("%d->",u); 15 } 16 17 int main(){ 18 int _case,k=1; 19 scanf("%d",&_case); 20 while(_case--){ 21 scanf("%d",&n); 22 for(int i=1;i<=n;i++){ 23 scanf("%d",&value[i]); 24 } 25 value[n+1]=0; 26 scanf("%d",&m); 27 memset(dp,0,sizeof(dp)); 28 memset(path,-1,sizeof(path)); 29 memset(map,false,sizeof(map)); 30 for(int i=1;i<=m;i++){ 31 int x,y; 32 scanf("%d%d",&x,&y); 33 map[x][y]=true; 34 } 35 for(int i=2;i<=n+1;i++){ 36 for(int j=1;j<i;j++)if(map[j][i]){ 37 if(dp[i]<dp[j]+value[i]){ 38 dp[i]=dp[j]+value[i]; 39 path[i]=j; 40 } 41 } 42 } 43 path[1]=-1; 44 printf("CASE %d#\n",k++); 45 printf("points : %d\n",dp[n+1]); 46 printf("circuit : "); 47 Print(path[n+1]); 48 printf("1\n"); 49 if(_case>0){ 50 printf("\n"); 51 } 52 } 53 return 0; 54 }