次小生成树 (poj 1679)
次小生成树
建立在最小生成树的基础上;
由于最小生成树的性质所决定,对于最小生成树任何的两点之间再加一条边,就会形成一个环;
那么,也就是说,加上一条边后,减去一条除刚加上的一条边外权值最大的边,又会构成一棵生成树;
这样,将所有最小生成树上没有的边按上述遍历一次后,得到生成树的最小的权值即为该图的次小生成树。
其中,最大的权值可由prime求最小生成树时顺便求出:
a[x][j]=max{a[x][i], ver[i][j]};
(其中,x为当前在树上的节点,j为当前要入树的点,i为j的父节点)。
代码:
View Code
1 #include<iostream> 2 #include<string.h> 3 using namespace std; 4 #define INF 1000000000 5 int first[105]; 6 struct node{ 7 int start; 8 int end; 9 int w; 10 int next; 11 }; 12 node ver[100005]; 13 int visver[100005]; 14 int a[105]; 15 int vispoint[105]; 16 int secondmax[105][105]; 17 int main(){ 18 int n1; 19 cin>>n1; 20 int n,m; 21 while(n1--){ 22 cin>>n>>m; 23 memset(first,-1,sizeof(first)); 24 memset(ver,-1,sizeof(ver)); 25 memset(visver,-1,sizeof(visver)); 26 memset(vispoint,-1,sizeof(vispoint)); 27 memset(secondmax,-1,sizeof(secondmax)); 28 for(int i=1;i<=m;i++){ 29 int temp1,temp2,temp3; 30 cin>>temp1>>temp2>>temp3; 31 if(first[temp1]==-1){ 32 first[temp1]=i; 33 } 34 else{ 35 int temp=first[temp1]; 36 first[temp1]=i; 37 ver[i].next=temp; 38 } 39 ver[i].start=temp1; 40 ver[i].end=temp2; 41 ver[i].w=temp3; 42 if(first[temp2]==-1){ 43 first[temp2]=i+m; 44 } 45 else{ 46 int temp=first[temp2]; 47 first[temp2]=i+m; 48 ver[i+m].next=temp; 49 } 50 ver[i+m].start=temp2; 51 ver[i+m].end=temp1; 52 ver[i+m].w=temp3; 53 } 54 int times=0; 55 int start=1; 56 a[times++]=start; 57 vispoint[start]=1; 58 int sum=0; 59 int flag=0; 60 for(int j=1;j<n;j++){ 61 int minn=INF; 62 int p=0; 63 for(int k=0;k<times;k++){ 64 for(int g=first[a[k]];g!=-1;g=ver[g].next){ 65 if(ver[g].w<minn&&vispoint[ver[g].end]==-1){ 66 minn=ver[g].w; 67 p=g; 68 } 69 } 70 } 71 if(minn>=INF){ 72 flag=1; 73 break; 74 } 75 sum+=minn; 76 int second=ver[p].w; 77 for(int g=0;g<times;g++){ 78 int temp=second>secondmax[ver[p].start][a[g]]?second:secondmax[ver[p].start][a[g]]; 79 secondmax[ver[p].end][a[g]]=temp; 80 secondmax[a[g]][ver[p].end]=temp; 81 } 82 visver[p]=1; 83 visver[p+m]=1; 84 if(vispoint[ver[p].end]==-1){ 85 a[times++]=ver[p].end; 86 vispoint[ver[p].end]=1; 87 } 88 } 89 if(flag==1){ 90 cout<<"unexist SecondMinTree"<<endl; 91 continue; 92 } 93 cout<<sum<<endl; 94 int sumsec=INF; 95 for(int j=1;j<=m;j++){ 96 if(visver[j]==-1){ 97 if(secondmax[ver[j].start][ver[j].end]==-1) 98 continue; 99 int tempsum=sum-secondmax[ver[j].start][ver[j].end]; 100 tempsum+=ver[j].w; 101 if(tempsum<sumsec) 102 sumsec=tempsum; 103 } 104 } 105 if(sumsec>=INF){ 106 cout<<"inexist SecondMinTree"<<endl; 107 continue; 108 } 109 cout<<sumsec<<endl; 110 } 111 return 0; 112 } 113
poj 1679
The Unique MST
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 15700 | Accepted: 5427 |
Description
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: 1. V' = V. 2. 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'.
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: 1. V' = V. 2. 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!
题意:
图中是否存在相等的最小生成树;有则输出“not Unique”,无则输出最小生成树的值。
View Code
1 #include<iostream> 2 #include<string.h> 3 using namespace std; 4 #define INF 1000000000 5 int first[105]; 6 struct node{ 7 int start; 8 int end; 9 int w; 10 int next; 11 }; 12 node ver[100005]; 13 int visver[100005]; 14 int a[105]; 15 int vispoint[105]; 16 int secondmax[105][105]; 17 int main(){ 18 int n1; 19 cin>>n1; 20 int n,m; 21 while(n1--){ 22 cin>>n>>m; 23 memset(first,-1,sizeof(first)); 24 memset(ver,-1,sizeof(ver)); 25 memset(visver,-1,sizeof(visver)); 26 memset(vispoint,-1,sizeof(vispoint)); 27 memset(secondmax,-1,sizeof(secondmax)); 28 for(int i=1;i<=m;i++){ 29 int temp1,temp2,temp3; 30 cin>>temp1>>temp2>>temp3; 31 if(first[temp1]==-1){ 32 first[temp1]=i; 33 } 34 else{ 35 int temp=first[temp1]; 36 first[temp1]=i; 37 ver[i].next=temp; 38 } 39 ver[i].start=temp1; 40 ver[i].end=temp2; 41 ver[i].w=temp3; 42 if(first[temp2]==-1){ 43 first[temp2]=i+m; 44 } 45 else{ 46 int temp=first[temp2]; 47 first[temp2]=i+m; 48 ver[i+m].next=temp; 49 } 50 ver[i+m].start=temp2; 51 ver[i+m].end=temp1; 52 ver[i+m].w=temp3; 53 } 54 int times=0; 55 int start=1; 56 a[times++]=start; 57 vispoint[start]=1; 58 int sum=0; 59 int flag=0; 60 for(int j=1;j<n;j++){ 61 int minn=INF; 62 int p=0; 63 for(int k=0;k<times;k++){ 64 for(int g=first[a[k]];g!=-1;g=ver[g].next){ 65 if(ver[g].w<minn&&vispoint[ver[g].end]==-1){ 66 minn=ver[g].w; 67 p=g; 68 } 69 } 70 } 71 if(minn>=INF){ 72 flag=1; 73 break; 74 } 75 sum+=minn; 76 int second=ver[p].w; 77 // cout<<p<<" "<<ver[p].start<<" "<<ver[p].end<<" "<<ver[p].w<<endl; 78 for(int g=0;g<times;g++){ 79 int temp=second>secondmax[ver[p].start][a[g]]?second:secondmax[ver[p].start][a[g]]; 80 // cout<<ver[p].end<<" "<<a[times]<<endl; 81 secondmax[ver[p].end][a[g]]=temp; 82 secondmax[a[g]][ver[p].end]=temp; 83 // cout<<temp<<" "<<secondmax[ver[p].start][a[times]]<<endl; 84 } 85 visver[p]=1; 86 visver[p+m]=1; 87 if(vispoint[ver[p].end]==-1){ 88 a[times++]=ver[p].end; 89 vispoint[ver[p].end]=1; 90 } 91 } 92 if(flag==1){ 93 cout<<"0"<<endl; 94 // cout<<"unexist SecondMinTree"<<endl; 95 continue; 96 } 97 // cout<<sum<<endl; 98 /* for(int i=1;i<=n;i++){ 99 for(int k=1;k<=n;k++){ 100 cout<<i<<" "<<k<<" "<<secondmax[i][k]<<endl; 101 } 102 }*/ 103 int sumsec=INF; 104 for(int j=1;j<=m;j++){ 105 if(visver[j]==-1){ 106 if(secondmax[ver[j].start][ver[j].end]==-1) 107 continue; 108 int tempsum=sum-secondmax[ver[j].start][ver[j].end]; 109 tempsum+=ver[j].w; 110 if(tempsum<sumsec) 111 sumsec=tempsum; 112 } 113 } 114 /* if(sumsec>=INF){ 115 cout<<"inexist SecondMinTree"<<endl; 116 continue; 117 }*/ 118 if(sum==sumsec){ 119 cout<<"Not Unique!"<<endl; 120 } 121 else 122 cout<<sum<<endl; 123 } 124 return 0; 125 } 126

浙公网安备 33010602011771号