HDU 1598 find the most comfortable road

Source: Internet
Author: User

Greedy + and query set

Fighting ~~~~~!!

1 # include <iostream> 2 # include <algorithm> 3 # include <cmath> 4 # include <cstdio> 5 using namespace STD; 6 # define maxn 300 7 # define INF 0x3fffffff 8 int par [maxn]; 9 int n, m; 10 11 struct node12 {13 int X; 14 int y; 15 int W; 16}; 17 node E [maxn * maxn/2]; 18 int CMP (const node & A, const node & B) 19 {20 return. W <B. w; 21} 22 void Init () 23 {24 for (INT I = 1; I <= N; I ++) 25 par [I] = I; 26} 27 int find (in T x) 28 {29 int T, R, K; 30 k = x; 31 While (X! = Par [x]) 32 x = par [X]; 33 while (K! = X) // tanta34 {35 t = par [k]; 36 par [k] = x; 37 k = T; 38} 39 return X; 40} 41 void merge (int x, int y) 42 {43 int A = find (x); 44 int B = find (y); 45 if (! = B) 46 {47 par [a] = B; 48} 49} 50 void Kruskal () 51 {52 int Q, St, en, mindis, I, j; 53 While (CIN> N> m) 54 {55 for (INT I = 0; I <m; I ++) 56 CIN> E [I]. x> E [I]. y> E [I]. w; 57 sort (E, E + M, CMP); 58 CIN> q; 59 While (Q --) 60 {61 CIN> st> en; 62 mindis = inf; 63 for (I = 0; I <m; I ++) // meijuzuixiao64 {65 Init (); 66 for (j = I; j <m; j ++) // zhaozuidabian67 {68 Merge (E [J]. x, E [J]. y); 69 If (find (ST) = find (En) // start point end point connection 70 {71 Mindis = min (mindis, E [J]. w-e [I]. w); 72 break; 73} 74} 75 if (j = m) break; // J = m continue enumeration and 76} 77 If (mindis! = Inf) cout <mindis <Endl; 78 else cout <"-1" <Endl; 79} 80} 81} 82 int main () 83 {84 freopen ("input.txt", "r", stdin); 85 Kruskal (); 86 return 0; 87}

 

HDU 1598 find the most comfortable road

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.