Test Instructions:Link
Method:Mst
parsing:The first two days just finished not know how the matter did not send up, so had to rewrite. This problem has been a long time for me. The first idea is DP, and later found that the W array is not monotonous, so want to sort out after the DP, but no eggs, because there is also the right edge, W minimum edge may be great, so this idea quickly d dropped. The second idea is the network flow, for the moment, do not talk about the complexity of the problem, we have to consider the Chinese-style network flow! How does it flow, split, each point two, and then the flow of 1 of the edge, and then the traffic is 1 words, he after the point may have a lot, but is limited to 1, even if we give him the INF flow, then will be recalculated many times W, so also wrong, that if we have the INF run after the O (n) Delete the traffic that should not have? No, because after the deletion is not guaranteed to be the minimum cost, so this idea is D. Third, there is really no way to re-examine the topic, each point has several cases? Two, the first is to give themselves water, the second is someone to give him irrigation. Others give him the situation can be directly connected to the side, but how to do? With the idea of network flow is not afraid of, set up a source point, to each point with a weight of w[i] side, so how to think? We have to take all the points, including the source point, but also the least cost, this is not the MST, the correctness of the obvious Ah, so the MST solution is good. The other side of the n^2, if the kruscal words o (eloge) is available, but will be slower, anyway can be done. The superiority of prim is reflected in this dense map. However, I am writing a kruscal.
Code:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define N 310using namespace STD;intNintFa[n];structnode{intFrom,to,val;} l[n*n*2];intFindintx) {if(fa[x]!=x)returnFa[x]=find (Fa[x]);returnx;}intCMP (node A,node b) {returnA.val<b.val;}intTotintMain () {scanf("%d", &n); for(intI=1; i<=n;i++) {intXscanf("%d", &x); L[++tot].from=1, l[tot].to=i+1, L[tot].val=x; Fa[i]=i; } fa[n+1]=n+1; for(intI=1; i<=n;i++) { for(intj=1; j<=n;j++) {intXscanf("%d", &x);if(I==J)Continue; l[++tot].from=i+1, l[tot].to=j+1, L[tot].val=x; }} sort (L +1, l+tot+1, CMP);intans=0, cntchoose=0; for(intI=1; i<=tot;i++) {intx=l[i].from,y=l[i].to;intFx=find (x), Fy=find (y);if(FX!=FY) {fa[fx]=fy; Ans+=l[i].val; cntchoose++;if(cntchoose==n) Break; } }printf("%d\n", ans);}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Bzoj 1601 [Usaco2008 Oct] Watering MST