Constructing Roads
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 17353 Accepted Submission(s): 6600
Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B, or there exists a village C such that there is a road between A and C, and C and B are connected.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 1000]) between village i and village j.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.
Sample Input
3 0 990 692 990 0 179 692 179 0 1 1 2
Sample Output
179
最短路模板题
普利姆算法直接的模板,克鲁斯卡尔也可写,附上两种代码
prim:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define INF 0x3f3f3f3f
#define N 110
int dis[N][N];
int n;
void prim(){
int min,distance[N];
int vis[N],k;
memset(vis,0,sizeof(vis));
for(int i=1;i<=N;i++)
distance[i]=dis[1][i];
distance[1]=0;
vis[1]=1;
for(int v=1;v<n;v++){
min=INF;
k=1;
for(int i=1;i<=n;i++){
if(!vis[i]&&distance[i]<min){
min=distance[i];
k=i;
}
}
vis[k]=1;
for(int i=1;i<=n;i++){
if(!vis[i]&&distance[i]>dis[k][i])
distance[i]=dis[k][i];
}
}
int sum=0;
for(int i=2;i<=n;i++){//
sum+=distance[i];
// printf("%d ",distance[i]);
}
printf("%d\n",sum);
return;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
scanf("%d",&dis[i][j]);
}
}
int k,a,b;
scanf("%d",&k);
while(k--){
scanf("%d%d",&a,&b);
dis[a][b]=dis[b][a]=0;
}
// for(int i=1;i<=n;i++){
// for(int j=1;j<=n;j++){
// printf("%d ",dis[i][j]);
// }
// putchar('\n');
// }
prim();
}
return 0;
}
写克鲁斯卡尔的代码时遇见了最坑爹的问题,在杭电上C++AC而G++WA;后将调用<algorithm>的sort删去,改为qsort就神奇的AC了,原因至今不明:
问题链接http://acm.hdu.edu.cn/showproblem.php?pid=1102
问题代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
int per[110000],N,M;
struct str{
int a,b,x;
}edge[1001000];
int p[1100][1100];
int cmp(str a,str b){
return a.x<b.x;
}
void init(){
for(int i=1;i<=110000;i++)
per[i]=i;
}
int find(int x){
if(x==per[x])
return x;
return per[x]=find(per[x]);
}
bool join(int x,int y){
int i=find(x);
int j=find(y);
if(i==j)
return false;
per[i]=j;
return true;
}
int main(){
int N,a,b;
while(scanf("%d",&N)!=EOF){
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++){
scanf("%d",&p[i][j]);
}
scanf("%d",&M);
for(int m=1;m<=M;m++){
scanf("%d%d",&a,&b);
p[a][b]=0;
p[b][a]=0;
}
// for(int i=1;i<=N;i++){
// for(int j=1;j<=N;j++){
// printf("%d ",p[i][j]);
// }
// putchar('\n');
// }
int k=1;
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++){
edge[k].a=i;
edge[k].b=j;
edge[k].x=p[i][j];
// printf("--%d %d %d - %d %d %d\n",edge[k].a,edge[k].b,edge[k].x,i,j,p[i][j]);
k++;
}
M=k;
sort(edge,edge+M,cmp);
// for(int m=1;m<M;m++){
// printf("--%d %d %d\n",edge[m].a,edge[m].b,edge[m].x);
// }
init();
int sum=0;
for(int m=1;m<M;m++){
if(join(edge[m].a,edge[m].b)){
sum+=edge[m].x;
}
}
printf("%d\n",sum);
}
}
AC代码:
#include<stdio.h>
#include<string.h>
#include <stdlib.h>
int per[110000],N,M;
struct str{
int a,b,x;
}edge[1001000];
int p[1100][1100];
int cmp(const void *a,const void *b)
{
struct str *aa=(struct str *)a;
struct str *bb=(struct str *)b;
if(aa->x != bb->x)
return aa->x - bb->x;
else
return aa->a - bb->a;
}
void init(){
for(int i=0;i<=110000;i++)
per[i]=i;
}
int find(int x){
if(x==per[x])
return x;
return per[x]=find(per[x]);
}
bool join(int x,int y){
int i=find(x);
int j=find(y);
if(i==j)
return false;
per[i]=j;
return true;
}
int main(){
int N,a,b;
while(scanf("%d",&N)!=EOF){
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++){
scanf("%d",&p[i][j]);
}
scanf("%d",&M);
for(int m=1;m<=M;m++){
scanf("%d%d",&a,&b);
p[a][b]=0;
p[b][a]=0;
}
int k=1;
for(int i=1;i<=N;i++)
for(int j=1;j<=N;j++){
edge[k].a=i;
edge[k].b=j;
edge[k].x=p[i][j];
k++;
}
M=k;
qsort(&edge[1],M,sizeof(edge[1]),cmp);
init();
int sum=0;
for(int m=1;m<M;m++){
if(join(edge[m].a,edge[m].b)){
sum+=edge[m].x;
}
}
printf("%d\n",sum);
}
}