Poj-1797heavy transportation, the shortest-circuit deformation, with a slight modification of dijkstra can be;

Source: Internet
Author: User
Tags cmath

Heavy Transportation
Time Limit: 3000MS Memory Limit: 30000K

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

Source

TUD Programming Contest 2004, Darmstadt, Germany

In fact, this question looked for a long time did not understand the meaning, and then find a blog is given n a site (so understand it), M road, two sites have and only one road, each road has a maximum load capacity, the entire path is composed of the largest load-bearing; For example, 1 to 2 of the load is 3, More than 3 can not be from this road, and 1 to 3 of the load is 4, when the diagram has been linked up, so the maximum is 4, if the election of 1->2->3, then 1, 2 the road between the weight of 5 lift;

In this way, it is equivalent to a maximum spanning tree of the weight of the smallest edge, but with the method of spanning tree is not time-out re, had to use the shortest path in an algorithm to solve, we see, since Dijkstra is the shortest single source, d[i] stored is from the starting point to I point of the shortest path, Then we use it to save the starting point to the point where I have the least weight, so the answer doesn't come out; think about it, the problem is very logical, the maximum weight of the load is actually to find the entire Unicom map of the smallest weights, like the principle of the short board-how much water can be loaded depends on the shortest piece of wood;

Kruskal Spanning Tree: RE, the array is large and will time out;

 #include <cstdio> #include <cmath> #include <cstring> #include < Iostream> #include <algorithm>using namespace std;const int n=1000+10;struct node{int u,v,w;} A[n];int n,m,f[n];int Find (int x) {return f[x]==-1?x:f[x]=find (f[x]);} int CMP (node A,node b) {return A.W>B.W;}    int ks (int n,int m) {for (int i=0;i<m;i++) scanf ("%d%d%d", &A[I].U,&A[I].V,&A[I].W);    Sort (a,a+m,cmp);    int minn=10000000;    memset (F,-1,sizeof (f));        for (int i=0;i<m;i++) {int x=find (A[I].U);        int Y=find (A[I].V);            if (x!=y) {minn=min (MINN,A[I].W);        F[x]=y;    } if (find (1) ==find (n)) break; } return Minn;}    int main () {int t;    scanf ("%d", &t);    int t1=t;        while (t--) {memset (a,0,sizeof (a));        scanf ("%d%d", &n,&m);        int X=ks (N,M);    printf ("Scenario #%d:\n%d\n", t1-t,x); } return 0;} 


Dijkstra Shortest-circuit distortion: AC
#include <cstdio> #include <cmath> #include <iostream> #include <cstring> #include < algorithm>using namespace Std;const int n=1005;int w[n][n],v[n],d[n],n,m,t,t1;void Dijkstra () {    int a,b,c,i,j;    Memset (W,0,sizeof (w));     for (I=1; i<=m; i++)    {        scanf ("%d%d%d", &a,&b,&c);        w[a][b]=w[b][a]=c;    }     memset (v,0,sizeof (v));    for (I=1; i<=n; i++)        d[i]=w[1][i];    for (i=1;i<=n;i++)    {        int x,m=-1;        for (j=1;j<=n;j++)            if (!v[j]&&d[j]>m)            m=d[x=j];        V[x]=1;        for (j=1;j<=n;j++)            if (!v[j] && d[j]<min (d[x],w[x][j])             d[j]=min (D[x],w[x][j]);    }    printf ("Scenario #%d:\n", t1-t);    printf ("%d\n\n", D[n]);} int main () {    scanf ("%d", &t);    t1=t;    while (t--)    {        scanf ("%d%d", &n,&m);        Dijkstra ();    }    return 0;}


Poj-1797heavy Transportation, the shortest-circuiting deformation, with a slight modification of dijkstra can be;

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.