Question link: http://acm.timus.ru/problem.aspx? Space = 1 & num = 1416
Zaphod Beeblebrox-president of the imperial Galactic Government. and by chance he is an owner of specified ISES that trade in secondhand pens. this is a complicated highly protable and highly competitive business. if you want to stay a leader you are to minimize your expenses all the time. and the Presedent's high post helps in those aairs. but he is to keep this business in secret. as a President Zaphod has access to the top secret and important information an exact value of power loss in the hyperspace transition between the planets. of course, this information is very useful to his company. zaphod is to choose the minimal possible set of trans-planet passages so that he cocould pass from any planet to any other one via those passages and their total cost wowould be minimal. the task won't be complicated if Zaphod was not to keep in secret that he helps his company with the secret information. thus, Zaphod decided to find not the cheapest passages set but the next one. as a real businessman he wants to estimate the value of his conspiracy expenses. inputthe first input line contains two integers:
N(2 ≤
N≤ 500) is a number of planets in the galaxy and
MIs an amount of possible transitions. The next
MLines contain three Integers
AI,
BiThe numbers of the planets that are connected with some passage (1 ≤
AI,
Bi≤
N), And
Wi(0 ≤
Wi≤ 1000) is the transition cost. If
ATo
BTransition is possible then
BTo
ATransition is possible, too. the cost of those transitions are equal. there is not more than one passage between any two planets. one can reach any planet from any other planet via some chain of these passages. outputyou shoshould find two different sets of transitions with the minimal possible cost and output theirs costs. print the minimal possible cost first. if any of those sets of transitions does not exist denote it's cost by −1.
Give an undirected graph with m edges at N points, and find the Minimum Spanning Tree and secondary spanning tree (the graph has no duplicate edges and seems to be a connected graph ).
Idea: Refer to Wang Ting's IOI Training Team's paper "Expansion of the Minimum Spanning Tree problem" in 2014. The time complexity is O (n ^ 2 ).
Code (0.109 ms ):
1 #include <cstdio> 2 #include <algorithm> 3 #include <cstring> 4 #include <iostream> 5 using namespace std; 6 7 const int MAXV = 510; 8 const int MAXE = MAXV * MAXV; 9 const int INF = 0x3f3f3f3f;10 11 int head[MAXV], ecnt;12 int to[MAXE], next[MAXE], cost[MAXE];13 bool select[MAXE];14 int n, m;15 16 void init() {17 memset(head + 1, -1, n * sizeof(int));18 ecnt = 0;19 }20 21 void add_edge(int u, int v, int c) {22 to[ecnt] = v; cost[ecnt] = c; next[ecnt] = head[u]; head[u] = ecnt++;23 to[ecnt] = u; cost[ecnt] = c; next[ecnt] = head[v]; head[v] = ecnt++;24 }25 26 int dis[MAXV], pre[MAXV];27 int maxPath[MAXV][MAXV];28 bool vis[MAXV];29 30 int prim() {31 memset(dis + 1, 0x3f, n * sizeof(int));32 dis[1] = 0;33 int res = 0;34 for(int _ = 0; _ < n; ++_) {35 int u = -1;36 for(int i = 1; i <= n; ++i) if(!vis[i] && dis[i] < INF)37 if(u == -1 || dis[i] < dis[u]) u = i;38 if(u == -1) return -1;39 for(int p = head[u]; ~p; p = next[p]) {40 int &v = to[p];41 if(vis[v]) continue;42 if(cost[p] < dis[v]) dis[v] = cost[p], pre[v] = p;43 }44 for(int i = 1; i <= n; ++i) if(vis[i]) {45 int &v = to[pre[u] ^ 1];46 maxPath[i][u] = maxPath[u][i] = max(maxPath[i][v], dis[u]);47 }48 res += dis[u];49 vis[u] = true;50 if(u != 1) select[pre[u]] = select[pre[u] ^ 1] = true;51 }52 return res;53 }54 55 int solve(int ans) {56 int res = -1;57 for(int u = 1; u <= n; ++u) {58 for(int p = head[u]; ~p; p = next[p]) {59 int &v = to[p];60 if(select[p] || u == v) continue;61 if(res == -1 || ans - maxPath[u][v] + cost[p] < res)62 res = ans - maxPath[u][v] + cost[p];63 }64 }65 return res;66 }67 68 int main() {69 scanf("%d%d", &n, &m);70 init();71 for(int i = 0, a, b, c; i < m; ++i) {72 scanf("%d%d%d", &a, &b, &c);73 add_edge(a, b, c);74 }75 int ans1 = prim(), ans2 = -1;76 if(ans1 != -1) ans2 = solve(ans1);77 printf("Cost: %d\n", ans1);78 printf("Cost: %d\n", ans2);79 }
View code