HDU 1224 Free DIY Tour DAG最长路

DAG最长路可以用动态规划或者spfa的方法求。逛了一圈论坛,看见有人说dijkstra也可以,但我自己写了个dijkstra却WA了,还是太菜了呀。

#include<iostream>
#include<cstdio>
#include<string.h>
#include<algorithm>
#include<stack>
using namespace std;

int t,n,cas,m,point[105],mapp[105][105],dist[105],fa[105];

int main()
{
    cas=0;
    scanf("%d",&t);
    while (t--)
    {
        cas++;
        stack<int> stacks;
        if (cas>1) cout<<endl;
        cout<<"CASE "<<cas<<"#"<<endl;
        scanf("%d",&n);
        n++;
        for (int i=1; i<n; i++)
        {
            scanf("%d",&point[i]);
        }
        point[n]=0;
        for(int i=1; i<=n; i++)
        {
            for (int j=1; j<=n; j++)
            {
                mapp[i][j]=0;
            }
        }
        for (int i=1; i<=n; i++)
        {
            dist[i]=0; fa[i]=-1;
        }
        scanf("%d",&m);
        for (int i=1; i<=m; i++)
        {
            int a,b; scanf("%d%d",&a,&b);
            mapp[a][b]=1;
        }
        for (int i=1; i<=n; i++)
        {
            for (int j=1; j<=n; j++)
            {
                //cout<<"i="<<i<<" j="<<j<<" disti="<<dist[i]<<" pi="<<point[i]<<" dj"<<dist[j]<<endl;
                if (mapp[i][j] && dist[i]+point[i]>dist[j])
                {
                    dist[j]=dist[i]+point[i];
                    fa[j]=i;
                    //cout<<"j="<<j<<" i="<<i<<endl;
                }
            }
        }
        cout<<"points : "<<dist[n]<<endl;
        cout<<"circuit : 1->";
        int tmp=n;
        stacks.push(1);
        while (fa[tmp]!=-1)
        {
            tmp=fa[tmp];
            stacks.push(tmp);
        }
        int first=true;
        while (!stacks.empty())
        {
            if (first)
            {
                cout<<stacks.top();
                first=false;
            }
            else
            {
                cout<<"->"<<stacks.top();
            }
            stacks.pop();
        }
        cout<<endl;
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值