HDU 3371 Connect the Cities Prim + Kruskal two algorithms respectively AC water over ~ ~ ~ ~ ~ ~

Source: Internet
Author: User

Connect the CitiesTime limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 11727 Accepted Submission (s): 3278


Problem DescriptionIn 2100, since the sea level rise, most of the cities disappear. Though Some survived cities is still connected with others, but most of them become disconnected. The government wants to build some roads to connect all of these cities again, but they don't want to take too much.
Inputthe First line contains the number of test cases.
Each test case is starts with three integers:n, M and K. N (3 <= n <=500) stands for the number of survived cities, m (0 <= M <= 25000) stands for the number of roads you can choose to connect the cities and K (0 <= K <=) St ANDs for the number of still connected cities.
To make it easy, the cities is signed from 1 to N.
Then follow m lines, each contains three integers p, Q and C (0 <= C <=), means it takes C to connect P and Q.
Then follow k lines, each line starts with a integer t (2 <= t <= N) stands for the number of this connected cities . Then T integers follow stands for the ID of these cities.

Outputfor each case, the output of the least money is need to take, if it's impossible, just output-1.
Sample Input
16 4 31 4 22 6 12 3 53 4 332 1 22 1 33 4 5 6

Sample Output
1

Authordandelion Reprint please declare blog original address: http://blog.csdn.net/lionel_d
Fuuuuuuuuuuuuuuuuuuuck, I forgot to judge the heavy side!!!! All right, I'm so stupid! First of all, the problem is very simple, we only need two loops to connect the distance between the city set to 0 can be, prim algorithm 400ms+, and Kruskal algorithm 700ms+, and I also did and check set optimization.
Prim Algorithm Code:
#include <stdio.h> #include <string.h> #define MAX 550#define INF 100000000int Graph[max][max]; int prim (int n {int sum = 0, Lowcost[max]; bool Visited[max]; memset (visited,false,sizeof (visited)); for (int i = 1; I <= n; ++i) {lo Wcost[i] = Graph[1][i];} Visited[1] = true; for (int i = 1; i < n; ++i) {int min = INF, index =-1, for (int j = 1; j <= N; ++j) {if (!visite D[J] && min>lowcost[j]) {min = lowcost[j]; index = j;}} if (index = =-1) {if (I < n) {return INF;} break;} Sum + = Lowcost[index]; Visited[index] = true; for (int j = 1; j <= N; ++j) {if (!visited[j] && Lowcost[j]>gra Ph[index][j]) {lowcost[j] = Graph[index][j];}} return sum;} int main () {int t; scanf ("%d", &t), while (t--) {int n, m, k;scanf ("%d%d%d", &n,&m,&k); for (int i = 1; i < = N; ++i) {Graph[i][i] = 0; for (int j = 1; j < i; ++j) {graph[i][j] = graph[j][i] = INF;}} for (int i = 0; i < m; ++i) {int x, y, W; scanf ("%d%d%d", &x,&y,&w); if (w<graph[x] [y]) {Graph[x][y] = graph[y][x] = w;}} int P[max]; for (int i = 0; i < K; ++i) {int t; scanf ("%d", &t); for (int j = 0; j < T; ++j) {scanf ("%d", &p[j] );} for (int j = 0, J < T; ++j) {for (int r = 0; R < J; ++r) {Graph[p[j]][p[r]] = graph[p[r]][p[j]] = 0;}} int sum = PRIM (n), if (sum >= INF) {puts ("1");} else{printf ("%d\n", Sum);}} return 0;}

Kruskal Algorithm Code:
#include <cstdio> #include <algorithm> #include <cstring> #define MAX 550#define INF 1000000000using namespace std; struct edge{int x, Y, W;} Edge[max*max];bool cmp (const edge &AMP;E1, const edge &e2) {return E1.W&LT;E2.W;} int F[max]; int find (int x) {int r = x; while (r! = F[r]) {r = F[r];} int temp; while (x! = F[x]) {temp = f[x]; f[x] = r; x = temp;} return r;} void Init () {for (int i = 0; i < MAX; ++i) {f[i] = i;}} int Kruskal (int n, int m) {sort (edge,edge+n,cmp); init (); int sum = 0, Count = 0;for (int i = 0; i < n; ++i) {int x = fi nd (edge[i].x), y = Find (EDGE[I].Y), if (x! = y) {count++; sum + = edge[i].w;f[x] = F[y];}} if (count<m-1) {return INF;} return sum;}  int main () {int t; scanf ("%d", &t), while (t--) {int n, m, k;scanf ("%d%d%d", &n,&m,&k); int index = 0; for (int i = 0; I < m; ++i) {int x, y, W; scanf ("%d%d%d", &x,&y,&w); edge[index].x = x, edge[index].y = y, edge[index++].w = w;} int P[max]; for (int i = 0; i < K; ++i{int t; scanf ("%d", &t), for (int j = 0; j < T; ++j) {scanf ("%d", &p[j]);} for (int j = 0; j < T; ++j) {for (int r = 0; R < J; ++r) {edge[index].x = P[j], edge[index].y = P[r], edge[index++ ].W = 0;}}} int sum = Kruskal (index,n), if (sum >= INF) {puts ("1");} else{printf ("%d\n", Sum);}} return 0;}

with June

HDU 3371 Connect the Cities Prim + Kruskal two algorithms respectively AC water over ~ ~ ~ ~ ~ ~

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.