POJ 1797 Heavy Transportation (two points + and check set/kruskal)

Source: Internet
Author: User
Tags cmath

Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000K
Total Submissions: 24398 Accepted: 6472

Description

Background
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 numbere D 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.

Input

The 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.

Output

The output for every scenario begins with a line containing "scenario #i:", where I am the number of the scenario starting 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

Main topic:

there are N cities, M Road, from 1 to n the maximum load capacity of the road, the maximum load capacity is the minimum amount of the road through the load. Oh hehe ~ ~ ~ again to hold a chestnut: Suppose n=3,m=3, connecting 1 and 2 of the city's road load capacity is 3, connection 1 and 3 of the carrying capacity is 4, 2 and 3 of the load is 5, then from 1 to 3 there are 2 ways: (1) 1-2-3, carrying capacity is 3, 5, then the maximum load capacity is 5 ; (2) 1-3, carrying capacity is 4, the maximum load capacity is also 4, so the answer is 4.

Problem Solving Ideas:
maximize the minimum value, the obvious two points. In addition, the problem can also be done with Kruskal deformation, to make 1 and N connected to a maximum spanning tree. The specific method is to put the edge in descending order of weight, according to the weights from the largest to the smallest of the sequence to add the edge to the spanning tree, when 1 in n connectivity, the output of the answer.

Reference code:

/* binary + memory:1288 KB time:250 MS language:g++ result:accepted */#include <map> #include <stack> #incl ude<queue> #include <cmath> #include <vector> #include <cctype> #include <cstdio># include<cstring> #include <iostream> #include <algorithm>using namespace Std;const double eps=1e-10; const int Inf=0x3f3f3f3f;const int Maxn=1100;int n,m,par[maxn],s[maxn*maxn],e[maxn*maxn],cost[maxn*maxn];int Find ( int x) {return x==par[x]?x:par[x]=find (par[x]);}    void Unite (int x,int y) {x=find (x);    Y=find (y);    if (x==y) return; Par[x]=y;}    BOOL judge (int x) {for (int i=1;i<=n;i++) par[i]=i;    for (int i=1;i<=m;i++) if (cost[i]>=x) Unite (S[i],e[i]); return find (1) ==find (n);}    int main () {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//Online_judge int tcase,f=0;    scanf ("%d", &tcase);        while (tcase--) {scanf ("%d%d", &n,&m);       for (int i=1;i<=m;i++)     scanf ("%d%d%d", &s[i],&e[i],&cost[i]);        int l=1,r=1e6+50;            while (l<r) {int mid= (L+R)/2;            if (judge (mid)) l=mid+1;        else R=mid;    } printf ("Scenario #%d:\n%d\n\n", ++f,r-1); } return 0;}
/* Maximum spanning tree memory:14904 KB time:454 MS language:g++ result:accepted */#include <map> #include <stack> #incl ude<queue> #include <cmath> #include <vector> #include <cctype> #include <cstdio># include<cstring> #include <iostream> #include <algorithm>using namespace Std;const double eps=1e-10;    const int INF=0X3F3F3F3F;CONST int maxn=1100;struct edge{int s,e,v;    BOOL operator< (const edge& t) const {return v>t.v; }};edge e[maxn*maxn];int n,m,par[maxn],ans;int Find (int x) {return x==par[x]?x:par[x]=find (par[x]);}    BOOL Unite (int x,int y) {x=find (x);    Y=find (y);    if (x==y) return false;    Par[x]=y; return true;} BOOL Same (int x,int y) {return find (x) ==find (y);}    void Kruskal () {sort (e+1,e+1+m);            for (int i=1; i<=m; i++) {if (!same (E[I].S,E[I].E)) {unite (E[I].S,E[I].E);            Ans=min (ANS,E[I].V);        if (Same (1,n)) break; }}}int Main () {#ifndef Online_judge freopen ("In.txt", "R", stdin), #endif//Online_judge int tcase,f=0;    scanf ("%d", &tcase);        while (tcase--) {scanf ("%d%d", &n,&m);        for (int i=1; i<=n; i++) par[i]=i;        memset (E,0,sizeof (e));        for (int i=1; i<=m; i++) scanf ("%d%d%d", &AMP;E[I].S,&AMP;E[I].E,&AMP;E[I].V);        Ans=inf;        Kruskal ();    printf ("Scenario #%d:\n%d\n\n", ++f,ans); } return 0;}



Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

POJ 1797 Heavy Transportation (two points + and check set/kruskal)

Related Article

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.