POJ 1797 Heavy Transportation (maximum spanning tree/shortest path distortion)

Source: Internet
Author: User

Transmission Door Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 31882 Accepted: 8445
DescriptionBackground
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever mans tells him whether there really is a-a-a-from-the-place his customer have build his giant stee L Crane to the place where it's needed on which all streets can carry the weight.
Fortunately he already have a plan of the city with all streets and bridges and all the allowed weights. Unfortunately he have no idea how to find the the the maximum weight capacity in order to tell him customer how heavy the crane May become. But you surely know.

problem
You is given the plan of the city, described by the streets (with weight limits) between the crossings, which is num Bered from 1 to N. Your task is to find the maximum weight, can be transported from crossing 1 (Hugo's place) to crossing N (the customer ' s place). You are assume that there are at least one path. All streets can is travelled in both directions. InputThe first line contains the number of scenarios (city plans). For each city the number N of the street crossings (1 <= n <=) and number M of streets is given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed Weight, which is positive and not larger than 1000000. There 'll is at the most one street between each pair of crossings. OutputThe output for every scenario begins with a line containing "scenario #i:", where I am the number of the scenario star Ting at 1. Then print a, containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line. Sample Input
13 31 2 31 3 42 3 5
Sample Output
Scenario #1:4
Thinking of solving problemsTest instructions: From finding a road from 1 to N, the minimum value of each distance in this road is maximized. idea: Use the idea of Kruskal, the weight of each section of the road in accordance with the order from the big to small, and then add the edge until 1 up to N, at this time the path of the smallest straight is the answer. using the idea of Dijkstra or SPFA, dis[x] represents the maximum value of the minimum value of the road segment of the road that reaches X from 1.

Kruskal ()

 #include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int maxn = 1005;struct edge{int u,v,w;} Edge[maxn*maxn/2];int Fa[maxn];bool cmp (Edge A,edge b) {return A.W > b.w;} int find (int x) {return fa[x] = = X?fa[x]:fa[x] = find (Fa[x]);} void Union (int x,int y) {int fx = find (x), FY = Find (y), if (FX! = FY) fa[fx] = fy;} int main () {int tcase;scanf ("%d", &tcase), for (int t = 1;t <= tcase;t++) {int n,m,res = 0x3f3f3f3f;scanf ("%d%d",& N,&M); for (int i = 0;i <= n;i++) fa[i] = i;for (int i = 0;i < m;i++) {scanf ("%d%d%d", &EDGE[I].U,&EDGE[I].V , &EDGE[I].W);} Sort (Edge,edge + m,cmp); for (int i = 0; i< m;i++) {Union (EDGE[I].U,EDGE[I].V); if (find (1) = = Find (n)) {res = EDGE[I].W;BR Eak;}} printf ("Scenario #%d:\n", T);p rintf ("%d\n", res), if (t! = tcase) printf ("\ n");} return 0;} 

dijkstra ()

#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; const int MAXN = 1005;const int INF = 0x3f3f3f3f;struct edge{int U,v,w,nxt;bool operator < (const Edge &a) Const{ret Urn W < a.w;}} Edge[maxn*maxn];int tot = 0,head[maxn],dis[maxn];bool vis[maxn];void addedge (int u,int v,int W) {Edge[tot] = (edge) {u,v,w , head[u]};head[u] = tot++;} void Dijkstra (int st,int n) {Edge p;priority_queue<edge>que;memset (dis,0,sizeof (dis)); memset (vis,false,sizeof (VIS)); P.V = ST;P.W = inf;dis[st] = Inf;que.push (p); while (!que.empty ()) {p = que.top (); Que.pop (); int u = p.v;if (Vis[u]) continue; Vis[u] = true;for (int i = Head[u];~i;i = edge[i].nxt) {int v = edge[i].v,w = Edge[i].w;if (Dis[v] < min (dis[u],w)) {dis[ V] = min (dis[u],w);p. v = V,P.W = Dis[v];que.push (p); }}}}int Main () {int tcase;scanf ("%d", &tcase), for (int t = 1;t <= tcase;t++) {int N,m,u,v,w;memset (head,-1,sizeof ( Head); scanf ("%d%d", &n,&m); for (int i = 0;i < m;i++) {scanF ("%d%d%d", &u,&v,&w); Addedge (u,v,w); Addedge (v,u,w);} Dijkstra (1,n);p rintf ("Scenario #%d:\n", T);p rintf ("%d\n", dis[n]); if (t! = tcase) printf ("\ n");}  return 0;}

SPFA ()

#include <iostream> #include <cstdio> #include <cstring> #include <queue>using namespace std; const int MAXN = 1005;const int INF = 0x3f3f3f3f;struct edge{int u,v,w,nxt;} Edge[maxn*maxn];int tot = 0,head[maxn],dis[maxn];bool vis[maxn];void addedge (int u,int v,int W) {Edge[tot] = (edge) {u,v,w , head[u]};head[u] = tot++;} void Spfa (int st,int ed) {memset (vis,false,sizeof (Vis)); memset (dis,0,sizeof (dis)); queue<int>que;dis[st] = INF; Que.push (ST); vis[st] = True;while (!que.empty ()) {int u = que.front (); Que.pop (); Vis[u] = false;for (int i = Head[u];~i;i = EDGE[I].NXT) {int v = edge[i].v,w = edge[i].w;if (min (dis[u],w) > Dis[v]) {Dis[v] = min (dis[u],w), if (!vis[v]) {Que.push ( v); Vis[v] = true;}}}} int main () {int tcase;scanf ("%d", &tcase), for (int t = 1;t <= tcase;t++) {int N,m,u,v,w;memset (head,-1,sizeof (head) ); scanf ("%d%d", &n,&m); for (int i = 0;i < m;i++) {scanf ("%d%d%d", &u,&v,&w); Addedge (U,V,W); Addedge (v,u,w);} SPFA (1,n);p rintf ("Scenario #%d:\n", T);p Rintf ("%d\n", dis[n]); if (t! = tcase) printf ("\ n");}} 

  

POJ 1797 Heavy Transportation (maximum spanning tree/shortest path distortion)

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.