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;
}