Topic set Kuangbin with you fly six smallest spanning tree

Source: Internet
Author: User

Feel almost, the problem of BFS + minimum spanning tree, after I make up, feel the smallest generation tree, should have a certain understanding of it 、、、、

And whether it's prim or prime--#.
A-jungle Roads

Description
The Head Elder of the tropical island of Lagrishan have a problem. A burst of foreign aid money is spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly and so the large road network are too expensive to maintain. The Council of elders must choose to stop maintaining some roads. The map above on the left shows all the roads with use now and the cost in aacms per month to maintain them. Of course there needs to be some-to-get between all the villages on maintained roads, even if the route was not as Shor T as before. The chief Elder would like to tell the Council of the elders what would is the smallest amount they could spend in aacms per m Onth to maintain roads this would connect all the villages. The villages is labeled A through I in the maps above. The map on the right shows the roads that could is maintained most cheaply, for 216 aacms per month. Your task is to write a program that would solve such problems.

Input the input consists of one to the data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < N <, and the VI Llages is labeled with the first n letters of the alphabet, capitalized. Each data set was completed with n-1 lines this start with the village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, K, of roads from this village to villages with Labels later in the alphabet. If K is greater than 0, the line continues with data for each of the K roads. The data for each road was the village label for the other end of the road followed by the monthly maintenance cost in AACM s for the road. Maintenance costs is positive integers less than 100. All data fields in the row is separated by single blanks. The road network would always be allow to travel between all the villages. The network would NEVEr has more than roads. No Village'll has more than roads going to other villages (before or after the alphabet). In the sample input below, the first data set goes with the map above.

Output the output is an integer per line for each data set:the minimum cost of aacms per month to maintain a road system That's connect all the villages. Caution:a Brute force solution that examines every possible set of roads would not finish within the one minute time limit .

Sample Input

9
A 2 b i
B 3 C ten H 2 I 8
C 1 D 2 G f (
g)
f 0
G 1 h
H 1 I
3
A 2 b C +
B 1 C
0

Sample Output

216
30

At first thought that input trouble does not want to do, actually is quite simple 、、、

#include <stdio.h> #include <iostream> #include <algorithm> #include <map> #include <

String.h> using namespace std;
#define N #define MAX 0x3fffffff int tt[n][n];
int vis[n];
int dis[n];

int n, m;
    int prim () {int I, j, K;
    int ans = 0;
    int min, t;
    memset (Vis, 0, sizeof (VIS));
    for (i = 1; I <= n; i + +) {Dis[i] = Tt[1][i];
    } vis[1] = 1;
        for (i = 1; i < n; i + +) {min = Max;  for (j = 1; J <= N; j + +) {if (!vis[j] && dis[j] < min) {min
                = Dis[j];
            t = j;
        }} if (min = = Max) break;
        Ans + = min;

        Vis[t] = 1; for (k = 1; k <= N; k + +) {if (!vis[k] && dis[k] > Tt[t][k]) dis[k] = Tt[t][k
        ];
}} return ans; } void Init () {for (int i = 0; I <= n; i + +) {for (int j = 0; J <= N
            J + +) {if (i = = j) Tt[i][j] = 0;
        else tt[i][j] = Max;
    }}} int main () {char t[10];
    Char t1[10];
    int A, B;
        while (~SCANF ("%d", &n), n) {init ();
            for (int i = 0; i < n-1; i + +) {scanf ("%s%d", T, &a);
            int tmp_1 = t[0]-' A ' + 1;
            printf ("%s%d", t,a);
                for (int j = 0; J < A; J + +) {scanf ("%s%d", T1, &b);
                int tmp_2 = t1[0]-' A ' + 1;
                Tt[tmp_1][tmp_2] = b;
                Tt[tmp_2][tmp_1] = b;
            printf ("%c%c%d \ n", T[0],t1[0], tt[tmp_1][tmp_2]);
        }//printf ("\ n");
    } printf ("%d\n", Prim ()); }
}

b-networking

Description you is assigned to the design network connections between certain points in a wide area. You is given a set of points in the area, and a set of possible routes for the cables, which may connect pairs of points. For each possible route between and points, you were given the length of the cable that's needed to connect the points ove R that route. Note that there may exist many possible routes between, given points. It is assumed, the given possible routes connect (directly or indirectly) each of the points in the area.
Your task is to design the network for the area, so there is a connection (direct or indirect) between every TS (i.e., all the points is interconnected, but not necessarily by a direct cable), and which the total length of the used Cable is minimal.

Input The input file consists of a number of data sets. Each data set defines one required network. The first line of the set contains integers:the first defines the number P of the given points, and the second the Nu Mber R of given routes between the points. The following R lines define the given routes between the points, each giving three integer numbers:the first and numbers Identify the points, and the third gives the length of the route. The numbers is separated with white spaces. A data set giving only one number p=0 denotes the end of the input. The data sets is separated with a empty line.
The maximal number of points is 50. The maximal length of a given route is 100. The number of possible routes is unlimited. The nodes is identified with integers between 1 and P (inclusive). The routes between II points I and J may be given as I J or as J I.

Output for each data set, print one number in a separate line this gives the total length of the cable used for the entire Designed network.

Sample Input

1 0

2 3
1 2 PNS
2 1
1 2 3 7 1 2 2

3 each
3 1 7
1 3 5
2 3 3
1 91
  
   1 2

5 7
1 2 5
2 3 7
2 4 8 4 5 each 3 5 1
5 6
4 2

0
  

Sample Output

0
26

#include <stdio.h> #include <string.h> #define MAX 1000000 int ans;
int map[110][110];
int dis[110];
int vis[110];

int n;
    int prim () {int I, j, K;
    int ans = 0;
    int min, t;
    memset (Vis, 0, sizeof (VIS));
    for (i = 1; I <= n; i + +) {Dis[i] = Map[1][i];
    } vis[1] = 1;
        for (i = 1; i < n; i + +) {min = Max;  for (j = 1; J <= N; j + +) {if (!vis[j] && dis[j] < min) {min
                = Dis[j];
            t = j;
        }} if (min = = Max) break;
        Ans + = min;

        Vis[t] = 1; for (k = 1; k <= N; k + +) {if (!vis[k] && dis[k] > map [t][k]) dis[k] = Map[t
        ][K];
}} return ans;
            } void init (int n) {for (int i = 0; I <= n; i + +) {for (int j = 0; J <= N; j + +) {
  if (i = = j) {Map[i][j] = 0;              Continue
        } Map[i][j] = Max;
    }}} int main () {int i;
    int A, b, C;
    int m;
        while (~SCANF ("%d", &n) &&n) {scanf ("%d", &m);
        Init (n);
            for (i = 0; i < m; i + +) {scanf ("%d%d%d", &a, &b, &c);
                if (Map[a][b] > C) {map[a][b] = C;
            Map[b][a] = c;
    }} printf ("%d\n", Prim ());
} return 0; }

d-constructing Roads

Description There is N villages, which is numbered from 1 to N, and we should build some roads such that every both vill Ages can connect to each of the other. We say village A and B are connected, if and only if there is a road between A and B, or there exists a village C such That there was a road between A and C, and C and B are connected.

We know that there be already some roads between some villages and your job is the build some roads such so all the Vil Lages is connect and the length of the roads built is minimum.

Input The first line is a integer n (3 <= N <=), which is the number of villages. Then come n lines, the i-th of which contains n integers, and the j-th of these n integers are the distance (the distance s Hould is an integer within [1, +]) between village I and village J.

Then there was an integer q (0 <= q <= n * (n + 1)/2). Then come Q lines, each line contains the integers a and B (1 <= a < b <= N), which means the road between Villag e A and village B has been built.

Output you should output a line contains an integer, which is the length of the "All" the roads to being built such that all the VI Llages is connected, and this value is minimum.

Sample Input

3
0 990 692
990 0 179
692 179 0
1
1 2

Sample Output

179

Enter a set representing the distance between the point and the point, and the following group indicates that there is a bridge between the two points, so only 0 is required.

#include <stdio.h> #include <iostream> #include <algorithm> #include <string.h> using namespace

Std
#define N-#define MAX 1 << int vis[n];
int dis[n];
int map[n][n];

int n, m;
    int prime () {int I, j, K;
    int ans = 0;
    int min, t;
    memset (Vis, 0, sizeof (VIS));
    for (i = 1; I <= n; i + +) {Dis[i] = Map[1][i];
    } vis[1] = 1;
        for (i = 1; i < n; i + +) {min = Max;  for (j = 1; J <= N; j + +) {if (!vis[j] && dis[j] < min) {min
                = Dis[j];
            t = j;
        }} if (min = = Max) break;
        Ans + = min;

        Vis[t] = 1; for (k = 1; k <= N; k + +) {if (!vis[k] && dis[k] > map [t][k]) dis[k] = Map[t
        ][K];
}} return ans;
  } void Init () {for (int i = 0; I <= n; i + +) {for (int j = 0; J <= N; j + +)      {if (i = = j) Map[i][j] = 0;
        else map[i][j] = Max;
    }}} int main () {int A, B;
        while (~SCANF ("%d", &n)) {init (); for (int i = 1; I <= n; i + +) {for (int j = 1; J <= N; j + +) {scanf (
            "%d", &map[i][j]);
        }} scanf ("%d", &m);
            for (int i = 0; i < m; i + +) {scanf ("%d%d", &a,&b);
        MAP[A][B] = Map[b][a] = 0;
    } printf ("%d\n", Prime ()); }

}

h-highways

Description The nation of Flatopia is perfectly flat. Unfortunately, Flatopia has a very poor system of public highways. The Flatopian government is aware of this problem and have already constructed a number of highways connecting some of the Most important towns. However, there is still some towns that you can ' t reach via a highway. It's necessary to build more highways so it'll be possible to drive between any pair of towns without leaving the Highway System.

Flatopian towns is numbered from 1 to N and town I have a position given by the Cartesian coordinates (xi, Yi). Each highway connects Exaclty and towns. All highways (both the original ones and the ones, so is to being built) follow straight lines, and thus their length is EQ UAL to Cartesian distance between towns. All highways can is used in both directions. Highways can freely cross all other, but a driver can only switch between highways at a town that's located at the end O F both highways.

The Flatopian government wants to minimize, the cost of building new highways. However, they want to guarantee that every town are highway-reachable from every the other town. Since Flatopia is so flat and the cost of a highway are always proportional to its length. Thus, the least expensive highway system would be the one that minimizes the total highways length.

The

input consists of parts. The first part describes all towns in the country, and the second part describes all of the highways that has already bee N built.

The first line of the input file contains a single integer n (1 <= n <=), representing the number of T Owns. The next N lines each contain of integers, Xi and Yi separated by a space. These values give the coordinates of I th town (for I from 1 to N). Coordinates'll has an absolute value no greater than 10000. Every town have a unique location.

The next line contains a single integer M (0 <= m <=), representing the number of existing highways. The next M lines each contain a pair of integers separated by a space. These integers give a pair of town numbers which is already connected by a highway. Each pair of towns are connected by at the most one highway.

Output Write to the output A, single, each new highway, should be built in order to connect all towns with mini Mal possible total length of new highways. Each highway should is presented by printing town numbers the This highway connects, separated by a space.

If no new highways need to is built (all towns is already connected), then the output file should is created but it Shoul D be empty.

Sample Input

9
1 5
0 0 
3 2
4 5 5
1
0 4
5 2
1 2
5 3
3
1 3
9 7 1
2

Sample Output

1 6
3 7
4 9
5 7
8 3


This question is request, also need to establish which bridge, a start with Kruskal, half-day do not come out, later turned prim, disgusting I long, next time to see again ...

#include <stdio.h> #include <algorithm> #include <string.h> #include <math.h> using namespace

Std
#define N #define MAX 0x3fffffff int n, m;
int x[n];
int y[n];
int dis[n];
int vis[n];
int map[n][n];

int path[n];
    void Prim () {int ans = 0;
    int min, t;
    memset (Vis, 0, sizeof (VIS));
        for (int i = 1; I <= n; i + +) {Dis[i] = Map[1][i];
    Path[i] = 1;
    } vis[1] = 1;
        for (int i = 1; i < n; i + +) {min = Max;
                for (int j = 1; J <= N; j + +) {if (!vis[j] && dis[j] < min) {
                min = Dis[j];
            t = j;
        }}//if (min = = Max)//break;
        Ans + = min;
        Vis[t] = 1;
        if (map[path[t]][t]! = 0) printf ("%d%d\n", T, Path[t]);
                for (int k = 1; k <= N; k + +) {if (!vis[k] && dis[k] > map [t][k]) { DIS[K]= Map[t][k];
            Path[k] = t;
        }}}} void Init () {for (int i = 1; I <= n; i + +) {for (int j = 1; J <= N; j + +)
            {if (i = = j) {Map[i][j] = 0;
        } else map[i][j] = Max;
    }}} int main () {int A, B;
        while (~SCANF ("%d", &n)) {init ();
        for (int i = 1; I <= n; i + +) {scanf ("%d%d", &x[i],&y[i]);
                } for (int i = 1; I <= n; i + +) {for (int j = 1; J <= N; j + +) {
                MAP[I][J] = (X[i]-x[j]) * (X[i]-x[j]) + (Y[i]-y[j]) * (y[i]-y[j]);
            Map[j][i] = Map[i][j];
        }} scanf ("%d", &m);
            for (int i = 1; I <= m; i + +) {scanf ("%d%d", &a, &b);
        MAP[A][B] = Map[b][a] = 0;
    } prim (); }
}

i-agri-net

Description

Farmer John had been elected mayor of his town! One of his campaign promises is to bring internet connectivity to all farms in the area. He needs your help, of course.

Farmer John ordered a high speed connection for he farm and is going to share he connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.

Given a list of how much fiber it takes to connect all pair of farms, you must find the minimum amount of fiber needed to Connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.

The distance between any and farms would not exceed 100,000.

Input

The input includes several cases. For each case, the first line contains the number of farms, n (3 <= n <= 100). The following lines contain the n x n conectivity matrix, where each element is shows the distance from on farm to another. Logically, they is n lines of n space-separated integers. Physically, they is limited in the length to the characters, so some lines continue onto others. Of course, the diagonal would be 0, since the distance from farm I to itself are not interesting for this problem.

Output

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.