POJ2263 & amp; 1797 Max Spanning Tree

Source: Internet
Author: User

Both questions are the minimum values in the maximum spanning tree. Exit when the start and end points are found.
There is no trap. paste the Code directly.
1797
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string>
# Include <cmath>
# Include <cstring>
# Include <queue>
# Include <set>
# Include <vector>
# Include <stack>
# Include <map>
# Include <iomanip>
# Deprecision Max 100000
# Define inf 1 <28
Using namespace std;
Struct kdq
{
Int s, e, l;
} Line [Max];
Int n, m;
Int f [Max * 10];
Int find (int x)
{
Return f [x] = x? X: f [x] = find (f [x]);
}
Void merge (int x, int y)
{
If (x> y)
F [x] = y;
Else
F [y] = x;
}
Bool cmp (kdq & a, kdq & B)
{
Return a. l> B. l;
}
Int kruskal ()
{
Int I, j;
For (I = 1; I <= n + 10; I ++)
F [I] = I;
Int ans = inf;
For (I = 0; I <m; I ++)
{
Int x = find (line [I]. s );
Int y = find (line [I]. e );
If (x! = Y)
{
Merge (x, y );
If (ans> line [I]. l)
Ans = line [I]. l;
If (find (1) = find (n ))
Return ans;
}
}
}
Int main ()
{
Int I, j, k = 0, l;
Int T;
Cin> T;
Int x, y, s;
While (T --)
{
K ++;
Cin> n> m;
For (I = 0; I <m; I ++)
{
Scanf ("% d", & line [I]. s, & line [I]. e, & line [I]. l );
}
Sort (line, line + m, cmp );
Printf ("Scenario # % d: \ n", k );
Cout <kruskal () <endl;
}
Return 0;
}
/*
1
4 2
1 3 1
3 4 2
*/
2263
[Cpp]
# Include <iostream>
# Include <cstdio>
# Include <algorithm>
# Include <string>
# Include <cmath>
# Include <cstring>
# Include <queue>
# Include <set>
# Include <vector>
# Include <stack>
# Include <map>
# Include <iomanip>
# Define PI acos (-1.0)
# Deprecision Max 20005
# Define inf 1 <28
# Define LL (x) (x <1)
# Define RR (x) (x <1 | 1)
Using namespace std;
 
Int n, m;
Struct kdq
{
Int s, e, l;
} Line [Max];
 
Bool cmp (kdq & a, kdq & B)
{
Return a. l> B. l;
}
Int f [Max];
Void init ()
{
For (int I = 0; I <= n; I ++)
F [I] = I;
}
Int find (int x)
{
Return f [x] = x? X: f [x] = find (f [x]);
}
Void Union (int x, int y)
{
X = find (x );
Y = find (y );
If (x = y) return;
If (x> y)
F [x] = y;
Else
F [y] = x;
}
Int kruskal (int s, int e, int num)
{
Init ();
Int ans = inf;
For (int I = 0; I <num; I ++)
{
Int x = find (line [I]. s );
Int y = find (line [I]. e );
If (x! = Y)
{
Union (x, y );
If (ans> line [I]. l)
Ans = line [I]. l;
If (find (s) = find (e ))
Return ans;
}
}
}
Int main ()
{
Int I, j, k, l;
Int cc = 0;
Char aaaa [40], bbbb [40];
While (scanf ("% d", & n, & m), (n + m ))
{
Map <string, int> mymap;
String a, B;
Int num = 1;
Int num1 = 0;
While (m --)
{
Scanf ("% s % d", aaaa, bbbb, & l );
A = aaaa, B = bbbb;
If (! Mymap [a])
{
Mymap [a] = num;
Num ++;
}
If (! Mymap [B])
{
Mymap [B] = num;
Num ++;
}
Line [num1]. s = mymap [a], line [num1]. e = mymap [B], line [num1]. l = l;
Num1 ++;
}
Sort (line, line + num1, cmp );
Scanf ("% s", aaaa, bbbb );
A = aaaa, B = bbbb;
Printf ("Scenario # % d \ n % d tons \ n", ++ cc, kruskal (mymap [a], mymap [B], num1 ));
}
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.