###### B - Constructing Roads （vj）

#include<bits/stdc++.h>
using namespace std;
const int maxn=0x3f3f3f;
int Map[105][105],vis[105],low[105];
int n,ans;
void prime()
{
int next;
memset(vis,0,sizeof(vis));
for(int i=1;i<=n;i++) low[i]=Map[1][i];
vis[1]=1;
for(int i=1;i<n;i++)
{
int mini=maxn;
for(int j=1;j<=n;j++)
{
if(!vis[j]&&low[j]<mini)
{
mini=low[j];
next=j;
}
}
ans+=mini;
vis[next]=1;
for(int j=1;j<=n;j++)
if(!vis[j]&&low[j]>Map[next][j])
low[j]=Map[next][j];
}
}
int main()
{
while(scanf("%d",&n)==1)
{
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
scanf("%d",&Map[i][j]);
int q,a,b;
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&a,&b);
Map[a][b]=0,Map[b][a]=0;
}
ans=0;
prime();
printf("%d\n",ans);
}
return 0;
}

2012-04-05 17:49:50

2015-08-11 21:07:43

2016-05-21 19:43:15

#### HDU 1102 Constructing Roads （裸的并查集）

2014-06-07 19:55:30

#### hdu 1025 Constructing Roads In JGShining&#39;s Kingdom(最长上升子序列(LIS)O(nlogn)算法)

2013-06-19 19:28:40

2013-04-21 23:06:26

#### 杭电OJ——1025 Constructing Roads In JGShining's Kingdom(比较有趣的一道题目，思路详解)

2012-11-10 01:48:30

2017-06-11 01:32:13

2018-02-22 00:19:24

#### hdu 1102 Constructing Roads(最小生成树 Prim)

2014-08-19 09:04:55