hdu 3371(prim算法)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3371

Connect the Cities

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10313    Accepted Submission(s):
2937

Problem Description

In 2100, since the sea level rise, most of the cities
disappear. Though some survived cities are 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 money.

Input

The first line contains the number of test
cases.
Each test case 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 <= 100) stands for the number of still connected cities.
To make
it easy, the cities are signed from 1 to n.
Then follow m lines, each
contains three integers p, q and c (0 <= c <= 1000), means it takes c to
connect p and q.
Then follow k lines, each line starts with an integer t (2
<= t <= n) stands for the number of this connected cities. Then t integers
follow stands for the id of these cities.

Output

For each case, output the least money you need to take,
if it’s impossible, just output -1.

Sample Input

1

6 4 3

1 4 2

2 6 1

2 3 5

3 4 33

2 1 2

2 1 3

3 4 5 6

Sample Output

1

题目大意:

这一题也就是一个很简单的prim算法的变形,题意很容易理解,大概是这样的:一场大雨摧毁可很多路,但是还有一些依然存留,所以题目给出了一些路,还给了已经建好的一些,目的是求所有的路全部连通的最小花费。

在比赛的时候一直想用克鲁斯卡尔,结果不知道是哪里出问题了,一直不能ac,所有。。。。依旧是差这一题ak,在这里反思一下,应该学会转变,不能一直一个思路走下去,结果比赛后,看了这个题目,用prim很容易的解决了,只要在建好路的地图map[a][b]=map[b][a]=0就ok了!还有要注意判重~

详见代码。

 1 #include <iostream>
 2 #include <cstdio>
 3 using namespace std;
 4
 5 int map[1010][1010],r[1010],Min,n,sum,node[1010];
 6 const int INF=9999999;
 7
 8 void set()
 9 {
10     for (int i=1; i<=n; i++)
11     {
12         node[i]=INF;
13         for (int j=1; j<=n; j++)
14             map[i][j]=INF;
15     }
16 }
17
18 void prim()
19 {
20     int tm=1,m;
21     int vis[1010]= {0};
22     node[tm]=0;
23     vis[tm]=1;
24     for (int k=2; k<=n; k++)
25     {
26         Min=INF;
27         for (int i=1; i<=n; i++)
28             if (!vis[i])
29             {
30                 if (node[i]>map[tm][i])
31                     node[i]=map[tm][i];
32                 if (Min>node[i])
33                 {
34                     Min=node[i];
35                     m=i;
36                 }
37             }
38         tm=m;
39         sum+=Min;
40         vis[m]=1;
41     }
42 }
43
44 int main ()
45 {
46     int T;
47     cin>>T;
48     while (T--)
49     {
50         sum=0;
51         int m,k;
52         scanf("%d%d%d",&n,&m,&k);
53         set();
54         for (int i=1; i<=m; i++)
55         {
56             int p,q,c;
57             scanf("%d%d%d",&p,&q,&c);
58             if (map[p][q]>c)
59                 map[p][q]=map[q][p]=c;
60         }
61         while (k--)
62         {
63             int t;
64             scanf("%d",&t);
65             for (int i=1; i<=t; i++)
66             {
67                 cin>>r[i];
68             }
69             for (int i=1; i<=t; i++)
70             {
71                 for (int j=1; j<=t; j++)
72                 {
73                     map[r[i]][r[j]]=map[r[j]][r[i]]=0;
74                 }
75             }
76         }
77         prim();
78         if (sum<INF)
79         printf ("%d\n",sum);
80         else
81         printf ("-1\n");
82     }
83     return 0;
84 }
时间: 2024-10-08 08:16:21

hdu 3371(prim算法)的相关文章

hdu 1863 prim算法的使用

http://acm.hdu.edu.cn/showproblem.php?pid=1863 畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 15980    Accepted Submission(s): 6627 Problem Description 省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一

HDU 5253 Prim算法

http://acm.hdu.edu.cn/showproblem.php?pid=5253 Prim算法是 1.每次选出 (已经选出的)点集 能够连接 边权值最小的点 2.使用新找出的点能带来的新的更小的边权,来更新旧的较大的边权 3.重复,直到连接所有点 的贪心算法 使用优先权队列优化 查找 边权值最小的点 的步骤. #include <stdio.h> #include <string.h> #include <stdlib.h> #include <ios

hdu 3371 最小生成树prim算法

Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 8992    Accepted Submission(s): 2519 Problem Description In 2100, since the sea level rise, most of the cities disappear. Thoug

hdu 3371 Connect the Cities Prim + Kruskal两种算法分别AC 水过~~~~

Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 11727    Accepted Submission(s): 3278 Problem Description In 2100, since the sea level rise, most of the cities disappear. Tho

hdu 3371 Connect the Cities (最小生成树Prim)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没有用克鲁斯卡尔算法(Kruskal's algorithm),因为这题数据比较大,而且要处理大量的数据使它为0,怕超时T^T..... 1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 5 usi

hdu 3371 最小生成树 prim

http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目大意:告诉你有几座城市,再告诉你哪两座城市之间建路要多少钱,在给你哪几个城市之间已经有路,不需要再建.要求的是要使所有城市之间连通最小要花费多少钱. 这里我用了prim算法..保存城市之间的权值,对于已经建好的城市,将他们的权值赋为0.还有就是要判断是否能找出最小生成树,如果不可以就输出-1,如果在遍历点的时候,并不能找出一个最小的边,那么肯定就是没有最小生成树.(注意要在每个kase开始的时候将所

hdu 1102 Constructing Roads (Prim算法)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 21947    Accepted Submission(s): 8448 Problem Description There are N villa

hdu 1162 Eddy&amp;#39;s picture (Kruskal算法,prim算法,最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 [题目大意] 给你n个点的坐标,让你找到联通n个点的一种方法.保证联通的线路最短,典型的最小生成树问题. 方法一 . 通过不断找到最小的边来找到终于结果. Kruskal 算法 #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespac

hdu 1162 Eddy&#39;s picture (Kruskal算法,prim算法,最小生成树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 [题目大意] 给你n个点的坐标,让你找到联通n个点的一种方法,保证联通的线路最短,典型的最小生成树问题. 方法一 , 通过不断找到最小的边来找到最终结果. Kruskal 算法 #include <iostream> #include <algorithm> #include <cstdio> #include <cmath> using namespac