HDU 1385 minimum transport cost

Source: Internet
Author: User

The shortest path.


The question is to give you a matrix, which is the cost of each point to each point. Then we will give you n numbers, which is the toll required for each point.

Then, ask a and B about the minimum fee and the path.


If it is not the path, the minimum Lexicographic Order must be output, which is very simple. Spfa write

It will be very troublesome. Then I will use Floyd. Update the path together.


# Include <cstdio> # include <cstring> # include <string> # include <queue> # include <algorithm> # include <map> # include <stack> # include <iostream> # include <list> # include <set> # include <cmath> # define INF 0x7fffffff # define EPS 1e-6 # define ll long longusing namespace STD; int n, m; int G [101] [101]; int cost [101]; int path [101] [101]; void Floyd () {for (int K = 1; k <= N; k ++) {for (INT I = 1; I <= N; I ++) {for (Int J = 1; j <= N; j ++) {If (G [I] [k] = inf | G [k] [J] = inf) continue; int Len = G [I] [k] + G [k] [J] + cost [k]; If (G [I] [J]> Len) {G [I] [J] = Len; path [I] [J] = path [I] [k];} else if (G [I] [J] = Len) {path [I] [J] = min (path [I] [J], path [I] [k]) ;}}} int main () {While (scanf ("% d", & N), n) {int TMP; for (INT I = 1; I <= N; I ++) for (Int J = 1; j <= N; j ++) {scanf ("% d ", & TMP); If (TMP =-1) g [I] [J] = inf; else G [I] [J] = TMP; path [I] [J] = J ;}for (INT I = 1; I <= N; I ++) scanf ("% D", & cost [I]); Floyd (); int I, j; while (scanf ("% d", & I, & J ), i! =-1 | j! =-1) {printf ("from % d to % d: \ n", I, j); printf ("Path: % d", I ); int K = I; while (K! = J) {printf ("--> % d", path [k] [J]); k = path [k] [J];} printf ("\ n"); printf ("Total cost: % d \ n", G [I] [J]) ;}}


HDU 1385 minimum transport cost

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.