soj 2198 prime+DijkstraDijkstra

 soj 2198 prime
The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.
题目大意是,N个村庄,给你N个村庄修路的代价,问使得N个村庄联通的最小代价。
示例输入:
3
0 990 692
990 0 179
692 179 0
示例输出:
692

最小生成树两个解法prime和kruskal,一个从点出发,一个从最短的边出发,根据实际情况来定。

不太能理解为啥是692?我才开始以为求的是最终用的所有修路代价和,得出的692的代码是,不算sum

int main(){
	int n;
	scanf("%d",&n);
	while(n--){
		int i,j,max;
		scanf("%d",&m);
		for(i=0;i<m;i++)
			for(j=0;j<m;j++)
				scanf("%d",&edge[i][j]);
		prim();
        int max=0; 
		for(i=1;i<m;i++){
			if(max<dist[i])
				max=dist[i];
		}
	printf("%d",max);
	return 0;
	}
}

 这个是总和871

#include<stdio.h>
#define N 550
int m;
int edge[N][N];
int dist[N]={-1};
int visit[N]={0};

int prim(int n){
	visit[n]=1;
	int i,j,k,sum=0,min,x;
	for(j=0;j<m;j++){
		dist[j]=edge[n][j];	
	}//初始化,将初始起点和边加入Vnew,Enew 
	for(i=1;i<m;i++){
		min=100000;
		for(j=0;j<m;j++)
			if(min>dist[j]&&dist[j]!=0){
				min=dist[j];
				x=j;
			}//在Enew里找到最小边,加到最后权值 
		if(!visit[x]){
			sum+=dist[x];	
			visit[x]=1;
		}//如果这个点再Vnew外,加入 
		for(j=0;j<m;j++)
			if(edge[x][j]<dist[j]&&visit[j]==0) 
				dist[j]=edge[x][j];//将新加入的这个点的相关边加入Enew 
	}
	return sum;
}
int main(){
	int n;
	scanf("%d",&n);
	while(n--){
		int i,j,max;
		scanf("%d",&m);
		for(i=0;i<m;i++)
			for(j=0;j<m;j++)
				scanf("%d",&edge[i][j]);
		max=prim(0);
		for(i=1;i<m;i++){
			if(max<prim(i))
				max=prim(i);
		}
	printf("%d",max);
	return 0;
	}
}

 无向图最短路径:用dijkstra算法。给你n个点(编号从1到n)和m条边,求点1到各个点的最短距离。
输入:(n表示n个点,接着输入m跳边,每一行输入 a b c 表示a到b之间的距离是c)
6 9
1 2 6
1 3 3
2 3 2
2 4 5
3 4 3
3 5 2
4 5 2
4 6 3
5 6 5
输出:(输出点1到各个点的最短距离)
0 5 3 6 5 9

int a[10][10];
int *Dijkstra(int n){
	int i,j,min,min_i;
	int *dist,visit[n];
	for(i=0;i<n;i++) visit[i]=0;
	dist=(int *)malloc(sizeof(int)*n);
	for(i=0;i<n;i++) dist[i]=a[0][i];
	visit[0]=1;
	for(i=1;i<n;i++){
		min=999;
		for(j=1;j<n;j++){
			if(!visit[j]&&min>dist[j]){
				min=dist[j];
				min_i=j;
			}
		}
		visit[min_i]=1;
		for(j=1;j<n;j++){
			if(!visit[j]&&dist[j]>min+a[min_i][j])
				dist[j]=min+a[min_i][j];
		}
	}
	return dist;
}
int main(){
	int m,n,i,j,k,*p,t;
	scanf("%d %d",&m,&n);
	for(i=0;i<m;i++){
		for(j=0;j<m;j++){
			if(i==j) a[i][j]=0;
			else a[i][j]=9999;
		}
	}
	for(i=0;i<n;i++){
		scanf("%d %d %d",&t,&j,&k);
		a[t-1][j-1]=k;
		a[j-1][t-1]=k;
	}	
	p=Dijkstra(m);
	for(i=0;i<m;i++)
		printf("%d ",p[i]);
} 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值