Nyoj 203 Three Kingdoms

Source: Internet
Author: User
Three KingdomsTime Limit: 3000 MS | memory limit: 65535 kb difficulty: 5
Description

Three Kingdoms is a classic management strategy game. Our friends are loyal players in this game. Now he simplifies the game. There is only one force on the map. Now he has only one city, and there are some empty cities around him, but these empty cities have a lot of different treasures of the same kind. Our little white students watched the treasures in these cities.

According to the rules of the game, as long as he assigns a military commander to take over the city, the treasures will be owned by him. However, when we take over the city, our military commanders will be left behind and cannot be withdrawn. He doesn't care about this because there are countless military commanders under us.

The military commanders dispatched from the city of Xiao Bai will consume the grain of one stone each time they walk through the principle, and the grain on his hand is limited. Now, Tom counts the roads on the map. These roads are two-way. He wants to ask you to calculate the maximum number of treasures he can get. We use the city id to represent the city pool. It is required that the city pool where Xiao Bai is located is no. 0, and other city pools start from 1.

 
Input
This question contains multiple groups of data:
First, it is an integer T (1 <=t <= 20), representing the number of data groups
Then, the following is the T group test data. Each group of data contains three rows:
Row 1: three numbers: S, n, m
(1 <= S <= 10000, 1 <= n <=, 1 <= m <=)
S represents the grain (Stone) in his hand, N represents the number of cities, and m represents the number of roads.
Row 2: Contains m Triple rows of AI, Bi, and Ci (1 <= A, B <= N, 1 <= C <= 100 ).
Represents ai. The road length between two bi cities is CI (km ).
Row 3: contains n elements. VI represents the number of treasures in the city I. (1 <= V <= 100)
Output
Each group of outputs occupies one row, and only one integer is output, which indicates the maximum wealth value that Xiao Bai can obtain.
Sample Input
210 1 10 1 325 2 30 1 2 0 2 4 1 2 12 3
Sample output
25
Source
Zhengzhou University competition questions
Uploaded
Zhang yuncong


Problem solving: the shortest path + 0-1 backpack, first finding the distance from 0 to each city, then carrying out a backpack in 1... n cities at the cost of distance.

 1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <vector> 6 #include <climits> 7 #include <algorithm> 8 #include <cmath> 9 #include <queue>10 #define LL long long11 #define INF 0x3f3f3f3f12 using namespace std;13 const int maxn = 110;14 struct arc{15     int to,w;16 };17 vector<arc>g[maxn];18 queue<int>q;19 int s,n,m;20 int val[maxn],d[maxn],pre[maxn];21 int dp[1000010];22 bool in[maxn];23 void spfa(int src){24     int i,j,u,v;25     for(i = 0; i <= n; i++){26         d[i] = INF;27         in[i] = false;28         pre[i] = -1;29     }30     d[src] = 0;31     while(!q.empty()) q.pop();32     q.push(src);33     in[src] = true;34     while(!q.empty()){35         u = q.front();36         q.pop();37         in[u] = false;38         for(i = 0; i < g[u].size(); i++){39             v = g[u][i].to;40             if(d[v] > d[u]+g[u][i].w){41                 d[v] = d[u]+g[u][i].w;42                 pre[v] = u;43                 if(!in[v]){44                     q.push(v);45                     in[v] = true;46                 }47             }48         }49     }50 }51 int main(){52     int t,i,j,u,v,w;53     scanf("%d",&t);54     while(t--){55         scanf("%d%d%d",&s,&n,&m);56         for(i = 0; i <= n; i++)57             g[i].clear();58         for(i = 0; i < m; i++){59             scanf("%d%d%d",&u,&v,&w);60             g[u].push_back((arc){v,w});61             g[v].push_back((arc){u,w});62         }63         for(i = 1; i <= n; i++)64             scanf("%d",val+i);65         spfa(0);66         memset(dp,0,sizeof(dp));67         for(i = 1; i <= n; i++){68             for(j = s; j >= d[i]; j--){69                 dp[j] = max(dp[j],dp[j-d[i]]+val[i]);70             }71         }72         printf("%d\n",dp[s]);73     }74     return 0;75 }
View code

 

 

Nyoj 203 Three Kingdoms

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.