标签:
1 #include<stdio.h> 2 #include<string.h> 3 using namespace std; 4 #define N 505 5 #define INF 0x7fffffff 6 int n , m , k ; 7 int g[N][N] , vis[N] , low[N] ; 8 9 void prim () 10 { 11 int pos = 1 , res = 0 ; 12 memset (vis , 0 , sizeof(vis)) ; 13 vis[pos] = 1 ; 14 for (int i = 1 ; i <= n ; i++) 15 if ( !vis[i] ) 16 low[i] = g[pos][i] ; 17 for (int i = 1 ; i < n ; i++) { 18 int minn = INF ; 19 pos = -1 ; 20 for (int j = 1 ; j <= n ; j++) 21 if ( !vis[j] && low[j] < minn) { 22 minn = low[j] ; 23 pos = j ; 24 } 25 if (pos == -1) { 26 puts ("-1") ; 27 return ; 28 } 29 res += minn ; 30 vis[pos] = 1 ; 31 for (int j = 1 ; j <= n ; j++) 32 if (!vis[j] && low[j] > g[pos][j]) 33 low[j] = g[pos][j] ; 34 } 35 printf ("%d\n" , res) ; 36 } 37 int main() 38 { 39 // freopen ("a.txt" , "r" , stdin); 40 int u , v , w ; 41 int T , t; 42 scanf ("%d" , &T) ; 43 while (T--) { 44 scanf ("%d%d%d" , &n , &m , &k) ; 45 for (int i = 0 ; i <= n ; i++) 46 for (int j = 0 ; j <= n ; j++) 47 g[i][j] = INF ; 48 for (int i = 0 ; i < m ; i++) { 49 scanf ("%d%d%d" , &u , &v , &w) ; 50 if (g[u][v] > w)//notice 51 g[u][v] = g[v][u] = w ; 52 } 53 for (int i = 0 ; i < k ; i++) { 54 scanf ("%d%d" , &t , &u) ; 55 for (int j =1 ; j <= t - 1 ; j++) { 56 scanf ("%d" , &v) ; 57 g[u][v] = g[v][u] = 0 ; 58 } 59 } 60 prim () ; 61 } 62 }
给偶上了一课,本以为用kruskal就能一条路走到黑:
但遗憾的TLE了,
标签:
原文地址:http://www.cnblogs.com/get-an-AC-everyday/p/4290188.html