The main topic: Give a picture, see can form a few spanning tree
Problem-solving ideas: First to determine whether the formation of a tree, in determining if there are sub-niche into a tree
#include <cstdio>#include <cstring>#include <vector>using namespace STD;#define N#define M 410#define INF 0x3f3f3f3fstructedge{intFrom, to, cost, next;} E[M];intHead[n], D[n], maxcost[n][n], id[m], f[n];intTot, N, m;BOOLMark[n], vis[m];voidAddedge (intUintVintc) {e[tot].from = u; E[tot].to = v; E[tot].cost = C; E[tot].next = Head[u]; Head[u] = tot++; u = u ^ v; v = u ^ v; u = u ^ v; E[tot].from = u; E[tot].to = v; E[tot].cost = C; E[tot].next = Head[u]; Head[u] = tot++;}voidInit () {memset(Head,-1,sizeof(head)); tot =0;scanf("%d%d", &n, &m);intU, V, c; for(inti =0; I < m; i++) {scanf("%d%d%d", &u, &v, &c); Addedge (U, V, c); }}intPrim () { for(inti =1; I <= N; i++) D[i] = INF;memset(Vis,0,sizeof(VIS));memset(Mark,0,sizeof(Mark)); d[1] =0; f[1] =1; vector<int>VintAns =0; for(inti =1; I <= N; i++) {intt = INF, x; for(intj =1; J <= N; J + +)if(!mark[j] && d[j] < T) T = D[x = j];if(t = = INF) {return-1; } Mark[x] =true;if(X! =1) {Vis[id[x]] = vis[id[x] ^1] =true; Ans + = e[id[x]].cost; }intSize = V.size (); for(intj =0; J < size; J + +) maxcost[v[j]][x] = Maxcost[x][v[j]] = max (Maxcost[v[j]][f[x]], e[id[x]].cost); V.push_back (x); for(intj = Head[x]; ~j; j = e[j].next) {intv = e[j].to;if(!mark[v] && e[j].cost < d[v]) {D[v] = E[j].cost; ID[V] = j; F[V] = x; } } }returnAns;}intCAS =1;voidSolve () {printf("Case #%d:", cas++);intans = Prim ();if(ans = =-1) {printf("No way\n");return; }BOOLFlag =false;intAns2 = INF, u, v; for(inti =0; i < tot; i + =2) {if(!vis[i]) {u = e[i].from; v = e[i].to; ans2 = min (ans2, ans-maxcost[u][v] + e[i].cost); } }if(Ans2 = = INF)printf("No second way\n");Else printf("%d\n", ans2);}intMain () {intTestscanf("%d", &test); while(test--) {init (); Solve (); }return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
UVA-10462 is there A Second to the left? (Sub-niche into a tree)