A walk through the forest hdu1142 short circuit + simple motion Gauge

Source: Internet
Author: User

This question uses the shortest path and DP knowledge. It feels like a good question.

First, based on the diagram provided by the original question, 2 has been used as the start point to find the single-source shortest path, and then, based on what the question says, when d [I]> d [J], it can be from I to J, so another graph can be created according to this requirement, this figure is a typical "number Tower" model, so we can find the number of routes by DP.

For more information, see code implementation.

# Include <iostream> # include <cstdio> # include <vector> using namespace STD; const int maxn = 1005; const int INF = (1 <30 ); vector <int> edge [maxn]; // store the int map [maxn] [maxn] of the walking edge; // The original graph int d [maxn]; // d [I] indicates the distance from I to 2. Int DP [maxn]; // DP [I] indicates the number of paths from I to 2. bool visit [maxn]; void DFS (INT v) {If (DP [v]! =-1) return; DP [v] = 0; For (INT I = 0; I <edge [v]. size (); I ++) {int u = edge [v] [I]; DFS (U); DP [v] + = DP [u] ;}} int main () {int I, j, n, m; int A, B, C; while (scanf ("% d", & N), n) {scanf ("% d", & M); for (I = 1; I <= N; I ++) {visit [I] = false; d [I] = inf; For (j = 1; j <= N; j ++) map [I] [J] = inf;} d [2] = 0; while (M --) {scanf ("% d", & A, & B, & C); If (Map [a] [B]> C) {map [a] [B] = C; Map [B] [a] = C ;}} d [0] = inf; while (1) {J = 0; for (I = 1; I <= N; I ++) if (! Visit [I] & D [I] <D [J]) J = I; If (j = 0) break; visit [J] = true; for (I = 1; I <= N; I ++) if (! Visit [I] & D [J] + map [J] [I] <D [I]) d [I] = d [J] + map [J] [I] ;}for (I = 1; I <= N; I ++) edge [I]. clear (); for (I = 1; I <= N; I ++) for (j = 1; j <= N; j ++) if (Map [I] [J]! = Inf & D [I]> d [J]) edge [I]. push_back (j); for (I = 1; I <= N; I ++) DP [I] =-1; DP [2] = 1; DFS (1 ); printf ("% d \ n", DP [1]);} return 0 ;}

 

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.