2021-1-31 最小生成樹入門
A - 畅通工程
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
Input
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
Output
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
Sample Input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
Sample Output
3
?
題解:
標准的模板題,用prim、kruskal都可以。
#include <cstdio>#include <string>#include <cstring>#include <iostream>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;const int maxn=105;int fa[maxn];int n,m,u,v,w,sum;struct node{int x,y,z;}edge[4*maxn];bool cmp(node a,node b){return a.z<b.z;}int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]);}int main(){while(~scanf("%d %d",&n,&m)){sum=0;if(n==0)break;for(int i=1;i<=n;i ){scanf("%d %d %d",&edge[i].x,&edge[i].y,&edge[i].z);}for(int i=0;i<=m;i ) fa[i]=i;sort(edge 1,edge n 1,cmp);for(int i=1;i<=n;i ){int x=find(edge[i].x);int y=find(edge[i].y);if(x==y)continue;fa[x]=y;sum =edge[i].z;}int ans=0;for(int i=1;i<=m;i ){if(i==fa[i])ans ;}if(ans>1)cout<<"?"<<endl;else cout<<sum<<endl;}return 0;}
B - 畅通工程再续
相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现。现在政府决定大力发展百岛湖,发展首先要解决的问题当然是交通问题,政府决定实现百岛湖的全畅通!经过考察小组RPRush对百岛湖的情况充分了解后,决定在符合条件的小岛间建上桥,所谓符合条件,就是2个小岛之间的距离不能小于10米,也不能大于1000米。当然,为了节省资金,只要求实现任意2个小岛之间有路通即可。其中桥的价格为 100元/米。
Input
输入包括多组数据。输入首先包括一个整数T(T <= 200),代表有T组数据。
每组数据首先是一个整数C(C <= 100),代表小岛的个数,接下来是C组坐标,代表每个小岛的坐标,这些坐标都是 0 <= x, y <= 1000的整数。
Output
每组输入数据输出一行,代表建桥的最小花费,结果保留一位小数。如果无法实现工程以达到全部畅通,输出”oh!”.
Sample Input
2
2
10 10
20 20
3
1 1
2 2
1000 1000
Sample Output
1414.2
oh!
題解:
只要輸入坐標,然後利用循環,求出每個坐標和另外坐標的距離,就是道路距離,然後利用模板做就可以了。
很疑惑地一點是,數組開小會TLE
#include <cstdio>#include <string>#include <cstring>#include<cmath>#include <iostream>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;const int maxn=110;int fa[maxn];double x[maxn],y[maxn];int T,n;double sum;struct node{double x,y,z;}edge[6110];bool cmp(node a,node b){return a.z<b.z;}int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]);}int main(){scanf("%d",&T);while(T--){sum=0;scanf("%d",&n); if(n== 0 || n== 1) { printf("0.0\n"); continue; }for(int i=1;i<=n;i ) scanf("%lf %lf",&x[i],&y[i]);double d;int cnt=1;for(int i=1;i<=n;i ){for(int j=1;j<i;j ){d=sqrt(pow((x[i]-x[j]),2) pow((y[i]-y[j]),2));if(d>=10.0&&d<=1000.0){edge[cnt].x=i;edge[cnt].y=j;edge[cnt].z=d;cnt ;}}}for(int i=1;i<=n;i ) fa[i]=i;sort(edge 1,edge cnt 1,cmp);int ans=0;for(int i=1;i<=cnt-1;i ){int x=find(edge[i].x);int y=find(edge[i].y);if(x==y)continue;fa[x]=y;sum =edge[i].z;ans ;}sum*=100;if(ans!=n-1)printf("oh!\n");else printf("%.1lf\n",sum);}return 0;}
C題hdu-1879
只要把已經有連接的兩個點串到一個集合裏再用模板做就可以了。
E - The Unique MST
Given a connected undirected graph, tell if its minimum spanning tree is unique.
Definition 1 (Spanning Tree): Consider a connected, undirected graph G = (V, E). A spanning tree of G is a subgraph of G, say T = (V’, E’), with the following properties:
- V’ = V.
- T is connected and acyclic.
Definition 2 (Minimum Spanning Tree): Consider an edge-weighted, connected, undirected graph G = (V, E). The minimum spanning tree T = (V, E’) of G is the spanning tree that has the smallest total cost. The total cost of T means the sum of the weights on all the edges in E’.
Input
The first line contains a single integer t (1 <= t <= 20), the number of test cases. Each case represents a graph. It begins with a line containing two integers n and m (1 <= n <= 100), the number of nodes and edges. Each of the following m lines contains a triple (xi, yi, wi), indicating that xi and yi are connected by an edge with weight = wi. For any two nodes, there is at most one edge connecting them.
Output
For each input, if the MST is unique, print the total cost of it, or otherwise print the string ‘Not Unique!’.
Sample Input
2
3 3
1 2 1
2 3 2
3 1 3
4 4
1 2 2
2 3 2
3 4 2
4 1 2
Sample Output
3
Not Unique!
#include <cstdio>#include <string>#include <cstring>#include <iostream>#include <algorithm>#define INF 0x3f3f3f3fusing namespace std;const int maxn=105;int fa[maxn];int T,n,m,u,v,w,sum,sum2;struct node{int x,y,z;int equals;//是否存在相等邊int used;int del; }edge[20005];bool cmp(node a,node b){return a.z<b.z;}int find(int x){ return x==fa[x]?x:fa[x]=find(fa[x]);}int Kruskal(){for(int i=0;i<=n;i )fa[i]=i;int summ=0;for(int i=1;i<=m;i ){if(edge[i].del==1)continue;int x=find(edge[i].x);int y=find(edge[i].y);if(x==y)continue;fa[x]=y;summ =edge[i].z;}return summ;}int main(){ scanf("%d",&T);while(T--){sum=0;scanf("%d %d",&n,&m);for(int i=1;i<=m;i ){scanf("%d %d %d",&edge[i].x,&edge[i].y,&edge[i].z);edge[i].equals=0;edge[i].del=0;edge[i].used=0;}for(int i=1;i<=m;i ){for(int j=1;j<=m;j ){if(i==j)continue;if(edge[i].z==edge[j].z)edge[i].equals=1;}}for(int i=0;i<=n;i ) fa[i]=i;sort(edge 1,edge m 1,cmp);for(int i=1;i<=m;i ){int x=find(edge[i].x);int y=find(edge[i].y);if(x==y)continue;fa[x]=y;edge[i].used=1;sum =edge[i].z;}int f=0;for(int i=1;i<=m;i ){if(edge[i].used==1&&edge[i].equals==1){edge[i].del=1;sum2=Kruskal();if(sum==sum2){f=1;printf("Not Unique!\n");break;}edge[i].del=0;}} if(f==0) { cout<<sum<<endl;}}return 0;}