P1340 animal Path Management and p1340 Management
Description
The herd of John farm wanted to move freely between N (1 <= N <= 200) lawns. The number of lawns ranges from 1 to N. The grass is separated by forests. The herd wants to select the path between lawns so that the herd can move from any lawn to any other lawn. The herd can pass through the path in two directions.
The herd cannot create paths, but they will keep and take advantage of the paths (hereinafter referred to as animal paths) that have been found by the beasts ). Each week, they choose and manage some or all known animal paths as pathways.
A new animal path will be found at the beginning of each week. They then had to decide which animal paths they managed to form the path through which the herd moved from any grass to any grass this week. The herd can only use the animal paths managed by the week as the path.
The herd wants the shortest diameter they manage to be the minimum. The herd can choose from all the animal paths they know to manage them. The paths that a herd can choose are irrelevant to whether it was previously managed.
Animal paths are never straight lines, so different animal paths connecting two lawns can have different lengths. In addition, although the two animal paths may overlap, the herd is very focused, unless the intersection is in the grass, it will not be switched to another animal path.
At the beginning of each week, the herd will describe their newly discovered animal paths. If possible, find a group of animal paths that need to be managed from any grassland to minimize the length of the animal paths.
Input/Output Format
Input Format:
The first line contains two integers N and W separated by spaces. W represents the number of weeks that your program needs to process. (1 <= W <= 6000 ).
The following data is read every week, representing the newly discovered animal path of the week. The three integers separated by spaces represent the two endpoints of the animal path (the numbers of two lawns) length (1... 10000 ). The two endpoints of a trail must be different.
Output Format:
Each time you read the newly discovered animal paths, your program must immediately output the length and number of animal paths. This group of animal paths can access the other grass from any grass, and minimize the length of the animal path. If you cannot find a group of animals that can access the other grassland from any grassland, then "-1" is output ".
Input and Output sample input sample #1:
4 6 1 2 10 1 3 8 3 2 3 1 4 3 1 3 6 2 1 2
Output sample #1:
-1 // No trail connects 4 to the rest of the fields. -1 // No trail connects 4 to the rest of the fields. -1 // No trail connects 4 to the rest of the fields.14 // Maintain 1 4 3, 1 3 8, and 3 2 3.12 // Maintain 1 4 3, 1 3 6, and 3 2 3.8 // Maintain 1 4 3, 2 1 2, and 3 2 3. // program exit
We don't have to perform sort every time,
We only need to store the order of appearance of each record,
Run the Minimum Spanning Tree.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<cmath> 5 #include<algorithm> 6 using namespace std; 7 const int MAXN=6001; 8 void read(int & n) 9 {10 char c='+';int x=0;bool flag=0;11 while(c<'0'||c>'9')12 {c=getchar();if(c=='-')flag=1;}13 while(c>='0'&&c<='9')14 {x=x*10+(c-48),c=getchar();}15 flag==1?n=-x:n=x;16 }17 int n,m;18 struct node19 {20 int u,v,w,happen;21 }edge[MAXN];22 int num=1;23 void add_edge(int x,int y,int z)24 {25 edge[num].u=x;26 edge[num].v=y;27 edge[num].w=z;28 edge[num].happen=num;29 num++;30 }31 int comp(const node & a,const node & b)32 {33 return a.w<b.w;34 }35 int fa[MAXN];36 int find(int x)37 {38 if(fa[x]==x)39 return fa[x];40 return fa[x]=find(fa[x]);41 }42 void unionn(int x,int y)43 {44 int fx=find(x);45 int fy=find(y);46 fa[fx]=fy;47 }48 int vis[MAXN];49 void kruskal(int p)50 {51 int ans=0;52 int k=0;53 54 for(int i=1;i<=n;i++)55 fa[i]=i;56 for(int i=1;i<=num-1;i++)57 {58 if(find(edge[i].u)!=find(edge[i].v)&&edge[i].happen<=p)59 {60 unionn(edge[i].u,edge[i].v);61 ans+=edge[i].w;62 k++;63 }64 if(k==n-1)65 {66 printf("%d\n",ans);67 return ;68 }69 }70 printf("-1\n"); 71 }72 int main()73 {74 read(n);read(m);75 for(int i=1;i<=n;i++)fa[i]=i;76 for(int i=1;i<=m;i++)77 {78 int x,y,z;79 read(x);read(y);read(z);80 add_edge(x,y,z);81 //add_edge(y,x,z);82 }83 sort(edge+1,edge+num,comp);84 for(int i=1;i<=m;i++)85 {86 kruskal(i);87 }88 89 return 0;90 }