题目链接: 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): 13628    Accepted Submission(s): 3679

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

给出城市的数量 n 以及 需要相联的城市及所需花销,再给出已经相联的城市。求将所有城市相联的最小花销。

对于已经相联的城市,我们也将他们视为没有相联,并且将花销置为0.这样就和普通的最小生成树一样了,从头开始寻找就可以了

【源代码】

#include <cstdio>
#include <algorithm>
#include <vector>
#include <iostream>
#define INF 0xfffffff
using namespace std;int n,m,k;
const int maxn = 510;
struct node{int v,len;node(int v=0, int len = 0):v(v),len(len){}
};
vector <node> G[maxn];
int intree[maxn];
int minDist[maxn];
void init(){for(int i=0;i<maxn;i++){intree[i]=0;G[i].clear();minDist[i]=INF;}
}
int prim(int s){intree[s]=1;int ans=0;for(int i=0;i<G[s].size();i++){int vex = G[s][i].v;minDist[vex] = min(G[s][i].len,minDist[vex]);}for(int nodeNum=0;nodeNum<n-1;nodeNum++){int tmpMin=INF;int addNode;for(int i=1;i<=n;i++){ //从1 到 n 的城市标号 if(!intree[i]&&minDist[i]<tmpMin){tmpMin=minDist[i];addNode = i;}}if(tmpMin==INF) {// cout<<"bug"<<endl;return -1;}ans+=tmpMin;intree[addNode]=1;for(int i=0;i<G[addNode].size();i++){int vex = G[addNode][i].v;if(!intree[vex]&&G[addNode][i].len<minDist[vex])minDist[vex] = G[addNode][i].len;}}return ans;
}
int main(){int t;scanf("%d",&t);while(t--){init();scanf("%d%d%d",&n,&m,&k);int v1,v2,len;int start=1;for(int i=0;i<m;i++){scanf("%d%d%d",&v1,&v2,&len);G[v1].push_back(node(v2,len));G[v2].push_back(node(v1,len));if(i==0)start = v1;}for(int i=0;i<k;i++){int n;scanf("%d",&n);int num[110];for(int j=0;j<n;j++){scanf("%d",&num[j]);}for(int j=0;j<n;j++){for(int k=j+1;k<n;k++){G[num[j]].push_back(node(num[k],0));G[num[k]].push_back(node(num[j],0));}}}int ans=prim(start);printf("%d\n",ans);}return 0;
}

转载于:https://www.cnblogs.com/chaiwenjun000/p/5320979.html

hdu 3371 Connect the Cities(prim算法)相关推荐

  1. HDU 1863畅通工程(最小生成树)(prim算法)

    畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submi ...

  2. hdu 1233 还是畅通工程 最小生成树(prim算法 + kruskal算法)

    还是畅通工程                                                                            Time Limit: 4000/2 ...

  3. hdu 1233 还是畅通工程(最小生成树的Prim和Kruskal两种算法的c++实现)(prim算法详解)...

    赤裸裸滴最小生成树(MST),刚学的玩意,用两种方法熟练一下.(都是greedy) Kruskal方法:先对边按照代价非递减排序,再不断添加边且不产生环路,当边数=点数-1结束.判断加入(v,w)是否 ...

  4. 最小生成树(Kruskal算法+Prim算法)简单讲解+最小生成树例题 acm寒假集训日记22/1/8

    算法简讲部分: Kruskal算法: 基于贪心策略大致过程分为第三步:1. 我们先用结构体把每条边的端点和权值记录下来,然后对每条边按权值进行排序2. 因为 使图连通最少需要n-1 条边,所以我们依次 ...

  5. 数据结构与算法(7-3)最小生成树(普里姆(Prim)算法和克鲁斯卡尔(Kruskal)算法)

    目录 一.最小生成树简介 二.普里姆算法(Prim) 1.原理 2.存储 2-1.图顶点和权: 2-3. 最小生成树: 3.Prim()函数 3-1.新顶点入树 3-2.保留最小权 3-3. 找到最小 ...

  6. 基本数据结构(图: 基本结构,DFS,prim算法, kruskal算法)

    #include <iostream> using namespace std; //约定: //1. 图是由很多节点(VERTEX)构成的, 因此图结构是由一个VERTEX的链表构成的, ...

  7. 【数据结构】最小生成树 Prim算法 Kruskal算法

    最小生成树应用场景: 假设以下场景,有一块木板,板上钉上一些钉子,这些钉子可以由一些细绳连接起来.假设每个钉子可以通过一根或者多根细绳连接起来,那么一定存在这样得情况,即用最少的细绳把所有的钉子连接起 ...

  8. HDU1863(Prim算法)

    方法一:Prim算法 #include<iostream> #include<algorithm> #include<cstring> #include<ve ...

  9. 生成树的概念,最小生成树Prim算法 Kruskal算法

    求解最小生成树可以用Prim算法 Kruskal算法

最新文章

  1. 遇到问题:push的时候出现fatal: Authentication failed for
  2. 企业流程管理平台V2.0介绍
  3. HttpsURLConnection 返回 400
  4. kaggle:PUBG Finish Placement Prediction
  5. 【英语学习】【WOTD】accolade 释义/词源/示例
  6. Android中的GraphicBuffer同步机制-Fence
  7. GDI+ 保存HDC为位图文件
  8. 对比了最常见的几家开源OCR框架,我发现了最好的开源模型
  9. Win10修改EFI分区文件
  10. 虚拟打印机 android版,虚拟打印机(SmartPrinter)
  11. Dubbo太难了,我决定加入Spring Cloud阵营了...
  12. python培训班出来能找到工作吗-Python培训班出来好找工作吗?
  13. 磁盘驱动器或Windows Home Server失败的情况挽救了我的婚姻
  14. 记一次内网SSH后门误报事件
  15. Unity 碰撞体 composite
  16. 赠与今年的大学毕业生,胡适
  17. linux debian怎么重启网卡,debian10网卡设置
  18. android live 电视 源码,GitHub - mxiaoguang/LivePlayback: Android TV直播电视节目 ,包含各央视频道及卫视频道...
  19. 新生计算机能力水平测试,【新生必读】2018级新生计算机水平入学考试要点
  20. c# 页面打印预览 并保存为PDF

热门文章

  1. 软件设计模式之单例模式
  2. 理解C++ dynamic_cast
  3. 高通要求欧盟取消12亿美元反垄断罚款,理由是……
  4. 从安装认识Angular 2
  5. LINUX find、ln 常用命令总结
  6. android 关联源码
  7. HDU-时间挑战 树状数组
  8. The Double-Checked Locking is Broken Declaration
  9. 043、JVM实战总结:动手实验,自己动手模拟出频繁Young GC的场景
  10. l2正则化python_回归分析_L2正则化(岭回归)【python实现】