25
Solution: Find the distance from 0 to each castle, and then solve it with 01 knapsack.
A set of data is appended to the code, and the answer is 9.
#include <stdio.h> #include <string.h> #include <vector> #include <map> #include <queue># include<stack> #include <cstdio> #include <string> #include <math.h> #include <algorithm > #define LL Long long#define PI atan (1.0) *4#define DD double#define MAX 200000#define mod 10003#define Dian 1.000000011 #define INF 0x3f3f3fusing namespace Std;int du[max];int food[max],bao[max];int head[max],ans;int dis[max],vis[max];int Price[max],cost[max];int sum[max];int n,m;struct node{int u,v,w; int next;} edge[max];void Add (int u,int v,int W) {edge[ans].u=u; Edge[ans].v=v; Edge[ans].w=w; Edge[ans].next=head[u]; head[u]=ans++;} int SPFA (int sx,int sy) {int i,j; queue<int>q; memset (vis,0,sizeof (VIS)); for (i=0;i<=n;i++) Dis[i]=inf; Vis[sx]=1; dis[sx]=0; Q.push (SX); while (!q.empty ()) {int U=q.front (); Q.pop (); vis[u]=0; for (I=head[u];i!=-1;i=edge[i].next) { int top=edge[i].v; if (DIS[TOP]>DIS[U]+EDGE[I].W) {DIS[TOP]=DIS[U]+EDGE[I].W; if (!vis[top]) {vis[top]=1; Q.push (top); }}}} return Dis[sy];} int main () {int j,i,t,k,a,b,s,c,d; scanf ("%d", &t); while (t--) {scanf ("%d%d%d", &s,&n,&m); memset (head,-1,sizeof (head)); ans=0; for (i=1;i<=m;i++) {scanf ("%d%d%d", &a,&b,&c); Add (A,B,C); Add (B,A,C); } for (i=1;i<=n;i++) {scanf ("%d", &d); Price[i]=d; } for (i=1;i<=n;i++) FOOD[I]=SPFA (0,i); memset (sum,0,sizeof (sum));//note Empty for (i=1;i<=n;i++) {for (j=s;j>=food[i];j--) {Sum[j]=max (sum[j],sum [J-food[i]]+price[i]); }} printf ("%d\n", Sum[s]); }return 0;} /*10 5 60 1 70 2 11 4 31 3 42 3 22 5 45 4 2 6 2*/