UVA10917 A Walk Trough the Forest

Source: Internet
Author: User

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

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.