省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
输入:
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M (N, M < =100 );随后的 N 行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
输出:
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
样例输入:
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
样例输出:
3
?
两个思路:
1.DFS + Prim
2.并查集 + Prim
1.DFS + Prim
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int MaxInt = 9999;
const int MaxNum = 1000;
int c[MaxNum][MaxNum]; //邻接矩阵
int dist[MaxNum]; //每个节点到源点的最短距离
bool visited[MaxNum]; //每个节点是否访问过
void Prim(int s,int n)
{
for(int i = 1; i <= n; ++i)
dist[i] = c[s][i];
dist[s] = 0;
visited[s] = true;
//依次访问接下来的n - 1个unknown节点
for(int i = 1; i <= n - 1; ++i)
{
int tmp = MaxInt; //当前最短距离
int v = s; // 当前的节点
//在所有unknown节点中找到和源点距离最短的节点
for(int j = 1; j <= n; ++j)
{
if(!visited[j] && dist[j] < tmp)
{
v = j;
tmp = dist[j];
}
}
//找到了和源点距离最短的unknown节点,更改其访问数组
visited[v] = true;
//然后更新其邻接的边,所有的边都要访问一次,都可能会更新
for(int j = 1; j <= n; ++j)
{
if(!visited[j] && c[v][j] < MaxInt)
{
dist[j] = min(dist[j],c[v][j]); //prim算法!!
}
}
}
}
void DFS(int i, int n, int &count)
{
visited[i] = true;
count++;
for(int j = 1; j <= n; ++j)
{
if(!visited[j] && c[i][j] < MaxInt)
{
DFS(j,n,count);
}
}
}
int main()
{
int N,M; //N为道路条数,M为村庄数目
while(cin >> N >> M)
{
//跳出条件
if(N == 0)
break;
//初始化邻接矩阵
for(int i = 1; i <= M; ++i)
for(int j = 1; j <= M; ++j)
c[i][j] = MaxInt;
for(int i = 0; i < N; ++i)
{
int p,q,len;
cin >> p >> q >> len;
if(len < c[p][q])//如果有重边
{
c[p][q] = len;
c[q][p] = len; //无向图 如果有向图这句话就去掉
}
}
//初始化最短距离
for(int i = 1; i <= M; ++i)
dist[i] = MaxInt;
//初始化访问数组
for(int i = 1; i <= M; ++i)
visited[i] = false;
int count = 0;
DFS(1,M,count);
if(count != M)
{
cout << "?" << endl;
}
else
{
memset(visited,0,sizeof(visited));
Prim(1,M);
int min_dist = 0;
for(int i = 1; i <= M; ++i)
min_dist += dist[i];
cout << min_dist << endl;
}
}
return 0;
}
#include <iostream>
#include <algorithm>
#include <cstring>
#include <vector>
using namespace std;
const int MaxInt = 9999;
const int MaxNum = 1000;
int c[MaxNum][MaxNum]; //邻接矩阵
int dist[MaxNum]; //每个节点到源点的最短距离
bool visited[MaxNum]; //每个节点是否访问过
void Prim(int s,int n)
{
for(int i = 1; i <= n; ++i)
dist[i] = c[s][i];
dist[s] = 0;
visited[s] = true;
//依次访问接下来的n - 1个unknown节点
for(int i = 1; i <= n - 1; ++i)
{
int tmp = MaxInt; //当前最短距离
int v = s; // 当前的节点
//在所有unknown节点中找到和源点距离最短的节点
for(int j = 1; j <= n; ++j)
{
if(!visited[j] && dist[j] < tmp)
{
v = j;
tmp = dist[j];
}
}
//找到了和源点距离最短的unknown节点,更改其访问数组
visited[v] = true;
//然后更新其邻接的边,所有的边都要访问一次,都可能会更新
for(int j = 1; j <= n; ++j)
{
if(!visited[j] && c[v][j] < MaxInt)
{
dist[j] = min(dist[j],c[v][j]); //prim算法!!
}
}
}
}
int find(vector<int> &a, int x)
{
if(a[x] < 0)
return x;
else
return a[x] = find(a,a[x]);
}
void unionSets(vector<int> &a, int root1,int root2)
{
int b = find(a,root1);
int c = find(a,root2);
if(b == c)
return;
if(a[b] < a[c])
{
a[b] += a[c];
a[c] = b;
}
else
{
a[c] += a[b];
a[b] = c;
}
}
int main()
{
int N,M; //N为道路条数,M为村庄数目
vector<int> ivec; //并查集
while(cin >> N >> M)
{
//跳出条件
if(N == 0)
break;
ivec.assign(M+1,-1); //并查集初始化
//初始化邻接矩阵
for(int i = 1; i <= M; ++i)
for(int j = 1; j <= M; ++j)
c[i][j] = MaxInt;
for(int i = 0; i < N; ++i)
{
int p,q,len;
cin >> p >> q >> len;
unionSets(ivec,p,q); //插入并查集
if(len < c[p][q])//如果有重边
{
c[p][q] = len;
c[q][p] = len; //无向图 如果有向图这句话就去掉
}
}
//初始化最短距离
for(int i = 1; i <= M; ++i)
dist[i] = MaxInt;
//初始化访问数组
for(int i = 1; i <= M; ++i)
visited[i] = false;
int count = 0; //并查集的集合数
for(int i = 1; i <= M; ++i)
{
if(find(ivec,i) == i)
count++;
}
if(count != 1) //如果并查集的集合数不为1个,证明不联通
{
cout << "?" << endl;
}
else
{
memset(visited,0,sizeof(visited));
Prim(1,M);
int min_dist = 0;
for(int i = 1; i <= M; ++i)
min_dist += dist[i];
cout << min_dist << endl;
}
ivec.clear();
}
return 0;
}