标签:
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 12058 | Accepted: 5008 |
Description
Input
Output
Sample Input
5 8 1 2 3 1 3 7 2 3 10 2 4 4 2 5 8 3 4 6 3 5 2 4 5 17
Sample Output
42
Hint
Source
#include <cstdio> #include <cstring> #include <iostream> using namespace std; const int INF = 0x3f3f3f3f; int map[1010][1010], dis[1010], vis[1010]; int n, m; void Prim() { int TotalMon = 0; memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) dis[i] = map[1][i]; vis[1] = 1; for(int i = 1; i < n; i++){ int temp = 1, max = 0; for(int j = 1; j <= n; j++) { if(!vis[j] && dis[j] > max) { temp = j; max = dis[j]; } } if(max == 0){ printf("-1\n"); return; } TotalMon += max; vis[temp] = 1; for(int j = 1; j <= n; j++) if(!vis[j] && dis[j] < map[temp][j] && map[temp][j] != INF) dis[j] = map[temp][j]; } printf("%d\n", TotalMon); } int main() { while(~scanf("%d %d", &n, &m)) { int u, v, w; memset(map, -1, sizeof(map)); //权值初始化为负; for(int i = 0; i < m; i++){ scanf("%d %d %d", &u, &v, &w); if(map[u][v] < w) map[u][v] = map[v][u] = w; } Prim(); } return 0; }
Poj2377--Bad Cowtractors(最大生成树)
标签:
原文地址:http://www.cnblogs.com/fengshun/p/4764731.html