#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");
}
}
hdu1224
最新推荐文章于 2020-03-05 12:30:00 发布