一开始比较笨,采取的方案是由已建成边串联起来的顶点视为一个整体,使用prim加点时这些顶点加入一个就级联加入,不增加额外的代价,相应dist[]分量置零,这样使用队列比较方便,但对队列元素处理的过程有点繁琐,要比较小心,每个点出队,于这个顶点串联的顶点要入队,同时处理 该顶点加入后对dist数组的影响:总体来说是比较麻烦的方案:
#include <iostream>
#include <queue>
using namespace std;
#define Max 101
struct edge
{
int cost;
int status;
};
edge map[Max][Max];
int dist[Max];
int Prim(int n)
{
queue<int> Q;
int MinSumCost=0;
dist[1]=0;
int count=0;
for (int i=2;i<=n;i++)dist[i]=map[1][i].cost;
Q.push(1);
while(!Q.empty())
{
int work=Q.front();
dist[work]=0;count++;
for (int k=2;k<=n;k++)
if(map[work][k].cost<dist[k])dist[k]=map[work][k].cost;
Q.pop();
for (int t=2;t<=n;t++)
{
if (map[work][t].status==1&&dist[t]!=0)Q.push(t);
}
}
while(count!=n)
{
int min_cost,Min_pos;
min_cost=100000000;
for (int j=2;j<=n;j++)
{
if(dist[j]<min_cost&&dist[j]!=0)
{
Min_pos=j;
min_cost=dist[j];
}
}
MinSumCost+=dist[Min_pos];
dist[Min_pos]=0;
Q.push(Min_pos);
while(!Q.empty())
{
int work=Q.front();
dist[work]=0;count++;
for (int k=2;k<=n;k++)
if(map[work][k].cost<dist[k])dist[k]=map[work][k].cost;
Q.pop();
for (int t=2;t<=n;t++)
{
if (map[work][t].status==1&&dist[t]!=0)Q.push(t);
}
}
}
return MinSumCost;
}
int main()
{
int n;
while(cin>>n&&n)
{
int from,to,cost,statue;
int num=n*(n-1)/2;
while(num--)
{
cin>>from>>to>>cost>>statue;
map[from][to].cost=cost;
map[to][from].cost=cost;
map[from][to].status=statue;
map[to][from].status=statue;
}
cout<<Prim(n)<<endl;
}
return 0;
}
其实完全有更简单的方案,将已有道路相同的点之间的边权值置为0,而不是输入的代价,这样算法处理起来就统一了,细节处理起来没那么麻烦,在使用prim算法时,要注意常常是通过加入顶点数目进行统计来终止输入的,但这里不能单纯依靠dist分量来标示对应顶点是否已经加入,开始使用了一个投机取巧的方法,设置count为全局变量,当输入已有路径将对应count++;这是致命的错误,而且一般的测试用例检测不出,因为当这条边的一个顶点加入之后,另一个顶点的值自动被更新为0而不增加count计数,看似歪打正着,却忽略了这些已经建成的边构成 环路 的情形,这种情况有条边是冗余的,这样 就使count值 多加了1,以致最后还有一个顶点没有加入算法就已经结束了
代码如下:
#include <iostream>
using namespace std;
#define Max 101
/*int count;*///设置为全局变量,输入已建成的路径时直接加1标记为代价为0意味着顶点已被加入到树上 也可以不适用count机制检测到最小权值为0结束 不对,0不一定是与树集合的距离
struct edge
{
int cost;
int status;
};
edge map[Max][Max];
int dist[Max];
bool close_edge[Max];
int Prim(int n)
{
int MinSumCost=0;
memset(close_edge,false,sizeof(close_edge));
close_edge[1]=true;dist[1]=0;
int count=1;
for (int i=2;i<=n;i++)dist[i]=map[1][i].cost;
while(count!=n)
{
int min_cost,Min_pos;
min_cost=100000000;
for (int j=2;j<=n;j++)
if(dist[j]<min_cost&&!close_edge[j])
{
Min_pos=j;
min_cost=dist[j];
}
MinSumCost+=dist[Min_pos];
close_edge[Min_pos]=true;
count++;
for (j=2;j<=n;j++)
if(dist[j]>map[Min_pos][j].cost&&!close_edge[j])dist[j]=map[Min_pos][j].cost;//其不用!close_edge[j]应该也能通过
}
return MinSumCost;
}
int main()
{
int n;
while(cin>>n&&n)
{
int from,to,cost,statue;
int num=n*(n-1)/2;
while(num--)
{
cin>>from>>to>>cost>>statue;
if(statue)map[from][to].cost=map[to][from].cost=0/*,count++*/;//错prim是加点,并不是加边统计,0边不一定都取
else map[from][to].cost=map[to][from].cost=cost;
}
cout<<Prim(n)<<endl;
}
return 0;
}
another solution of prim:
#include <iostream>
using namespace std;
#define Max 101
struct edge
{
int cost;
int status;
};
edge map[Max][Max];
int dist[Max];
bool ok(int n)
{
for (int i=1;i<=n;i++)
if(dist[i]!=-1)return false;
return true;
}
int Prim(int n)
{
int MinSumCost=0;
dist[1]=-1;
for (int i=2;i<=n;i++)dist[i]=map[1][i].cost;
while(!ok(n))
{
int min_cost,Min_pos;
min_cost=100000000;
for (int j=2;j<=n;j++)
if(dist[j]<min_cost&&dist[j]!=-1)
{
Min_pos=j;
min_cost=dist[j];
}
MinSumCost+=dist[Min_pos];
dist[Min_pos]=-1;//不能用0识别,不然有些点自动加入了,并没有处理其对dist【】的更新
for (j=2;j<=n;j++)
if(dist[j]>map[Min_pos][j].cost&&dist[j])dist[j]=map[Min_pos][j].cost;//其不用!close_edge[j]应该也能通过
}
return MinSumCost;
}
int main()
{
int n;
while(cin>>n&&n)
{
int from,to,cost,statue;
int num=n*(n-1)/2;
while(num--)
{
cin>>from>>to>>cost>>statue;
if(statue)map[from][to].cost=map[to][from].cost=0/*,count++*/;//错prim是加点,并不是加边统计,0边不一定都取
else map[from][to].cost=map[to][from].cost=cost;
}
cout<<Prim(n)<<endl;
}
return 0;
}