Test instructions: give you a picture with the right direction, let you ask for the maximum tree shape chart. And on this premise ordered N-junction father's number is the smallest.
The game was a two-point, tle.
The weight of each side can actually be multiplied by 1000, for the parent edge of the n node, plus the weight of the (999-parent node number) size, so that the maximum tree is guaranteed, the number of the father of the N node is the smallest.
Online to find a Zhu-Liu algorithm of the board , the edge of the right to take negative can run the largest tree diagram.
#include <cstdio> #include <string> #include <cstring> #define MAXN 1005 #define MAXM 10005 using n Amespace std; struct node {int u, v; int W; }EDGE[MAXM]; int PRE[MAXN], ID[MAXN], VIS[MAXN], N, m; int IN[MAXN]; typedef int type; #define INF 2000000000type directed_mst (int root, int V, int E) {type ret = 0; while (true) {//1.????? for (int i = 0; i < V; i++) in[i] = INF; for (int i = 0; i < E; i++) {int u = edge[i].u; int v = EDGE[I].V; if (EDGE[I].W < in[v] && u! = V) {Pre[v] = u; in[v] = EDGE[I].W;} } for (int i = 0; i < V; i++) {if (i = = root) continue; if (in[i] = = INF) return-1;//???????????,??????? }//2.?? int cnt = 0; memset (ID,-1, sizeof (ID)); memset (Vis,-1, sizeof (VIS)); In[root] = 0; for (int i = 0; i < V; i++)//????? {ret + = In[i]; int v = i; while (vis[v]! = i && id[v] = = 1 && v! = root)//?????????,?????????,??????? {Vis[v] = i; v = pre[v]; } if (v! = root && id[v] = =-1)//?? {for (int u = pre[v]; u = v; u = pre[u]) id[u] = cnt; ID[V] = cnt++; }} if (cnt = = 0) break; //?? break for (int i = 0; i < V; i++) if (id[i] = = 1) id[i] = cnt++; 3.???? for (int i = 0; i < E; i++) {int u = edge[i].u; int v = EDGE[I].V; EDGE[I].U = Id[u]; EDGE[I].V = Id[v]; if (id[u]! = Id[v]) EDGE[I].W-= In[v]; } V = cnt; root = Id[root]; } return ret; } int Zu,xs[10005],ys[10005],zs[10005];int Main () {//freopen ("1009.in", "R",stdin);//freopen ("1009.out", "w", stdout); scanf ("%d", &zu); for (; zu;--zu) {scanf ("%d%d", &n, &m); for (int i = 0; i < m; i++) {scanf ("%d%d%d", &edge[i].u, &EDGE[I].V, &EDGE[I].W); edge[i].u--; edge[i].v--; edge[i].w*=1000; if (edge[i].v==n-1) {edge[i].w+= (999-EDGE[I].U); } EDGE[I].W=-EDGE[I].W; xs[i]=edge[i].u; YS[I]=EDGE[I].V; ZS[I]=EDGE[I].W; if (edge[i].u = = edge[i].v) {EDGE[I].W = INF; Zs[i] = INF; }} int ans = directed_mst (0, N, m); printf ("%d%d\n", (-ans)/1000,1000-(-ans)%1000); } return 0; }
"Chu-Liu Algorithm" "Minimum tree diagram" hdu6141 I am your father!