Routing Problem time limit: + Ms | Memory Limit:65535 KB Difficulty:4
-
-
Describe
-
Nanyang Institute of Technology to carry out the transformation of power lines, the headmaster now asked designers to design a wiring method, the wiring method needs to meet the following conditions:
1, all the buildings are to power up.
2, the use of wire to spend the least
-
-
Input
-
-
The first line is an integer n indicating that there are n sets of test data. (n<5)
The first line of each set of test data is two integer v,e.
V indicates the total number of school buildings (v<=500)
In the subsequent E-line, there are three integers in each line a,b,c represents a and B if the build line costs C (c<=100). (If the two buildings do not specify the cost, it means that the direct connection between the two buildings is too expensive or impossible to connect)
In the subsequent 1 lines, there are v integers, of which the number of I represents the cost of wiring from the I building to the external power supply facility. (0<e<v* (V-1)/2)
(Building number starting from 1), due to safety issues, can only choose one building connected to the external power supply equipment.
Data guarantees that there is at least one scenario that satisfies the requirements.
-
-
Output
-
-
each set of test data outputs a positive integer that represents the minimum cost of laying the line that meets the headmaster's requirements.
-
-
Sample input
-
-
14 61 2 102 3 103 1 101 4 12 4 13 4 11 3 5 6
-
-
Sample output
-
-
4
-
-
Source
-
-
[Zhang Yunzun] Original
-
-
Uploaded by
-
-
Zhang Yunzun
Minimum spanning tree template problem, the final answer is the sum of the sides of the minimum spanning tree plus the cost of connecting one of the buildings to an external power supply device. Record edges only record the smallest side connecting two buildings. The template applies the prim algorithm.
#include <stdio.h> #include <string.h> #define MAX_N 505#define INF 0x3f3f3f3f#include<algorithm> Using namespace Std;int Cost[max_n][max_n]; Represents the weight of the edge (the non-existent case is set to inf) int mincost[max_n]; From set X to the edge to each vertex of the minimum weight bool used[max_n]; Whether vertex i is contained in the set X (minimum spanning tree) of int v; Vertex number int prim ()//prim algorithm template {for (int i = 1;i <= v;i++)//Initialize {Mincost[i] = inf;used[i] = false;} MINCOST[1] = 0;int res = 0;while (true) {int e =-1;//select vertices from X to their weights for (int i = 1;i <= v;i++) {if (used[i] = = FA) from vertices that do not belong to X LSE && (E = =-1 | | mincost[i] < MINCOST[E])) e = i;} if (e = =-1) break;used[e] = true;res + = mincost[e];for (int i = 1;i <= v;i++) {mincost[i] = min (Mincost[i],cost[e][i]);} }return Res;} int main () {int n;scanf ("%d", &n), while (n--) {memset (cost,inf,sizeof (cost)); int e;scanf ("%d%d", &v,&e); int a,b,c;for (int i = 1;i <= e;i++) {scanf ("%d%d%d", &a,&b,&c); Cost[a][b] = cost[b][a] = min (cost[a][b],c) ;//record only the minimum cost of connecting two buildings}int minn = inf,x; Find the minimum cost of connecting an external power supply device for (int i = 1;i <= v;i++) {scanf ("%d", &x), minn = min (minn,x);} printf ("%d\n", Minn + prim ());//The final answer is the minimum cost of connecting V-buildings with Minn 0;}
Nyoj 38 Cabling Problems