标签:memset int lan include ready using ons pre this
http://poj.org/problem?id=2421
Input
Output
Sample Input
3 0 990 692 990 0 179 692 179 0 1 1 2
Sample Output
179
题解:建好的边记为 0
代码:
#include <stdio.h> #include <iostream> #include <string.h> using namespace std; #define inf 0x3f3f3f3f int N, P; int mp[110][110]; bool vis[110]; int dis[110]; int prim() { memset(dis, inf, sizeof(dis)); memset(vis, false, sizeof(vis)); dis[1] = 0; int temp; int res = 0; for(int i = 1; i <= N; i ++) dis[i] = mp[1][i]; for(int i = 1; i <= N; i ++) { int minn = inf; for(int j = 1; j <= N; j ++) { if(!vis[j] && dis[j] < minn) { temp = j; minn = dis[j]; } } vis[temp] = true; for(int j = 1; j <= N; j ++) if(!vis[j]) dis[j] = min(dis[j], mp[temp][j]); } for(int i = 1; i <= N; i ++) res += dis[i]; return res; } int main() { scanf("%d", &N); memset(mp, inf, sizeof(mp)); for(int i = 1; i <= N; i ++) { for(int j = 1; j <= N; j ++) { int s; scanf("%d", &s); if(s < mp[i][j]) { mp[i][j] = s; mp[j][i] = mp[i][j]; } } } int sum = 0; scanf("%d", &P); for(int i = 1; i <= P; i ++) { int a, b; scanf("%d%d", &a, &b); mp[a][b] = mp[b][a] = 0; } int ans = prim(); printf("%d\n", ans); return 0; }
标签:memset int lan include ready using ons pre this
原文地址:https://www.cnblogs.com/zlrrrr/p/9745136.html