Ural 1982 Electrification Plan (prim最小生成树)

很明显的最小生成树模板题 多点生成

[cpp] view plaincopy

  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. int n,k,a;
  4. int dist[120],m[120][120];
  5. bool p[120];
  6. void prim()
  7. {
  8. for(int i=1;i<=n;i++)
  9. {
  10. if(!p[i])
  11. {
  12. int Min=100020;
  13. for(int j=1;j<=n;j++)
  14. {
  15. if(p[j]&&m[i][j]<Min)
  16. Min=m[i][j];
  17. }
  18. dist[i]=Min;
  19. }
  20. }
  21. for(int i=1;i<=n-k;i++)
  22. {
  23. int Min=INT_MAX,k=0;
  24. for(int j=1;j<=n;j++)
  25. {
  26. if(!p[j]&&dist[j]<Min)
  27. {
  28. Min=dist[j];
  29. k=j;
  30. }
  31. }
  32. if(k==0)
  33. return;
  34. p[k]=true;
  35. for(int j=1;j<=n;j++)
  36. {
  37. if(!p[j]&&dist[j]>m[k][j])
  38. dist[j]=m[k][j];
  39. }
  40. }
  41. }
  42. int main()
  43. {
  44. memset(p,false,sizeof(p));
  45. scanf("%d%d",&n,&k);
  46. for(int i=1;i<=k;i++)
  47. {
  48. scanf("%d",&a);
  49. dist[a]=0;
  50. p[a]=true;
  51. }
  52. for(int i=1;i<=n;i++)
  53. for(int j=1;j<=n;j++)
  54. scanf("%d",&m[i][j]);
  55. prim();
  56. int sum=0;
  57. for(int i=1;i<=n;i++)
  58. sum+=dist[i];
  59. printf("%d\n",sum);
  60. return 0;
  61. }

Ural 1982 Electrification Plan (prim最小生成树)

时间: 2024-08-10 23:28:01

Ural 1982 Electrification Plan (prim最小生成树)的相关文章

URAL 1982. Electrification Plan(并查集)

题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1982 Some country has n cities. The government has decided to electrify all these cities. At first, power stations in k different cities were built. The other cities should be connected with the power

Timusoj 1982. Electrification Plan

http://acm.timus.ru/problem.aspx?space=1&num=1982 1982. Electrification Plan Time limit: 0.5 secondMemory limit: 64 MB Some country has n cities. The government has decided to electrify all these cities. At first, power stations in k different cities

timus 1982 Electrification Plan(最小生成树)

Electrification Plan Time limit: 0.5 secondMemory limit: 64 MB Some country has n cities. The government has decided to electrify all these cities. At first, power stations in k different cities were built. The other cities should be connected with t

Prim 最小生成树算法

Prim 算法是一种解决最小生成树问题(Minimum Spanning Tree)的算法.和 Kruskal 算法类似,Prim 算法的设计也是基于贪心算法(Greedy algorithm). Prim 算法的思想很简单,一棵生成树必须连接所有的顶点,而要保持最小权重则每次选择邻接的边时要选择较小权重的边.Prim 算法看起来非常类似于单源最短路径 Dijkstra 算法,从源点出发,寻找当前的最短路径,每次比较当前可达邻接顶点中最小的一个边加入到生成树中. 例如,下面这张连通的无向图 G,

HDU4081 Qin Shi Huang&#39;s National Road System【prim最小生成树+枚举】

先求出最小生成树,然后枚举树上的边,对于每条边"分别"找出这条割边形成的两个块中点权最大的两个 1.由于结果是A/B,A的变化会引起B的变化,两个制约,无法直接贪心出最大的A/B,故要通过枚举 2.不管magic road要加在哪里,加的边是否是最小生成树上的边,都会产生环,我们都要选择一条边删掉 注意删掉的边必须是树的环上的边,为了使结果最大,即找出最大的边 3.可以枚举两点,找出边,也可以枚举边,找出点,我是用后者,感觉比较容易写也好理解 #include <cstdio&g

poj 2485(prim最小生成树)

Highways Description The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways

POJ 1258 Agri-Net (prim最小生成树)

最小生成树模板题 #include<bits/stdc++.h> using namespace std; int n,a; int dist[120],m[120][120]; void prim() {     bool p[1020];     for(int i=2;i<=n;i++)     {         p[i]=false;         dist[i]=m[1][i];     }     dist[1]=0,p[1]=true;     for(int i=1;

ural Electrification Plan 最小生成树,多个源点

多个源点,虚拟成一个超点 题意:有很多岛,有些岛上有电站,岛与岛之间铺光缆可以互相通电.求使全部岛通电的最小代价.本来是很朴素的最小生成树,但是不止一个岛有电站就使得这个问题变成多个源点的问题 处理是重新构造图,将所有电站当作一个点,边(电站,无电岛) 最小生成树可以有超点,那最短路可不可以呢?联想上一道行星的题,求从底层一个点到顶层点的最短路,那可不可以把最顶的节点当成一个超点??怎么实现?这道题目测不可以.再仔细想,比如单源最短路dijstra算法球的是源点到每个点的最短距离,所以终止是一个

URAL(timus) 1272 Non-Yekaterinburg Subway(最小生成树)

Non-Yekaterinburg Subway Time limit: 1.0 secondMemory limit: 64 MB A little town started to construct a subway. The peculiarity of the town is that it is located on small islands, some of them are connected with tunnels or bridges. The mayor is sure