NYOJ 2033 National Journal (shortest circuit plus 01 backpack)

Source: Internet
Author: User

Kingdoms time limit:MS | Memory limit:65535 KB Difficulty:5
Describe

"Kingdoms" is a very classic business strategy game. Our little white classmate is the loyal player of this game. Now he has simplified the game, only his side of the map, and now he has only one city, and there are some empty cities around him, but there are many different quantities of the same treasure in these empty towns. Our little white students look at the treasures in these cities.

According to the rules of the game, he would appoint a warlord to take the city, and the treasure would be his own. But a measure of the conquest of this city, our generals will stay behind, can not be withdrawn. Because our little white men have countless generals, so he doesn't care about this.

From the small white cities to send the generals, every axiom of the distance will consume a stone of food, and his hands on the food is limited. Now the small white statistics on the map of the road between the cities, these roads are two-way, he would like to ask you to help calculate the most he can get the amount of treasure. We use the city's number to represent the city, the small white City is located in the city of No. 0, the other cities from the beginning of the 1th count.

Input
The subject contains multiple sets of data:
First, it is an integer T (1<=t<=20), which represents the number of groups of data
Then, here is the T-Group test data. For each set of data contains three rows:
First line: three digital s,n,m
(1<=s<=1000000,1<=n<=100,1<=m<=10000)
s represents the grain (stone) in his hand, N represents the number of cities, and M represents the number of road strips.
Second line: contains M ternary row Ai,bi,ci (1<=a,b<=n,1<=c<=100).
CI (km) represents the length of the road between the Ai,bi two cities.
The third line contains n elements, and VI represents the number of treasures in the first castle. (1<=v<=100)
Output
each set of output is a row, the output is only one integer, indicating the maximum wealth value of small white can be obtained.
Sample input
210 1 10 1 325 2 30 1 2 0 2 4 1 2 12 3
Sample output
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]&GT;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*/

  

NYOJ 2033 National Journal (shortest circuit plus 01 backpack)

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.