Poj big Christmas tree (Basic Shortest Path)

Source: Internet
Author: User

Big Christmas tree


Question Analysis:

You are advised to construct a Christmas tree so that (sum of weights of all descendant nodes) × (unit price of the edge) is as small as possible. After conversion, the distance between the root node and each node is the shortest, that is, the shortest path. The spanning tree may time out. I have not tried it. Then, the optimization solution should be used to solve the shortest circuit. Otherwise, it will time out. The final answer is: Sum = W [1] * Dist [1] + W [2] * Dist [2] + ..... W [N] * Dist [N]. you can simply push the sample by yourself.

It is a simple and short circuit. The result is that it is a waste of time in the morning because it is not cleared .,,, T_t

Remember Later !!! Blood lessons !!!!!


# Include <iostream> # include <algorithm> # include <vector> # include <queue> # include <cstdio> # include <cstring> using namespace STD; typedef long ll; const int maxn = 50000 + 100; struct edge {int from, to; ll C; edge () {}; edge (INT _ F, int _ T, ll _ C ): from (_ F), to (_ T), C (_ C) {};}; vector <edge> edges; vector <int> G [maxn]; ll weight [maxn], DIST [maxn]; bool VST [maxn]; int numv, nume; // clear void Init () {edges. clear (); fo R (INT I = 0; I <= numv; ++ I) {G [I]. clear () ;}} void addedge (int x, int y, ll c) {edges. push_back (edge (X, Y, C); int SZ = edges. size (); G [X]. push_back (SZ-1);} // relaxation operation bool relax (int u, int V, ll c) {If (Dist [u] =-1) | (Dist [u]> Dist [v] + C) {Dist [u] = DIST [v] + C; return true;} return false ;} // solve the shortest void spfa (ll src) {int I, U, K; queue <int> q; for (I = 0; I <= numv; ++ I) {VST [I] = 0; Dist [I] =-1;} Q. push (SRC); Dist [SRC] = 0; while (! Q. empty () {u = Q. front (); q. pop (); VST [u] = 0; for (I = 0; I <(INT) g [u]. size (); ++ I) {k = G [u] [I]; edge & E = edges [k]; If (relax (E. to, U, E. c )&&! VST [E. to]) {VST [E. to] = 1; q. push (E. to) ;}}} int main () {int t; scanf ("% d", & T); While (t --) {int X, Y; ll C; scanf ("% d", & numv, & nume); Init (); For (INT I = 1; I <= numv; ++ I) {CIN> weight [I] ;}for (INT I = 0; I <nume; ++ I) {scanf ("% d % i64d", & X, & Y, & C); addedge (X, Y, c); addedge (Y, X, c);} spfa (1); LL sum = 0; bool flag = false; For (INT I = 1; I <= numv; ++ I) {If (Dist [I] =-1) {flag = true; break;} sum + = DIST [I] * weight [I];} If (FLAG) puts ("No answer"); else printf ("% i64d \ n ", sum);} return 0 ;}


Zookeeper

Poj big Christmas tree (Basic Shortest Path)

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.