Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 18668 | Accepted: 8648 |
Description
Input
Output
Sample Input
1 3 0 990 692 990 0 179 692 179 0
Sample Output
692
Hint
Source
Accepted | 820K | 235MS | C++ | 1265B |
#include<stdio.h> #include<algorithm> using namespace std; const int maxn = 500+10; int w[maxn][maxn]; int p[maxn]; int n,m; int MaxWeight; struct Edge{ int u,v; int w; }edge[maxn*maxn/2]; bool cmp(Edge a, Edge b) { return a.w < b.w; } int find(int x) { return x == p[x] ? x : p[x] = find(p[x]); } void Kruskal() { for(int i = 1; i <= n; i++) p[i] = i; sort(edge,edge+m,cmp); for(int i = 0; i < m; i++) { int u = find(edge[i].u); int v = find(edge[i].v); if(u != v) { p[v] = u; MaxWeight = max(MaxWeight, edge[i].w); } } } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf("%d", &w[i][j]); m = 0; for(int i = 1; i <= n; i++) { for(int j = i+1; j <= n; j++) { edge[m].u = i; edge[m].v = j; edge[m++].w = w[i][j]; } } MaxWeight = 0; Kruskal(); printf("%d\n", MaxWeight); } return 0; }
Accepted | 580K | 172MS | C++ | 943B |
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int maxn = 510; const int INF = 65536*maxn; int w[maxn][maxn]; int d[maxn]; int vis[maxn]; int n; int MaxWeight; void Prime() { for(int i = 1; i <= n; i++) d[i] = INF; d[1] = 0; memset(vis, 0, sizeof(vis)); for(int i = 1; i <= n; i++) { int x, m = INF; for(int y = 1; y <= n; y++) if(!vis[y] && d[y] <= m) m = d[x=y]; vis[x] = 1; MaxWeight = max(MaxWeight, d[x]); for(int y = 1; y <= n; y++) if(!vis[y]) d[y] = min(d[y], w[x][y]); } } int main() { int T; scanf("%d", &T); while(T--) { scanf("%d", &n); for(int i = 1; i <= n; i++) for(int j = 1; j <= n; j++) scanf("%d", &w[i][j]); MaxWeight = 0; Prime(); printf("%d\n", MaxWeight); } return 0; }