。。。 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)