Poj1797-Heavy Transportation

Source: Internet
Author: User

Want to see more problem-solving reports:
Http://blog.csdn.net/wangjian8006/article/details/7870410
Reprinted please indicate the source: http://blog.csdn.net/wangjian8006

There are n cities and M roads with a carrying capacity on each road. Now the maximum carrying capacity is required from 1 to n cities, the maximum carrying capacity is the maximum carrying capacity of all the routes from City 1 to city n.
Solution: in fact, the biggest side can be similar to the nearest short-circuit. You only need to modify the conditions for updating the nearest short-circuit.

 

/* 4128 K 375msdijkstra adjacent matrix */# include <iostream> using namespace STD; # define maxv 1010 # define min (a, B) (a <B? A: B) int map [maxv] [maxv], n, m; int Dijkstra () {int vis [maxv], d [maxv], I, j, V; for (I = 1; I <= N; I ++) {vis [I] = 0; d [I] = map [1] [I]; // at this time, D does not represent the shortest path from 1 to n, but the maximum carrying capacity} for (I = 1; I <= N; I ++) {int F =-1; for (j = 1; j <= N; j ++) if (! Vis [J] & D [J]> F) {f = d [J]; V = J;} vis [v] = 1; for (j = 1; j <= N; j ++) if (! Vis [J] & D [J] <min (d [v], map [v] [J]) {d [J] = min (d [v], map [v] [J]) ;}} return d [N] ;}int main () {int T, I, j, sum, A, B, C; scanf ("% d", & sum); For (t = 1; t <= sum; t ++) {scanf ("% d", & N, & M); for (I = 0; I <= N; I ++) for (j = 0; j <= N; j ++) map [I] [J] = 0; for (I = 1; I <= m; I ++) {scanf ("% d", &, & B, & C); map [a] [B] = map [B] [a] = C;} printf ("Scenario # % d: \ n ", t); printf ("% d \ n", Dijkstra ();} return 0 ;}

 

========================================================== ========================================================== ==========

 

/* Spfa adjacent matrix 4156 K 469 Ms */# include <iostream> # include <queue> using namespace STD; # define maxv 1010 # define min (A, B) (A <B? A: B) int map [maxv] [maxv], n, m; int spfa () {queue <int> q; int I, j, V; int vis [maxv], d [maxv]; for (I = 1; I <= N; I ++) {vis [I] = 0; d [I] = 0;} Q. push (1); vis [1] = 1; while (! Q. empty () {v = Q. front (); q. pop (); vis [v] = 0; for (I = 1; I <= N; I ++) {If (V = 1 & map [v] [I]) {d [I] = map [v] [I]; q. push (I); vis [I] = 1; continue;} If (d [I] <min (d [v], map [v] [I]) {d [I] = min (d [v], map [v] [I]); If (! Vis [I]) {vis [I] = 1; q. push (I) ;}}} return d [N];} int main () {int T, I, j, sum, A, B, C; scanf ("% d", & sum); For (t = 1; t <= sum; t ++) {scanf ("% d", & N, & M); for (I = 0; I <= N; I ++) for (j = 0; j <= N; j ++) map [I] [J] = 0; for (I = 1; I <= m; I ++) {scanf ("% d", &, & B, & C); map [a] [B] = map [B] [a] = C;} printf ("Scenario # % d: \ n ", t); printf ("% d \ n", spfa ();} return 0 ;}

 

========================================================== ==========================================================

 

/* Bellman-Ford adjacent matrix time limit exceeded */# include <iostream> using namespace STD; # define maxv 1010 # define min (a, B) (a <B? A: B) int map [maxv] [maxv], n, m; int bellman_ford () {int I, j, v, k; int vis [maxv], d [maxv]; for (I = 1; I <= N; I ++) d [I] = map [1] [I]; for (I = 1; I <= N; I ++) {for (j = 1; j <= N; j ++) {for (k = 1; k <= N; k ++) {If (d [k] <min (d [J], map [J] [k]) & map [J] [k]) d [k] = min (d [J], map [J] [k]); If (d [J] <min (d [K], map [k] [J]) & map [k] [J]) d [J] = min (d [K], map [k] [J]) ;}} return d [N];} int main () {int T, I, j, sum, A, B, C; scanf ("% d", & sum); For (t = 1; t <= sum; t ++) {scanf ("% d", & N, & M); for (I = 0; I <= N; I ++) for (j = 0; j <= N; j ++) map [I] [J] = 0; for (I = 1; I <= m; I ++) {scanf ("% d", &, & B, & C); map [a] [B] = map [B] [a] = C;} printf ("Scenario # % d: \ n ", t); printf ("% d \ n", bellman_ford ();} return 0 ;}

 

========================================================== ========================================================== ==============================

 

/* 760 K 1532msbellman_ford adjacent table */# include <iostream> using namespace STD; # define maxv 1010 # define MaxE 1000010 # define min (a, B) (a <B? A: B) struct {int S, E, W;} edge [MaxE]; int n, m; int bellman_ford () {int I, j, d [maxv]; for (I = 1; I <= N; I ++) d [I] = 0; d [1] = 0 xffffff; for (I = 1; I <N; I ++) {for (j = 1; j <= m; j ++) {If (d [edge [J]. e] <min (d [edge [J]. s], edge [J]. w) d [edge [J]. e] = min (d [edge [J]. s], edge [J]. w); If (d [edge [J]. s] <min (d [edge [J]. e], edge [J]. w) d [edge [J]. s] = min (d [edge [J]. e], edge [J]. w) ;}} return d [N];} int main () {int T, I, sum; scanf ("% d", & sum ); for (t = 1; t <= sum; t ++) {scanf ("% d", & N, & M); for (I = 1; I <= m; I ++) {scanf ("% d", & edge [I]. s, & edge [I]. e, & edge [I]. w);} printf ("Scenario # % d: \ n", T); printf ("% d \ n", bellman_ford ();} return 0 ;}

 

========================================================== ========================================================== ======================

 

/* 760 K 250msbell_ford adjacent table optimization */# include <iostream> using namespace STD; # define maxv 1010 # define MaxE 1000010 # define min (a, B) (a <B? A: B) struct {int S, E, W;} edge [MaxE]; int n, m; int bellman_ford () {int I, j, d [maxv]; for (I = 1; I <= N; I ++) d [I] = 0; d [1] = 0 xffffff; int flag = 1; while (FLAG) {flag = 0; For (j = 1; j <= m; j ++) {If (d [edge [J]. e] <min (d [edge [J]. s], edge [J]. w) {d [edge [J]. e] = min (d [edge [J]. s], edge [J]. w); flag = 1;} If (d [edge [J]. s] <min (d [edge [J]. e], edge [J]. w) {d [edge [J]. s] = min (d [edge [J]. e], edge [J]. w); flag = 1 ;}} return d [N];} int main () {int T, I, sum; scanf ("% d", & sum ); for (t = 1; t <= sum; t ++) {scanf ("% d", & N, & M); for (I = 1; I <= m; I ++) {scanf ("% d", & edge [I]. s, & edge [I]. e, & edge [I]. w);} printf ("Scenario # % d: \ n", T); printf ("% d \ n", bellman_ford ();} return 0 ;}

 

========================================================== ========================================================== ========

 

/* Bellman-Ford adjacent matrix optimization 4124 K 1485 Ms */# include <iostream> using namespace STD; # define maxv 1010 # define min (a, B) (a <B? A: B) int map [maxv] [maxv], n, m; int bellman_ford () {int I, j, v, k; int vis [maxv], d [maxv]; for (I = 1; I <= N; I ++) d [I] = map [1] [I]; int flag = 1; while (FLAG) {flag = 0; For (j = 1; j <= N; j ++) {for (k = 1; k <= N; k ++) {If (d [k] <min (d [J], map [J] [k]) & map [J] [k]) {d [k] = min (d [J], map [J] [k]); flag = 1 ;} if (d [J] <min (d [K], map [k] [J]) & map [k] [J]) {d [J] = min (d [K], map [k] [J]); flag = 1 ;}}} return d [N];} int main () {int T, I, j, sum, A, B, C; scanf ("% d", & sum); For (t = 1; T <= sum; t ++) {scanf ("% d", & N, & M); for (I = 0; I <= N; I ++) for (j = 0; j <= N; j ++) map [I] [J] = 0; for (I = 1; I <= m; I ++) {scanf ("% d", & A, & B, & C ); map [a] [B] = map [B] [a] = C;} printf ("Scenario # % d: \ n", t ); printf ("% d \ n", bellman_ford ();} 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.