Title Description
Description
There are n cities in a country. A number of cities have a telephone line connection, now to increase the M-line (telephone line of course is two-way), so that any two cities directly or indirectly through other cities have telephone line connection, your program should be able to find the minimum cost and a connection scheme.
Enter a description
Input Description
The first line of the input file is the value of N (n<=100).
The second row to the N+1 line is a n*n matrix, the number of rows J of row I, if 0, indicates that city I has a telephone line connection with the city J, otherwise the connection cost between the two cities (range not exceeding 10000).
Output description
Output Description
The first behavior of the output file is the total number of phone lines you connect M, the second line to the m+1 behavior each phone line that you connect, the format is I J, (I<j), I J is the two cities connected by the telephone line. Output follow the prim algorithm to discover the sequential output of each edge, starting at 1.
Line m+2 is the total cost of connecting these telephone lines.
Sample input
Sample Input
5
0 15 27) 6 0
15 0 33) 19 11
27 33 0) 0 17
6 19 0) 0 9
0 11 17) 9 0
Sample output
Sample Output
2
1 4
2 5
17
Data range and Tips
Data Size & Hint
n<=100
Positive solution: Minimum spanning tree
Problem Solving Report:
Now can only brush a little water problem every day ... This problem is really proud, incredibly can only use prim ... Actually, I almost forgot what prim was.
1 //It's made by jump~2#include <iostream>3#include <cstdlib>4#include <cstring>5#include <cstdio>6#include <cmath>7#include <algorithm>8#include <ctime>9#include <vector>Ten#include <queue> One#include <map> A#include <Set> - #ifdef WIN32 - #defineOT "%i64d" the #else - #defineOT "%lld" - #endif - using namespacestd; +typedefLong LongLL; - Const intMAXN =111; + Const intINF = (1<< -); A intn,cnt; at intA[MAXN][MAXN]; - BOOLVIS[MAXN]; - intLOW[MAXN]; - intNEXT[MAXN]; - intans; - in structedge{ - ints,t; to }E[MAXN]; + -InlineintGetint () the { * intw=0, q=0; $ CharC=GetChar ();Panax Notoginseng while((c<'0'|| C>'9') && c!='-') c=GetChar (); - if(c=='-') q=1, c=GetChar (); the while(c>='0'&& c<='9') w=w*Ten+c-'0', c=GetChar (); + returnQ? -w:w; A } the +Inlinevoidsolve () { -n=getint (); $ for(intI=1; i<=n;i++) for(intj=1; j<=n;j++) a[i][j]=getint (); $ - for(intI=1; i<=n;i++) low[i]=inf; - thelow[1]=0; - Wuyi for(intI=1; i<=n;i++) { the intMinl=inf;intJilu; - for(intj=1; j<=n;j++)if(!vis[j] && minl>low[j]) {Minl=low[j]; jilu=J;} WuANS+=MINL; vis[jilu]=1; - for(intj=2; j<=n;j++)if(!vis[j] && a[jilu][j]<low[j]) {Low[j]=a[jilu][j]; next[j]=Jilu;} About if(MINL) {E[++cnt].s=jilu; e[cnt].t=Next[jilu];} $ } - -printf"%d\n", CNT); - A for(intI=1; i<=cnt;i++) { + if(e[i].s>e[i].t) Swap (e[i].s,e[i].t); theprintf"%d%d\n", e[i].s,e[i].t); - } $ theprintf"%d", ans); the } the the intMain () - { in solve (); the return 0; the}
Codevs1003 Telephone Connection