zoukankan      html  css  js  c++  java
  • hdu1224 dp(dp + 栈/父亲数组记录路径)

    题意:给定 n 个城市的有趣度,并给出可以从那些城市飞到那些城市。其中第一个城市即起始城市同样也作为终点城市,有趣度为 0,旅行途中只允许按有趣度从低到高旅行,问旅行的有趣度最大是多少,并输出旅行路径。

    我一开始读题的时候各种深井冰理解错想复杂,导致我一开始甚至认为第一个有趣度 0 代表的是第二个城市,因为第一个城市一定是 0 不需要给出,加上没看到题意里的第一个城市也是第 n + 1 个城市,觉得给出的能旅行的城市里出现了 n + 1 是应该的。还有关于城市联通,我以为给出的联通关系不一定就是按大小排列过后的,然后我还自行按有趣度高低排序之后再放入邻接矩阵。总之就是各种坑爹脑残各种蠢吧……

    后来敲完之后各种 WA ,各种比较题解,才发现了各种情况……修修改改 WA 四发之后终于 AC ,输出整个路径我看见大部分题解上用的都是 father 数组,昂,我觉得我还是多珍惜下用栈的机会咯

     1 #include<stdio.h>
     2 #include<string.h>
     3 #include<algorithm>
     4 #include<stack>
     5 using namespace std;
     6 
     7 struct city{
     8     int v,l;
     9 }c[101];
    10 /*
    11 bool cmp(city c1,city c2){
    12     return c1.v<c2.v;
    13 }
    14 */
    15 bool g[101][101];
    16 int dp[101];
    17 
    18 int main(){
    19     int T;
    20     while(scanf("%d",&T)!=EOF){
    21         for(int q=1;q<=T;q++){
    22             memset(g,0,sizeof(g));
    23             memset(dp,0,sizeof(dp));
    24             int n,m,i,j;
    25             scanf("%d",&n);
    26             for(i=1;i<=n;i++){
    27                 scanf("%d",&c[i].v);
    28                 c[i].l=-1;
    29             }
    30             c[1].l=1;
    31             scanf("%d",&m);
    32             for(i=1;i<=m;i++){
    33                 int a,b;
    34                 scanf("%d%d",&a,&b);
    35                 if(b==n+1)g[a][1]=1;
    36                 else g[a][b]=1;
    37             }
    38             int ans=0,t=1;
    39         
    40             for(i=2;i<=n;i++){
    41                 for(j=1;j<i;j++){
    42                     if(g[j][i]&&((dp[j]+c[i].v)>dp[i])){
    43                         dp[i]=dp[j]+c[i].v;
    44                         c[i].l=j;
    45                         if(g[i][1]&&dp[i]>ans){
    46                             ans=dp[i];
    47                             t=i;
    48                         }
    49                     }
    50                 }
    51             }
    52             printf("CASE %d#
    points : %d
    circuit : ",q,ans);
    53             stack<int>S;
    54             int tmp=t;
    55             while(!S.empty()){
    56                 S.pop();
    57             }
    58             S.push(1);
    59             while(c[tmp].l!=tmp){
    60                 S.push(tmp);
    61                 tmp=c[tmp].l;
    62             }
    63             printf("1");
    64             while(!S.empty()){
    65                 tmp=S.top();
    66                 S.pop();
    67                 printf("->%d",tmp);
    68             }
    69             printf("
    ");
    70             if(q!=T)printf("
    ");
    71         }
    72     }
    73     return 0;
    74 }
    View Code
  • 相关阅读:
    整理前端面试题1
    前端面试题2
    6.显示锁Lock 和 线程通信Condition
    5.创建执行线程的方式之三 :实现Callable 接口
    4.闭锁 CountDownLatch
    3.ConcurrentHashMap 锁分段机制 Copy-On-Write
    2.原子变量 CAS算法
    1.volatile关键字 内存可见性
    13.MyBatis注解式开发
    12.查询缓存
  • 原文地址:https://www.cnblogs.com/cenariusxz/p/4317323.html
Copyright © 2011-2022 走看看