一開始比較笨,採取的方案是由已建成邊串聯起來的頂點視爲一個整體,使用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;
}