还是畅通工程
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 22226 Accepted Submission(s): 9939
Problem Description
某省调查乡村交通状况,得到的统计表中列出了任意两村庄间的距离。省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可),并要求铺设的公路总长度为最小。请计算最小的公路总长度。
Input
测试输入包含若干测试用例。每个测试用例的第1行给出村庄数目N ( < 100 );随后的N(N-1)/2行对应村庄间的距离,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间的距离。为简单起见,村庄从1到N编号。
当N为0时,输入结束,该用例不被处理。
当N为0时,输入结束,该用例不被处理。
Output
对每个测试用例,在1行里输出最小的公路总长度。
Sample Input
3 1 2 1 1 3 2 2 3 4 4 1 2 1 1 3 4 1 4 1 2 3 3 2 4 2 3 4 5 0
Sample Output
3 5Huge input, scanf is recommended.HintHint
Prim(邻接矩阵, 134ms):
#include <stdio.h>
#include <string.h>
#define N 100
int n;
int map[N][N], low[N];
bool vis[N];
void Init(){
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i ++){
low[i] = 0xfffffff;
for(int j = 1; j <= n; j ++){
map[i][j] = 0xfffffff;
}
}
}
int Prim(int choose){
low[choose] = 0;
vis[choose] = 1;
for(int i = 1; i <= n; i ++){
low[i] = map[choose][i];
}
int ans = 0;
for(int j = 1; j < n; j ++){
int Min = 0xfffffff;
for(int i = 1; i <= n; i ++){
if(!vis[i] && Min > low[i]){
Min = low[i];
choose = i;
}
}
vis[choose] = 1;
ans += Min;
for(int i = 1; i <= n; i ++){
if(!vis[i] && low[i] > map[choose][i]){
low[i] = map[choose][i];
}
}
}
return ans;
}
int main()
{
int start, end, len;
while(scanf("%d", &n), n){
Init();
for(int i = 0; i < n * (n - 1) / 2; i ++){
scanf("%d%d%d", &start, &end, &len);
if(map[start][end] > len){
map[start][end] = map[end][start] = len;
}
}
int ans = Prim(1);
printf("%d\n", ans);
}
return 0;
}
Prim(邻接表, 250ms):
#include <stdio.h>
#include <string.h>
#include <vector>
#define N 100
using namespace std;
struct Node{
int next;
int len;
};
int n;
int low[N];
bool vis[N];
vector<Node>q[N];
void Init(){
memset(vis, 0, sizeof(vis));
for(int i = 1; i <= n; i ++){
low[i] = 0xfffffff;
q[i].clear();
}
}
int Prim(int choose){
low[choose] = 0;
vis[choose] = 1;
for(int i = 0; i < q[choose].size(); i ++){
int next = q[choose].at(i).next;
int len = q[choose].at(i).len;
low[next] = len;
}
int ans = 0;
for(int j = 1; j < n; j ++){
int Min = 0xfffffff;
for(int i = 1; i <= n; i ++){
if(!vis[i] && Min > low[i]){
Min = low[i];
choose = i;
}
}
vis[choose] = 1;
ans += Min;
for(int i = 0; i < q[choose].size(); i ++){
int next = q[choose].at(i).next;
int len = q[choose].at(i).len;
if(!vis[next] && low[next] > len){
low[next] = len;
}
}
}
return ans;
}
int main()
{
Node nw;
int start, end, len;
while(scanf("%d", &n), n){
Init();
for(int i = 0; i < n * (n - 1) / 2; i ++){
scanf("%d%d%d", &start, &end, &len);
nw.next = end;
nw.len = len;
q[start].push_back(nw);
nw.next = start;
q[end].push_back(nw);
}
int ans = Prim(1);
printf("%d\n", ans);
}
return 0;
}
Kruskal(邻接表, 296ms):
#include <stdio.h>
#include <queue>
#define N 110
using namespace std;
struct Node{
int start;
int end;
int len;
friend bool operator < (const Node& a, const Node& b){
return a.len > b.len;
}
};
int n;
priority_queue<Node>q;
int Father[N];
void Init(){
for(int i = 0; i <= n; i ++){
Father[i] = i;
}
}
int GetFather(int cur){
return Father[cur] == cur ? cur : Father[cur] = GetFather(Father[cur]);
}
bool Join(int start, int end){
int root1 = GetFather(start);
int root2 = GetFather(end);
if(root1 == root2){
return 0;
}
else{
Father[root1] = root2;
return 1;
}
}
int Kruskal(){
int ans = 0;
while(!q.empty()){
Node cur = q.top();
q.pop();
if(Join(cur.start, cur.end)){
ans += cur.len;
}
}
return ans;
}
int main()
{
Node nw;
int start, end, len;
while(scanf("%d", &n), n){
Init();
for(int i = 0; i < n * (n - 1) / 2; i ++){
scanf("%d%d%d", &start, &end, &len);
nw.start = start;
nw.end = end;
nw.len = len;
q.push(nw);
}
int ans = Kruskal();
printf("%d\n", ans);
}
return 0;
}