To find the shortest path to other points, the conditions of the topic become u->v not backtrack when and only when D[u]>d[v]. The DAG graph is then built according to this condition, running DP statistics scheme number, dp[u] = SUM (Dp[v]).
#include <bits/stdc++.h>using namespacestd;Const intMAXN =1001, MAXM =2002;structedge{intv,w,nxt;};#definePB push_backVector<Edge>Edges;vector<int>G[MAXN];intHEAD[MAXN];intD[MAXN];voidAddedge (intUintVintW) {edges. PB ({V,w,head[u]}); Head[u]= Edges.size ()-1;}voidinit () {memset (head,-1,sizeof(head)); Edges.clear ();} typedef pair<int,int>Node;#defineFi first#defineSe SecondvoidDijkstraints =1) {memset (d,0x3f,sizeof(d)); Priority_queue<Node,vector<Node>,greater<Node> >Q; Q.push (Node (d[s)=0, s)); while(Q.size ()) {Node x=q.top (); Q.pop (); intU =x.se; if(x.fi! = D[u])Continue; for(inti = Head[u]; ~i; i =edges[i].nxt) {Edge&e =Edges[i]; if(D[E.V] > d[u]+E.W) {D[E.V]= d[u]+E.W; Q.push (Node (D[E.V],E.V)); } } }}intDP[MAXN];intDfsintu) { int&ans =Dp[u]; if(~ans)returnans; if(U = =1)returnAns =1; Ans=0; for(inti =0; I < (int) G[u].size (); i++) {ans+=DFS (G[u][i]); } returnans;}voidRebuildintN) { for(inti =0; I < n; i++) g[i].clear (); for(intU =0; U < n; u++){ for(inti = Head[u]; ~i; i =edges[i].nxt) { intv =edges[i].v; if(D[v] <D[u]) G[u]. PB (v); } }}intMain () {//freopen ("In.txt", "R", stdin); intn,m; while(SCANF ("%d%d",&n,&m), N) {init (); while(m--){ intU,v,w; scanf"%d%d%d", &u,&v,&w); u--;v--; Addedge (U,V,W); Addedge (V,U,W); } Dijkstra (); Rebuild (n); Memset (DP,-1,sizeof(DP)); printf ("%d\n", DFS (0)); } return 0;}
UVA10917 A Walk Trough the Forest