hdu3371 Connect the Cities (MST)
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13722 Accepted Submission(s): 3711
want to take too much money.
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.
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
1
pid=1102" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1102
1301 1162pid=1198" target="_blank" style="color:rgb(26,92,200); text-decoration:none">1198
1598Statistic | Submit | Discuss | Note
难理解的就是最后那k行。開始的数字t表示有几个城市。然后输入t个城市,表示第一个城市和第二个连接,第二个和第三个连接。
。。
用kruskal算法超时的多提交两次。
。当然也能够用pri算法。。
不想写。。
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
struct node
{
int a,b,cost;
}c[30000];
int fa[505];
void init(int n)
{
for(int i=1;i<=n;i++)
fa[i]=i;
}
bool cmp(node x,node y)
{
return x.cost<y.cost;
}
int find(int x)
{
if(fa[x]!=x) fa[x]=find(fa[x]);
return fa[x];
}
int main()
{
int n,k,m,ncase;
scanf("%d",&ncase);
while(ncase--)
{
scanf("%d %d %d",&n,&k,&m);
init(n);
for(int i=0;i<k;i++)
scanf("%d %d %d",&c[i].a,&c[i].b,&c[i].cost);
for(int i=1;i<=m;i++)
{
int x,pos,pos1;
scanf("%d %d",&x,&pos);
for(int j=1;j<x;j++)
{
scanf("%d",&pos1);
c[k].a=pos,c[k].b=pos1,c[k].cost=0;
pos=pos1;
k++;
}
}
sort(c,c+k,cmp);
int sum=0;
for(int i=0;i<k;i++)
{
int x=find(c[i].a);
int y=find(c[i].b);
if(x!=y)
sum+=c[i].cost,fa[x]=y;
}
int count=0;
for(int i=1;i<=n;i++)
if(fa[i]==i)
count++;
if(count!=1)
printf("-1\n");
else
printf("%d\n",sum);
}
return 0;
}
hdu3371 Connect the Cities (MST)的更多相关文章
- Connect the Cities(MST prim)
Connect the Cities Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- HDU3371 Connect the Cities
题目描述: 有n个小岛,其中有的小岛之间没有通路,要修这样一条通路需要花费一定的钱,还有一些小岛之间是有通路的.现在想把所有的岛都连通起来,求最少的花费是多少. 输入: 第一行输入T,代表多少组数据. ...
- Connect the Cities[HDU3371]
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...
- Connect the Cities(hdu3371)并查集(附测试数据)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- hdu oj 3371 Connect the Cities (最小生成树)
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑
这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- hdoj 3371 Connect the Cities
Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Other ...
- Connect the Cities(prime)
Connect the Cities Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) ...
随机推荐
- 怎么用css hack处理各浏览器兼容IE6,IE7,IE8,IE9/ FF
第一:什么事浏览器兼容性 浏览器兼容性问题又被称为网页兼容性或网站兼容性问题,指网页在各种浏览器上的显示效果可能不一致而产生浏览器和网页间的兼容问题.在网站的设计和制作中,做好浏览器兼容,才能够让网站 ...
- Android 自己搭建一个直播系统吧
服务端用 SRS(Simple Rtmp Server),在这里下载simple-rtmp-server需要Linux系统最好是Ubuntu,装个Ubuntu虚拟机就行了在Linux里,解压缩SRS ...
- Json解析与Gson解析
本文主要介绍json最原始的解析与google提供的gson工具类解析 ①json解析 /** * 普通的json解析 * @param s * @throws JSONException */ pr ...
- service里设置websocket心跳并向fragment发送数据
垃圾小白写了自己看的 /** * service 文件 */ public class SocketService extends Service { //自己定义接口用来传参 private sta ...
- Redis主从复制失败(master_link_status:down)
今天配置redis主从复制时出现master_link_status:down提示. 首先打开slave的redis.conf配置文件,确定slaveof 和masterauth 两个选项配置是否正确 ...
- 解决springmvc返回json中文乱码
在pringmvc中通过设置@ResponseBody返回json乱码问题,这个问题上网找了很久,发现答案真是人云亦云,奉上我的解决方案: 解决方案一:需要导入 jackson-core-asl-1. ...
- HDU_1532_最大流
Drainage Ditches Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) ...
- CentOS安装Nodejs-v8.11.1
(1)到NodeJs官网(https://nodejs.org/en/download/),复制下载链接 (2)下载并解压 命令:wget https://nodejs.org/dist/v8.11. ...
- Linux之日志管理
日志介绍日志配置日志管理远程日志基于MYSQL的日志 日志介绍日志:历史事件:时间,地点,人物,事件日志级别:事件的关键性程度,Loglevel系统日志服务:sysklogd :CentOS 5之前版 ...
- JavaScript学习笔记之DOM介绍
目录 1.简介 2.方法 3.属性 4.访问节点 5.修改节点 6.添加节点 7.删除节点 8.替换节点 9.改变 CSS 1.简介 文档对象模型(Document Object Model,DOM) ...