首页 > 其他 > 详细

模板复习

时间:2015-11-28 18:05:05      阅读:242      评论:0      收藏:0      [点我收藏+]

prim

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define F(i,x) for(i=1;i<=x;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int MAXN=110;
int vis[MAXN],low[MAXN],mp[MAXN][MAXN];
int N;
void prime(){
	int i,j;
	int ans=0;
	mem(vis,0);mem(low,INF);
	vis[1]=1;
	F(i,N)low[i]=mp[1][i];
	F(i,N){
		int temp=INF,k;
		F(j,N)if(!vis[j]&&temp>low[j])temp=low[k=j];
		if(temp==INF)break;
		ans+=temp;
		vis[k]=1;
		F(j,N)if(!vis[j]&&mp[k][j]<low[j])low[j]=mp[k][j];
	}
	PI(ans);puts("");
}
int main(){
	while(~scanf("%d",&N)){
		int i,j;
		mem(mp,INF);
		F(i,N){
			F(j,N){
				scanf("%d",&mp[i][j]);
			}
		}
		prime();
	}
	return 0;
}

  kruthka

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<vector>
#include<algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
typedef long long LL;
#define mem(x,y) memset(x,y,sizeof(x))
#define T_T while(T--)
#define F(i,x) for(i=1;i<=x;i++)
#define SI(x) scanf("%d",&x)
#define SL(x) scanf("%lld",&x)
#define PI(x) printf("%d",x)
#define PL(x) printf("%lld",x)
#define P_ printf(" ")
const int MAXN=110;
int pre[MAXN];
int ans;
int find(int x){
	return x==pre[x]?x:find(pre[x]);
}
void merge(int x,int y,int z){
	if(pre[x]==0)pre[x]=x;
	if(pre[y]==0)pre[y]=y;
	int f1,f2;
	f1=find(x);f2=find(y);
	if(f1!=f2){
		pre[f1]=f2;ans+=z;
	}
}
int main(){
	int N;
	while(~scanf("%d",&N)){
		int i,j,x;
		mem(pre,0);
		ans=0;
		F(i,N){
			F(j,N){
				SI(x);
				merge(i,j,x);
			}
		}
		PI(ans);puts("");
	}
	return 0;
}

  

模板复习

原文:http://www.cnblogs.com/handsomecui/p/5002919.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!