hdu1224

#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#define N 200
using namespace std;
bool map[N][N];
bool inq[N];
int interest[N];
int dp[N];
int father[N];
void spfa(int n)
{
    int i,j,u,v;
    memset(inq,0,sizeof(inq));
    memset(dp,0,sizeof(dp));
    queue<int >q;
    q.push(1);
    dp[1]=interest[1];
    inq[1]=1;
    while(!q.empty())
    {
        u=q.front();
        q.pop();
        inq[u]=0;
        for(i=2;i<=n;i++)
        {
            if(map[u][i]&&dp[i]<dp[u]+interest[i])
            {
                dp[i]=dp[u]+interest[i];
                father[i]=u;
                if(!inq[i])
                {
                    q.push(i);
                    inq[i]=1;
                }
            }
        }

    }
}
int main()
{
    int ca,i,j,n,m,casecount=1;
    int x,y;
    scanf("%d",&ca);
    while(ca--)
    {
        memset(map,0,sizeof(map));
        memset(father,0,sizeof(father));
        memset(interest,0,sizeof(interest));
        scanf("%d",&n);
        for(i=1;i<=n;i++)
          scanf("%d",&interest[i]);
        scanf("%d",&m);
        for(i=0;i<m;i++)
        {
          scanf("%d%d",&x,&y);
          map[x][y]=1;
        }
        spfa(n+1);
        printf("CASE %d#\n",casecount++);
        printf("points : %d\n",dp[n+1]);
        printf("circuit : ");
        stack<int>ss;
        i=father[n+1];
        while(i!=0)
        {
            ss.push(i);
            i=father[i];
        }
        while(!ss.empty())
        {
            printf("%d->",ss.top());
            ss.pop();
        }
        printf("1\n");
        if(ca)
          printf("\n");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值