题目:
3 0 990 692 990 0 179 692 179 0 1 1 2
179
想法:prim算法,看代码。。
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=110;
const int INF=0x3f3f3f3f;
int n,ans;
int map[N][N],dis[N],vis[N];
void Prim()
{
int i;
for(i=1;i<=n;i++)
{
dis[i]=map[1][i];
vis[i]=0;
}
dis[1]=0;
vis[1]=1;
int j,k,tmp;
for(i=1;i<=n;i++)
{
tmp=INF;
for(j=1;j<=n;j++)
if(!vis[j] && tmp>dis[j])
{
k=j;
tmp=dis[j];
}
if(tmp==INF)
break;
vis[k]=1;
ans+=dis[k];
for(j=1;j<=n;j++)
if(!vis[j] && dis[j]>map[k][j])
dis[j]=map[k][j];
}
}
int main()
{
while(cin>>n)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
cin>>map[i][j];
int q,a,b;
cin>>q;
while(q--)
{
cin>>a>>b;
map[a][b]=map[b][a]=0;
}
ans=0;
Prim();
cout<<ans<<endl;
}
return 0;
}