Bad Cowtractors(最大生成樹)
阿新 • • 發佈:2018-06-30
found ins -m 普裏姆算法 卡爾 eal following void HR
Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
* Line 1: Two space-separated integers: N and M
* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3. 題目意思:為了防止雇主不給錢或者少給錢,安裝網絡的人想要是每一個谷堆之間連通網絡的成本最大,即生成一個最大生成樹。 \\\克魯斯卡爾算法
Description
Bessie has been hired to build a cheap internet network among Farmer John‘s N (2 <= N <= 1,000) barns that are conveniently numbered 1..N. FJ has already done some surveying, and found M (1 <= M <= 20,000) possible connection routes between pairs of barns. Each possible connection route has an associated cost C (1 <= C <= 100,000). Farmer John wants to spend the least amount on connecting the network; he doesn‘t even want to pay Bessie.Realizing Farmer John will not pay her, Bessie decides to do the worst job possible. She must decide on a set of connections to install so that (i) the total cost of these connections is as large as possible, (ii) all the barns are connected together (so that it is possible to reach any barn from any other barn via a path of installed connections), and (iii) so that there are no cycles among the connections (which Farmer John would easily be able to detect). Conditions (ii) and (iii) ensure that the final set of connections will look like a "tree".
Input
* Lines 2..M+1: Each line contains three space-separated integers A, B, and C that describe a connection route between barns A and B of cost C.
Output
* Line 1: A single integer, containing the price of the most expensive tree connecting all the barns. If it is not possible to connect all the barns, output -1.Sample Input
5 8 1 2 3 1 3 7 2 3 10 2 4 4 2 5 8 3 4 6 3 5 2 4 5 17
Sample Output
42
Hint
OUTPUT DETAILS:The most expensive tree has cost 17 + 8 + 10 + 7 = 42. It uses the following connections: 4 to 5, 2 to 5, 2 to 3, and 1 to 3. 題目意思:為了防止雇主不給錢或者少給錢,安裝網絡的人想要是每一個谷堆之間連通網絡的成本最大,即生成一個最大生成樹。 \\\克魯斯卡爾算法
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int n,m,sum; struct node { int start;///起點 int end;///終點 int power;///權值 } edge[20050]; int pre[20050]; int cmp(node a,node b) { return a.power<b.power;///按權值降序排列 // return a.power<b.power } int find(int x)///並查集找祖先 { int a;///循環法 a=x; while(pre[a]!=a) { a=pre[a]; } return a; } void merge(int x,int y,int n) { int fx =find(x); int fy =find(y); if(fx!=fy) { pre[fx]=fy; sum+=edge[n].power; } } int main() { int i,x,count; while(scanf("%d%d",&n,&m)!=EOF) { sum=0; count=0; for(i=1; i<=m; i++) { scanf("%d%d%d",&edge[i].start,&edge[i].end,&x); edge[i].power=x; //edge[i].power=-x; } for(i=1; i<=m; i++) ///並查集的初始化 { pre[i]=i; } sort(edge+1,edge+m+1,cmp); for(i=1; i<=m; i++) { merge(edge[i].start,edge[i].end,i); } for(i=1; i<=n; i++) { if(pre[i]==i) { count++; } } if(count==1) { printf("%d\n",sum); // printf("%d\n",-sum); } else { printf("-1\n"); } } return 0; }
///普裏姆算法
#include<stdio.h> #include<string.h> #define MAX 0x3f3f3f3f using namespace std; int logo[1010];///用0和1來表示是否被選擇過 int map1[1010][1010]; int dis[1010];///記錄任意一點到這一點的最近的距離 int n,m; int prim() { int i,j,now; int sum=0; for(i=1; i<=n; i++) ///初始化 { dis[i]=MAX; logo[i]=0; } for(i=1; i<=n; i++) { dis[i]=map1[1][i]; } dis[1]=0; logo[1]=1; for(i=1; i<n; i++) ///循環查找 { now=-MAX; int max1=-MAX; for(j=1; j<=n; j++) { if(logo[j]==0&&dis[j]>max1) { now=j; max1=dis[j]; } } if(now==-MAX)///防止不成圖 { break; } logo[now]=1; sum=sum+max1; for(j=1; j<=n; j++) ///填入新點後更新最小距離,到頂點集的距離 { if(logo[j]==0&&dis[j]<map1[now][j]) { dis[j]=map1[now][j]; } } } if(i<n) { printf("-1\n"); } else { printf("%d\n",sum); } } int main() { int i,j; int a,b,c; while(scanf("%d%d",&n,&m)!=EOF)///n是點數 { for(i=1; i<=n; i++) { for(j=i; j<=n; j++) { if(i==j) { map1[i][j]=map1[j][i]=0; } else { map1[i][j]=map1[j][i]=-MAX; } } } for(i=0; i<m; i++) { scanf("%d%d%d",&a,&b,&c); if(map1[a][b]<c)///防止出現重邊 { map1[a][b]=map1[b][a]=c; } } prim(); } return 0; }
Bad Cowtractors(最大生成樹)