标签:
还是畅通工程浙大计算机研究生复试上机考试-2006年
prim 算法
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<cstdlib>
#include<set>
#include<stack>
#include<cstring>
using namespace std;
template<class T>inline T read(T&x)
{
char c;
while((c=getchar())<=32)if(c==EOF)return -1;
bool ok=false;
if(c=='-')ok=true,c=getchar();
for(x=0; c>32; c=getchar())
x=x*10+c-'0';
if(ok)x=-x;
return 1;
}
template<class T> inline T read_(T&x,T&y)
{
return read(x)!=-1&&read(y)!=-1;
}
template<class T> inline T read__(T&x,T&y,T&z)
{
return read(x)!=-1&&read(y)!=-1&&read(z)!=-1;
}
template<class T> inline void write(T x)
{
if(x<0)putchar('-'),x=-x;
if(x<10)putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
write(x);
putchar('\n');
}
//-------ZCC IO template------
const int maxn=101;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod 10007
int G[maxn][maxn];
int vis[maxn];
int mincost[maxn];
int V,E;
void prim()
{
For(i,0,V+1)
{
mincost[i]=inf;
vis[i]=false;
}
mincost[1]=0;
int ans=0;
while(true)
{
int v=-1;
For(u,1,V+1)if(!vis[u]&&(v==-1||mincost[v]>mincost[u]))v=u;
if(v==-1)break;
vis[v]=true;
ans+=mincost[v];
For(u,1,V+1)mincost[u]=min(mincost[u],G[v][u]);
}
writeln(ans);
}
int main()
{
//#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
// #endif // ONLINE_JUDGE
int n,m,i,j,k,t;
while(read(V)&&V)
{
E=V*(V-1)/2;
while(E--)
{
int a,b,c;
read__(a,b,c);
G[a][b]=G[b][a]=c;
}
prim();
}
return 0;
}
Kruskal算法
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<string>
#include<queue>
#include<vector>
#include<map>
#include<cstdlib>
#include<set>
#include<stack>
#include<cstring>
using namespace std;
template<class T>inline T read(T&x)
{
char c;
while((c=getchar())<=32)if(c==EOF)return -1;
bool ok=false;
if(c=='-')ok=true,c=getchar();
for(x=0; c>32; c=getchar())
x=x*10+c-'0';
if(ok)x=-x;
return 1;
}
template<class T> inline T read_(T&x,T&y)
{
return read(x)!=-1&&read(y)!=-1;
}
template<class T> inline T read__(T&x,T&y,T&z)
{
return read(x)!=-1&&read(y)!=-1&&read(z)!=-1;
}
template<class T> inline void write(T x)
{
if(x<0)putchar('-'),x=-x;
if(x<10)putchar(x+'0');
else write(x/10),putchar(x%10+'0');
}
template<class T>inline void writeln(T x)
{
write(x);
putchar('\n');
}
//-------ZCC IO template------
const int maxn=101;
const double inf=999999999;
#define lson (rt<<1),L,M
#define rson (rt<<1|1),M+1,R
#define M ((L+R)>>1)
#define For(i,t,n) for(int i=(t);i<(n);i++)
typedef long long LL;
typedef double DB;
typedef pair<int,int> P;
#define bug printf("---\n");
#define mod 10007
struct edge
{
int from,to,cost;
edge(){}
edge(int a,int b,int c){from=a,to=b,cost=c;}
bool operator<(const edge&tmp)const{
return cost<tmp.cost;
}
}es[maxn*maxn];
int f[maxn];
void init(int n)
{
For(i,0,n+1)f[i]=i;
}
int find(int x)
{
return x==f[x]?x:(f[x]=find(f[x]));
}
void unio(int x,int y)
{
x=find(x);y=find(y);
if(x==y)return ;
f[x]=y;
}
bool same(int x,int y)
{
return find(x)==find(y);
}
int main()
{
//#ifndef ONLINE_JUDGE
//freopen("in.txt","r",stdin);
// #endif // ONLINE_JUDGE
int n,m,i,j,k,t;
int V,E;
while(read(V)&&V)
{
E=V*(V-1)/2;
for(i=0;i<E;i++)
{
int a,b,c;
read__(a,b,c);
es[i]=edge(a,b,c);
}
init(V);
sort(es,es+E);
int ans=0;
for(i=0;i<E;i++)
{
edge e=es[i];
if(!same(e.to,e.from))
{
ans+=e.cost;
unio(e.to,e.from);
}
}
writeln(ans);
}
return 0;
}
标签:
原文地址:http://blog.csdn.net/u013167299/article/details/44995233