Title Link: http://poj.org/problem?id=1287
Sample Input1 02 31 2 Panax Notoginseng2 1 -1 2 the3 71 2 +2 3 One3 1 71 3 52 3 the3 1 the1 2 +5 71 2 52 3 72 4 84 5 One3 5 Ten1 5 64 2 A0Sample Output0 - - -
Analysis: Minimal spanning tree pure template
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <cmath>5 using namespacestd;6 #defineN 1107 #defineMet (A, b) memset (A, B, sizeof (a))8 #defineINF 0x3f3f3f3f9 Ten One intN, M, Dist[n], maps[n][n], vis[n]; A - intPrim (ints) - { theVis[s] =1; - for(intI=1; i<=n; i++) - { -Dist[i] =Maps[s][i]; + } - intsum =0; + for(intI=1; i<n; i++) A { at intMin = INF, Index =-1; - for(intj=1; j<=n; J + +) - { - if(!vis[j] && min>Dist[j]) - { -Min =Dist[j]; inIndex =J; - to } + } -Sum + =Min; theVis[index] =1; * for(intj=1; j<=n; J + +) $ {Panax Notoginseng if(!vis[j] && dist[j]>Maps[index][j]) - { theDIST[J] =Maps[index][j]; + } A } the + } - returnsum; $ } $ - voidInit () - { the for(intI=0; i<=n; i++) - {Wuyi for(intj=0; j<=n; J + +) theMAPS[I][J] = (i==j)?0: INF; -Dist[i] =INF; WuVis[i] =0; - } About } $ - intMain () - { - while(SCANF ("%d", &N), N) A { + intu, V, W; the Init (); -scanf"%d", &m); $ for(intI=1; i<=m; i++) the { thescanf" %d%d%d", &u, &v, &W); theMAPS[U][V] = Maps[v][u] =min (Maps[u][v], W); the } - intAns = Prim (1); in theprintf"%d\n", ans); the } About return 0; the}
POJ-1287 Networking