Title Link: HDU 1102 constructing Roads
Constructing Roads
Problem Descriptionthere is N villages, which is numbered from 1 to N, and we should build some roads such that every t Wo villages 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.
Inputthe 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.
Outputyou should output a line contains an integer, which is the length of the "All the roads" to be built such Lages is connected, and this value is minimum.
Sample Input
30 990 692990 0 179692 179 011 2
Sample Output
179
Sourcekicc
Recommendeddy | We have carefully selected several similar problems for you:1142 1598 1116 1269 1596Test Instructions
There are n villages to repair the road, give the original cost of repairing each road, and now some roads have been repaired. We are asking for the minimum cost of repairing the remaining roads.
Analysis
The minimum spanning tree algorithm, because it is the adjacency matrix, so the use of the prim method is very convenient to solve, for the road has been repaired, we can set their costs to 0, and then the prim algorithm can be obtained by solving.
Code
Adjacency Matrix:
#include <iostream> #include <cstdio> #include <queue>using namespace std; #define MAXN 110#define INF 0xffffint g[maxn][maxn];int n;struct node{int V, key; friend bool operator< (Node A, Node B) {return a.key > B.key; }};bool Visited[maxn];node VX[MAXN];p riority_queue<node> q;void Prim () {for (int i = 1; I <= n; i++) { VX[I].V = i; Vx[i].key = INF; Visited[i] = false; } vx[1].key = 0; Q.push (vx[1]); while (!q.empty ()) {node nd = Q.top (); Q.pop (); int st = ND.V; if (visited[st]) continue; Visited[st] = true; for (int j = 1; J <= N; j + +) {if (J! = St &&!visited[j] && vx[j].key > G[st][j]) {vx[j].key = g[st][j]; Q.push (Vx[j]); }}}}int Main () {int m, a, B; while (~SCANF ("%d", &n)) {for (int i = 1; I <= n; i++) { for (int j = 1; J <= N; j + +) scanf ("%d", &g[i][j]); G[i][i] = INF; } scanf ("%d", &m); while (m--) {scanf ("%d%d", &a, &b); G[A][B] = G[b][a] = 0; } Prim (); int ans = 0; for (int i = 1; I <= n; i++) ans + = Vx[i].key; printf ("%d\n", ans); } return 0;}
adjacency table:
#include <iostream> #include <cstdio> #include <vector> #include <queue>using namespace std;# Define MAXN 110//MAX vertex number int n, m; Number of vertices, number of edges struct arcnode//edge node {int vertex; Vertex numbers adjacent to the header nodes int weight; The weighted value of the edge connecting the two vertices arcnode * next; Point to next adjacent contact Arcnode () {} arcnode (int v,int w): Vertex (v), weight (W), Next (NULL) {}};struct vernode//Vertex node, table for each adjacency table Head node {int vex; Current fixed-point number Arcnode * FIRARC; The edge of the first vertex connected to the vertex}ver[maxn];void Init ()//The adjacency table of the diagram needs to be initialized first, establishing vertex node {for (int i = 1; I <= n; i++) {Ver[i].vex = i; Ver[i].firarc = NULL; }}void Insert (int a, int b, int w)//tail interpolation, insert a as a starting point, B is the end point, the right is the edge of W, efficiency is inferior to the head plug, but can go to the heavy side {Arcnode * q = new Arcnode (b, W); if (Ver[a].firarc = = NULL) Ver[a].firarc = q; else {Arcnode * p = ver[a].firarc; if (P->vertex = = b) {if (P->weight > W) p->weight = w; return; } while (P->next! = NULL) { if (P->next->vertex = = b) {if (P->next->weight > W); P->next->weight = W; return; } p = p->next; } p->next = q; }}void Insert2 (int A, int b, int w)//head interpolation, more efficient, but cannot go to heavy side {Arcnode * q = new Arcnode (b, W); if (Ver[a].firarc = = NULL) Ver[a].firarc = q; else {Arcnode * p = ver[a].firarc; Q->next = p; Ver[a].firarc = q; }}STRUCT node//node {int V to save key value; int key; friend bool operator< (Node A, node B)//self-defined priority, key small priority {return a.key > B.key; }}; #define INF 0XFFFFF//weights upper limit bool VISITED[MAXN]; Whether the tree node VX[MAXN has been added]; Holds the weight of each node connected to its parent node priority_queue<node> q; The priority queue STL implementation void Prim ()//s represents the root node {for (int i = 1; I <= n; i++)//initialization {VX[I].V = i; Vx[i].key = INF; Visited[i] = false; } vx[1].key = 0; Q.push (vx[1]); while (!q.empty ()) {node nd = Q.top (); Take the team first, remember to quickly pop off Q.pop (); if (VISITED[ND.V])//Note the meaning of this sentence, avoid very much unnecessary operation continue; VISITED[ND.V] = true; Arcnode * p = ver[nd.v].firarc; while (P! = NULL)//Find all adjacent nodes, if not, enter queue {if (!visited[p->vertex] && p->weight < vx[p-& Gt;vertex].key) {Vx[p->vertex].key = p->weight; VX[P->VERTEX].V = p->vertex; Q.push (Vx[p->vertex]); } p = p->next; }}}int Main () {int m, a, b, X; while (~SCANF ("%d", &n)) {Init (); for (int i = 1; I <= n; i++) {for (int j = 1; J <= N; j + +) {scanf ("%d", &X); if (x! = 0) Insert2 (i, J, X); }} scanf ("%d", &m); while (m--) {scanf ("%d%d", &a, &b); Insert (A, b, 0); InsERT (b, a, 0); } Prim (); int ans = 0; for (int i = 1; I <= n; i++) ans + = Vx[i].key; printf ("%d\n", ans); } return 0;}
HDU 1102 constructing Roads, prim+ priority queue