题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1224
注意有些路是走不到的
写了好长时间,真烦
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
const int maxn=100+5;
const int INF=0x3f3f3f3f;
int d[maxn],Num[maxn],N,p[maxn];
bool G[maxn][maxn];int main(int argc, char const *argv[])
{int T,kase=0; cin>>T;while(T--){cin>>N;memset(G,false,sizeof(G));memset(Num,0,sizeof(Num));for (int i = 1; i <= N; ++i)scanf("%d",&Num[i]);int e,u,v; cin>>e;for (int i = 0; i < e; ++i){scanf("%d%d",&u,&v);G[u][v]=true;}memset(p,0,sizeof(p));memset(d,0,sizeof(d));for(u=2;u<=N+1;u++){int tmp=0;d[u]=Num[u];for(v=u-1;v>0;v--){if(G[v][u] && d[v]>=tmp){tmp=d[v];p[u]=v;}}d[u]+=tmp;}printf("CASE %d#\n",++kase);printf("points : %d\n",d[N+1]);printf("circuit : 1");int pos=N+1;vector<int> path;path.push_back(1);while(p[pos]!=1){pos = p[pos];path.push_back(pos);}for(int i=(int)path.size()-1;i>=0;i--)cout<<"->"<<path[i];cout<<endl;if(T) cout<<endl;}return 0;
}