ZOJ 3396 Conference Call: calculates the minimum spanning tree after a specific 3 point

Source: Internet
Author: User

[Cpp]
// ZOJ 3396 Conference Call
// Obtain the minimum spanning tree of a specific three-point question.
// Train of thought: enumeration of any point as a support point. to connect a specific three points, you must go through a common point. Find the shortest path from these three points to the enumerated point, and take the minimum value as the answer.
# Include <iostream>
# Include <stdio. h>
# Include <string. h>
# Include <queue>
Using namespace std;
 
# Define INF 50000000
# Define N 20005
# Define M 505
Int n, m, k;
 
Int sta [N];
Int head [M], num;
Int dis [4] [M];
Bool vis [M], mark [M];
 
Struct Edge {
Int from;
Int;
Int val;
Int next;
} Edge [N];
 
Void addedge (int from, int to, int val ){
Edge [num]. from = from;
Edge [num]. to =;
Edge [num]. val = val;
Edge [num]. next = head [from];
Head [from] = num ++;
}
 
Void init (){
Memset (head,-1, sizeof (head ));
Num = 0;
}
 
Void spfa (int s, int index ){
Memset (vis, 0, sizeof (vis ));
For (int I = 1; I <= m; ++ I)
Dis [index] [I] = INF;
Queue <int> Q;
Q. push (s );
Dis [index] [s] = 0;
Vis [s] = 1;
While (! Q. empty ()){
Int p = Q. front ();
Q. pop ();
Vis [p] = 0;
For (int I = head [p]; I! =-1; I = edge [I]. next ){
If (dis [index] [edge [I]. to]> dis [index] [p] + edge [I]. val ){
Dis [index] [edge [I]. to] = dis [index] [p] + edge [I]. val;
If (! Vis [edge [I]. to]) {
Q. push (edge [I]. );
Vis [edge [I]. to] = 1;
}
}
}
}
}
 
Int main (){
Int I, j, ca = 1;
Int a, B, c;
While (scanf ("% d", & n, & m, & k )! = EOF ){
Init ();
For (I = 1; I <= n; ++ I)
Scanf ("% d", & sta [I]);
For (I = 1; I <= k; ++ I ){
Scanf ("% d", & a, & B, & c );
Addedge (a, B, c );
Addedge (B, a, c );
}
Int bx;
Int qua;
Scanf ("% d", & qua );
Printf ("Case # % d \ n", ca ++ );
For (I = 1; I <= qua; ++ I ){
Scanf ("% d", & a, & B, & c );
Spfa (sta [a], 1 );
Spfa (sta [B], 2 );
Spfa (sta [c], 3 );
Int ans = INF;
For (j = 1; j <= m; ++ j)
If (dis [1] [j] + dis [2] [j] + dis [3] [j] <ans ){
Ans = dis [1] [j] + dis [2] [j] + dis [3] [j];
Bx = j;
}
Printf ("Line % d:", I );
// Printf ("bx: % d \ n", bx, dis [1] [bx], dis [2] [bx], dis [3] [bx]);
If (ans = INF)
Printf ("Impossible to connect! \ N ");
Else
Printf ("The minimum cost for this line is % d. \ n", ans );
}
}
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.