HDU 3371 Connect the Cities

Source: Internet
Author: User

Question MST .. I don't need to do this kind of template question anymore .... Sprint northeast competition !!!!

Problem: 3371 (Connect the Cities) Judge Status: Accepted
RunId: 8394387 Language: C ++ Author: CherryChou
Code Render Status: Rendered By hdoj c ++ Code Render Version 0.01 Beta
 

# Include <cstdio>
# Include <cstring>
# Include <queue>
# Include <vector>
# Include <climits>

Using namespace std;

Const int maxn = 2005;
Const int maxm = 4000005;
Const int inf = INT_MAX;
Typedef pair <int, int> node;

Struct edge
{
Int u, v, w, next;
} E [maxm];

Struct cmp
{
Bool operator () (const node & a, const node & B) const
{
Return a. second> B. second;
}
};

Int num, head [maxn];
Int dis [maxn], vis [maxn];
Priority_queue <node, vector <node>, cmp> q;

Inline void add (int u, int v, int w)
{
E [num]. u = u;
E [num]. v = v;
E [num]. w = w;
E [num]. next = head [u];
Head [u] = num ++;
}

Void addedge (int u, int v, int w)
{
Add (u, v, w );
Add (v, u, w );
}

Int prime (int s)
{
Int I, u, v, mincost = 0;
Dis [s] = 0;
Q. push (make_pair (s, 0 ));
While (! Q. empty ())
{
Node a = q. top ();
Q. pop ();
U = a. first;
If (vis [u])
Continue;
Vis [u] = 1;
Mincost + = a. second;
For (I = head [u]; I! =-1; I = e [I]. next)
{
V = e [I]. v;
If (! Vis [v] & e [I]. w <dis [v])
{
Dis [v] = e [I]. w;
Q. push (make_pair (v, e [I]. w ));
}
}
}
Return mincost;
}

Void init (int n)
{
Num = 0;
Memset (head,-1, sizeof (head ));
Memset (vis, 0, sizeof (vis ));
For (int I = 0; I <= n; I ++)
{
Dis [I] = inf;
Vis [I] = 0;
}
}


Int main ()
{
Int T;
Scanf ("% d", & T );
While (T --)
{
Int n, m, k, U, V, W, t, x, y;
Scanf ("% d", & n, & m, & k );
Init (n );
For (int I = 1; I <= m; I ++)
{
Scanf ("% d", & U, & V, & W );
Addedge (U, V, W );
}
While (k --)
{
Scanf ("% d", & t );
Scanf ("% d", & x );
For (int I = 1; I <t; I ++)
{
Scanf ("% d", & y );
Addedge (x, y, 0 );
}
}
Int ans = prime (1 );
Int flag = 1;
For (int I = 1; I <= n; I ++)
{
If (vis [I] = 0)
{
Flag = 0;
Break;
}
}
If (flag)
Printf ("% d \ n", ans );
Else printf ("-1 \ n ");
}
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.