Nyoj Kingdoms (Dijkstra + 01 backpack)

Source: Internet
Author: User

。。。 It's always a hell of a train.

Note that there may be multiple paths with different weights between two points, but it's good to take the minimum value as you enter it.

1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <cstring>5#include <string>6#include <queue>7#include <algorithm>8#include <map>9#include <iomanip>Ten#include <climits> One#include <string.h> A#include <cmath> -#include <stdlib.h> -#include <vector> the#include <Set> - #defineINF 1e7 - #defineMAXN 100010 - #defineMAXN 50 + #defineMAXM 1000 - #defineMod 1000007 + using namespacestd; AtypedefLong LongLL; at  -  - intT; - ints, N, M; - intA, B, C; - intv[ the]; in intg[ the][ the]; - intdist[1000010]; to intvis[ the]; + intdp[1000010]; -  the voidDijkstra () * { $      for(inti =0; I <= N; ++i)Panax NotoginsengDist[i] = g[0][i],vis[i] =0; -     intpos, min; thevis[0] =1; +dist[0] =0; A      for(inti =1; I <= N; ++i) { theMin =INF; +          for(intj =1; J <= N; ++j) { -             if(!vis[j] && min >Dist[j]) { $pos =J; $Min =Dist[j]; -             } -         } theVis[pos] =1; -          for(intj =1; J <= N; ++j) {Wuyi             if(!vis[j] && dist[j] > Dist[pos] +G[pos][j]) theDIST[J] = Dist[pos] +G[pos][j]; -         } Wu     } - } About  $ voidbag01 () - { -      for(inti =1; I <= N; ++i) -          for(intj = S; J >= Dist[i]; --j) ADP[J] = max (Dp[j-dist[i]) +v[i],dp[j]); + } the  - voidRun () $ { thescanf"%d%d%d", &s, &n, &m); thememset (V,0,sizeof(v)); theMemset (DP,0,sizeof(DP)); the      for(inti =0; I <= N; ++i) -          for(intj =0; J <= N; ++j) inG[I][J] =INF; the      for(inti =0; I < m; ++i) { thescanf"%d%d%d", &a, &b, &c); About         if(C <G[a][b]) theG[A][B] = G[b][a] =C; the     } the      for(inti =1; I <= N; ++i) +scanf"%d",&v[i]); - Dijkstra (); the bag01 ();Bayiprintf"%d\n", Dp[s]); the } the intMain () - { -scanf"%d", &T); the      while(t--) the run (); the     return 0; the}

Nyoj Kingdoms (Dijkstra + 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.