G - 最小生成树模板(适合Kruskal)
You are assigned to design network connections between certain points in a wide area. You are given a set of points in the area, and a set of possible routes for the cables that may connect pairs of points. For each possible route between two points, you are given the length of the cable that is needed to connect the points over that route. Note that there may exist many possible routes between two given points. It is assumed that the given possible routes connect (directly or indirectly) each two points in the area. Your task is to design the network for the area, so that there is a connection (direct or indirect) between every two points (i.e., all the points are interconnected, but not necessarily by a direct cable), and that the total length of the used cable is minimal. Input The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains two integers: the first defines the number P of the given points, and the second the number R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers: the first two numbers identify the points, and the third gives the length of the route. The numbers are separated with white spaces. A data set giving only one number P = 0 denotes the end of the input. The data sets are separated with an empty line. The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes are identified with integers between 1 and P (inclusive). The routes between two points i and j may be given as ‘i j’ or as ‘j i’. Output For each data set, print one number on a separate line that gives the total length of the cable used for the entire designed network. Sample Input 1 0 2 3 1 2 37 2 1 17 1 2 68 3 7 1 2 19 2 3 11 3 1 7 1 3 5 2 3 89 3 1 91 1 2 32 5 7 1 2 5 2 3 7 2 4 8 4 5 11 3 5 10 1 5 6 4 2 12 0 Sample Output 0 17 16 26
题意:n个点,m条边,找出最小生成树,输出最小生成树边权值之和
裸的最小生成树题,prim和Kruskal都可以。
Kruskal:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <vector> #define Twhile() int T;scanf("%d",&T);while(T--) #define ArrInit(a,b,n) for(int i=0;i<=n;i++)a[i]=b #define ArrInit2(a,b,n,m) for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)a[i][j]=b #define fora(i,a,b) for(int i=a;i<b;i++) #define fors(i,a,b) for(int i=a;i>b;i--) #define fora2(i,a,b) for(int i=a;i<=b;i++) #define fors2(i,a,b) for(int i=a;i>=b;i--) #define PI acos(-1.0) #define eps 1e-6 #define INF 0x3f3f3f3f typedef long long LL; typedef long long LD; using namespace std; const int maxn=5000+11; int N,M,ans;//点,边 struct edge { int u,v;//点u到v int var;//权值 }ma[maxn]; bool cmp(edge a,edge b) { return a.var<b.var; } int fa[maxn]; void initKruskal() { ans=0; fora2(i,1,N) { fa[i]=i; } sort(ma+1,ma+M+1,cmp); } /* Kruskal算法 (1) 将边按权值从小到大排序后逐个判断,如果当前的边加入以后不会产生环,那么就把当前边作为生成树的一条边 (2) 最终得到的结果就是最小生成树 并查集 */ int findx(int x) { if(x==fa[x])return x; return fa[x]=findx(fa[x]); } bool unio(int x,int y) { int fx=findx(x),fy=findx(y); if(fx==fy)return false; fa[fy]=fx; return true; } void kruskal() { initKruskal(); int m=0; fora2(i,1,M) { if(unio(ma[i].u,ma[i].v)) { m++; ans+=ma[i].var; //printf("%d %d %d\n",ma[i].u,ma[i].v,m); } if(m==N-1)return; } } int main() { while(~scanf("%d",&N)&&N) { scanf("%d",&M); int k=1; while(k<=M) { int x,y,z; scanf("%d%d%d",&x,&y,&z); ma[k].u=x; ma[k].v=y; ma[k].var=z; k++; } if(N==1){printf("0\n");continue;} kruskal(); printf("%d\n",ans); } return 0; }
prim:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <cmath> #include <algorithm> #include <queue> #include <map> #include <set> #include <stack> #include <vector> #define Twhile() int T;scanf("%d",&T);while(T--) #define ArrInit(a,b,n) for(int i=0;i<=n;i++)a[i]=b #define ArrInit2(a,b,n,m) for(int i=0;i<=n;i++)for(int j=0;j<=m;j++)a[i][j]=b #define fora(i,a,b) for(int i=a;i<b;i++) #define fors(i,a,b) for(int i=a;i>b;i--) #define fora2(i,a,b) for(int i=a;i<=b;i++) #define fors2(i,a,b) for(int i=a;i>=b;i--) #define PI acos(-1.0) #define eps 1e-6 #define INF 0x3f3f3f3f typedef long long LL; typedef long long LD; using namespace std; const int maxn=2000+11; int ma[maxn][maxn];//邻接矩阵 int N,M;//点数,边数 int ans;//最小生成树的权值之和 //stack<int>S; //最小生成树加入的点的顺序 //int lowerPoint[maxn]; // i离S中的lowerPoint[i]最近 int lowerCost[maxn],use[maxn];//lowerCost离S的最小距离,use是否被加入S void init() { ans=0; ArrInit2(ma,INF,N,N); ArrInit(use,0,N); } void primInit() { // S.push(1); use[1]=1; lowerCost[1]=0; // lowerPoint[1]=1; fora2(i,2,N) { // lowerPoint[i]=1; lowerCost[i]=ma[1][i]; } } void prim() { //printf("最小生成树:\n"); primInit(); int n=N-1; while(n--){ int k=-1,tem=INF; fora2(i,2,N)//找到距离S集合最小的点 { if(use[i])continue; if(lowerCost[i]<tem) { tem=lowerCost[i]; k=i; } } use[k]=1; ans+=tem; //printf("点%d到点%d,边权值:%d",k,lowerCost[k],tem); /* lowerCost[k]=0;lowerPoint[i]=i; //不更新的话,当要求输出最小生成树的边可以直接输出 S.push(k); */ fora2(i,1,N) { if(use[i])continue; int t=ma[i][k]; if(t<lowerCost[i]) { lowerCost[i]=t; //lowerPoint[i]=k; } } if(tem==INF)return;//图不连通 } } int main() { while(~scanf("%d",&N)&&N) { scanf("%d",&M); init(); while(M--) { int x,y,z; scanf("%d%d%d",&x,&y,&z); ma[x][y]=min(ma[x][y],z); ma[y][x]=min(ma[y][x],z); } if(N==1){printf("0\n");continue;} prim(); printf("%d\n",ans); } return 0; }