第一次AC时,用的是Kruskal算法&并查集,现在用Prim实现一次。
麻烦点的CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int SIZE = 101;
const int INF = 0x7fffffff;
int graph[SIZE][SIZE]; //存储图结构
int vis[SIZE]; //标记是否走过
int dis[SIZE]; //存储最小边
int n;
void init()
{
memset(vis, 0, sizeof(vis));
memset(graph, 0, sizeof(graph));
memset(dis, 0, sizeof(dis));
}//init
int prim(int src)
{
int i, j;
int min, v;
int tot = 0;
for(i = 1; i <= n; i++)
{
dis[i] = graph[src][i];
}
dis[src] = 0; //初始化源点的最短距离为0
vis[src] = 1;
for(i = 1; i < n; i++)
{
min = INF;
for(j = 1; j <= n; j++)
{
if(!vis[j] && dis[j] < min)
{
v = j;
min = dis[j];
}
}
vis[v] = 1;
tot += min;
for(j = 1; j <= n; j++)
{
if(!vis[j]) dis[j] <?= graph[v][j];
}
}
return tot;
}//prim
int main()
{
int i, j;
int Q, u, v;
while(~scanf("%d", &n))
{
init();
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%d", &graph[i][j]);
}
}
for(scanf("%d", &Q); Q > 0; Q--)
{
scanf("%d%d", &u, &v);
graph[u][v] = graph[v][u] = 0;
}
printf("%d\n", prim(1));
}
return 0;
#include <stdlib.h>
#include <string.h>
using namespace std;
const int SIZE = 101;
const int INF = 0x7fffffff;
int graph[SIZE][SIZE]; //存储图结构
int vis[SIZE]; //标记是否走过
int dis[SIZE]; //存储最小边
int n;
void init()
{
memset(vis, 0, sizeof(vis));
memset(graph, 0, sizeof(graph));
memset(dis, 0, sizeof(dis));
}//init
int prim(int src)
{
int i, j;
int min, v;
int tot = 0;
for(i = 1; i <= n; i++)
{
dis[i] = graph[src][i];
}
dis[src] = 0; //初始化源点的最短距离为0
vis[src] = 1;
for(i = 1; i < n; i++)
{
min = INF;
for(j = 1; j <= n; j++)
{
if(!vis[j] && dis[j] < min)
{
v = j;
min = dis[j];
}
}
vis[v] = 1;
tot += min;
for(j = 1; j <= n; j++)
{
if(!vis[j]) dis[j] <?= graph[v][j];
}
}
return tot;
}//prim
int main()
{
int i, j;
int Q, u, v;
while(~scanf("%d", &n))
{
init();
for(i = 1; i <= n; i++)
{
for(j = 1; j <= n; j++)
{
scanf("%d", &graph[i][j]);
}
}
for(scanf("%d", &Q); Q > 0; Q--)
{
scanf("%d%d", &u, &v);
graph[u][v] = graph[v][u] = 0;
}
printf("%d\n", prim(1));
}
return 0;
}
较简单的CODE:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
const int INF = 0x0fffffff;
const int SIZE = 101;
int w[SIZE][SIZE];
int d[SIZE], v[SIZE];
int n, m;
int prim(int src)
{
int i, j;
int tot = 0;
for(i = 1; i <= n; i++) d[i] = (i == src)? 0:INF;
for(i = 1; i <= n; i++)
{
int x, m = INF;
for(int y = 1; y <= n; y++) if(!v[y] && d[y] < m) m = d[x=y]; //min
v[x] = 1;
tot += m;
for(int y = 1; y <= n; y++) d[y] <?= w[x][y];
} // d[y] <?= d[x]+w[x][y]; Dijkstra
return tot;
} //prim
void init()
{
memset(w, 0, sizeof(w));
memset(d, 0, sizeof(d));
memset(v, 0, sizeof(v));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
w[i][j] = INF;
}
}
return ;
}
int main()
{
while(~scanf("%d", &n))
{
init();
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
scanf("%d", &w[i][j]);
}
}
for(scanf("%d", &m); m > 0; m--)
{
int u, v;
scanf("%d%d", &u, &v);
w[u][v] = w[v][u] = 0;
}
printf("%d\n", prim(1));
}
return 0;
}
#include <stdlib.h>
#include <string.h>
using namespace std;
const int INF = 0x0fffffff;
const int SIZE = 101;
int w[SIZE][SIZE];
int d[SIZE], v[SIZE];
int n, m;
int prim(int src)
{
int i, j;
int tot = 0;
for(i = 1; i <= n; i++) d[i] = (i == src)? 0:INF;
for(i = 1; i <= n; i++)
{
int x, m = INF;
for(int y = 1; y <= n; y++) if(!v[y] && d[y] < m) m = d[x=y]; //min
v[x] = 1;
tot += m;
for(int y = 1; y <= n; y++) d[y] <?= w[x][y];
} // d[y] <?= d[x]+w[x][y]; Dijkstra
return tot;
} //prim
void init()
{
memset(w, 0, sizeof(w));
memset(d, 0, sizeof(d));
memset(v, 0, sizeof(v));
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
w[i][j] = INF;
}
}
return ;
}
int main()
{
while(~scanf("%d", &n))
{
init();
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
scanf("%d", &w[i][j]);
}
}
for(scanf("%d", &m); m > 0; m--)
{
int u, v;
scanf("%d%d", &u, &v);
w[u][v] = w[v][u] = 0;
}
printf("%d\n", prim(1));
}
return 0;
}