--Minimum spanning tree processing (Kruskal algorithm that always captures the minimum edge)

Source: Internet
Author: User

Consider yourself lucky! Consider yourself lucky to be still breathing and have fun participating in
This contest. But we apprehend the many of your descendants may not have the this luxury. For, as you
Know, we is the dwellers of one of the most polluted cities on earth. Pollution is everywhere, both in
The environment and in society and our lack of consciousness are simply aggravating the situation.
However, for the time being, we'll consider only a type of pollution-the sound pollution. The
Loudness or intensity level of sound are usually measured in decibels and sound have intensity level 130
Decibels or higher is considered painful. The intensity level of normal conversation is 6065 decibels and
That's heavy tra?c is 7080 decibels.
Consider the following city map where the edges refer to streets and the nodes refer to crossings.
The integer on each edge are the average intensity level of sound (in decibels) in the corresponding street.
To get from crossing A to crossing G your may follow the following path:a-c-f-g. In the case
You must is capable of tolerating sound intensity as high as decibels. For the Paths a-b-e-g,
A-b-d-g and a-c-f-d-g you must tolerate respectively, and the decibels of sound intensity.
There is other paths, too. However, it's clear that a-c-f-d-g are the most comfortable path since
It does not demand your to tolerate more than decibels.
In this problem, given a city map you is required to determine the minimum sound intensity level
You must is able to tolerate on order to get from a given crossing to another.
Input
The input may contain multiple test cases.
The RST line of all test case contains three integers C (+), S (+) and Q (10000) where
C indicates the number of crossings (crossings is numbered using distinct integers ranging from 1 to
C), S represents the number of streets and Q is the number of queries.
Each of the next S lines contains three integers:c1; C2 and D indicating the average sound
Intensity level on the street connecting the Crossings C1 and c2 (c1? = C2) is D decibels.
Each of the next Q lines contains, integers c1 and c2 (c1? = C2) asking for the minimum sound
Intensity level must is able to tolerate on order to get from crossing C1 to crossing C2.
The input would terminate with three zeros form C, S and Q.
Output
For each test, the input RST output the test Case number (starting from 1) as shown in the
Sample output. Then to each query with the input print a line giving the minimum sound intensity level
(in decibels) must is able to tolerate in order to get from the RST to the second crossing in the
Query. If there exists no path between them just print the line \no path ".
Print a blank line between consecutive test cases.
Sample Input
7 9 3
1 2 50
1 3 60
2 4 120
2 5 90
3 6 50
4 6 80
4 7 70
5 7 40
6 7 140
1 7
2 6
6 2
7 6 3
1 2 50
1 3 60
2 4 120
3 6 50
4 6 80
5 7 40
9 {
1 7
2 4
0 0 0
Sample Output
Case #1
80
60
60
Case #2
40
No path
80

#include <stdio.h> #include <string.h> #include <queue> #include <algorithm>using namespace std ; int pre[110];struct node{int x,y,cost;} Edges[10010];bool CMP (node A,node b) {return a.cost < b.cost;}    void Init () {for (int i = 0;i<=110;i++) {Pre[i] = i;    }}int finds (int x) {int t = x;    while (x = Pre[x]) {x = pre[x];        } while (t! = Pre[t]) {int m = t;        t = pre[t];    PRE[M] = x; } return x;}    int main () {int cases = 1;    int flag = 1;    int n,m,q;    int ms[110][110]; while (scanf ("%d%d%d", &n,&m,&q), n| | m| |        Q) {memset (ms,0,sizeof (ms));        int x,y,c;        Init ();            for (int i = 0;i<m;i++) {scanf ("%d%d%d", &x,&y,&c);            edges[i].x = x;            Edges[i].y = y;        Edges[i].cost = C;        } sort (edges,edges+m,cmp);        int qs_u[10010],qs_v[10010]; for (int i = 0;i<q;i++) {scanf ("%d%d", &qs_u[i],&qs_v[i]);            } for (int i=0; i<m;i++) {int u = finds (edges[i].x);            int v = finds (EDGES[I].Y);                if (U = v) {Pre[u] = V;            MS[EDGES[I].X][EDGES[I].Y] = ms[edges[i].y][edges[i].x] = edges[i].cost;        }} if (flag ==1) flag = 0;        Else puts ("");        printf ("Case #%d\n", cases++);            for (int i = 0;i<q;i++) {int is_find = 0;            int vis[110];            int s = qs_u[i];            int e = Qs_v[i];            Queue<int> QQ;            queue<int> cc;            memset (vis,0,sizeof (VIS));            Qq.push (s);            Vis[s] = 1;            Cc.push (0);                while (!qq.empty ()) {int z = Qq.front ();                int CF = Cc.front ();                Qq.pop ();                Cc.pop ();                    if (z = = e) {is_find = 1;                    printf ("%d\n", CF);                Break } for (int j = 1;j<=n;j++) {if (ms[z][j]&& vis[j] = = 0) {Vis[j] = 1;                        Qq.push (j);                    Cc.push (Max (cf,ms[z][j]));        }}} if (Is_find = = 0) puts ("no path"); }    }}

  

--Minimum spanning tree processing (Kruskal algorithm that always captures the minimum edge)

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.