Connect the Cities

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 4   Accepted Submission(s) : 3
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
题解:
有一些城市未连接,一些城市已经连接,只需把连接的城市的权值赋值为0就好;
prime代码:
 #include<stdio.h>
#include<string.h>
const int INF=0x3f3f3f3f;
const int MAXN=;
int n,answer;
int map[MAXN][MAXN],vis[MAXN],low[MAXN];
void prime(){
memset(vis,,sizeof(vis));
int k,temp,flot=;
vis[]=;
for(int i=;i<=n;i++)
low[i]=map[][i];
for(int i=;i<=n;i++){
temp=INF;
for(int j=;j<=n;j++){
if(!vis[j]&&temp>low[j])temp=low[k=j];
}
if(temp==INF){
if(flot==n)printf("%d\n",answer);
else puts("-1");
// printf("%d %d\n",n,flot);
break;
}
vis[k]=;
flot++;
answer+=temp;
for(int j=;j<=n;j++)
if(!vis[j]&&low[j]>map[k][j])//ÎÞÓд³É<´íÁË°ëÌ죻£»£»
low[j]=map[k][j];
}
}
int main(){
int T,m,k,a,b,c;
scanf("%d",&T);
while(T--){answer=;
memset(map,INF,sizeof(map));
scanf("%d%d%d",&n,&m,&k);
while(m--){
scanf("%d%d%d",&a,&b,&c);
if(c<map[a][b])
map[a][b]=map[b][a]=c;//printf("...%d %d %d\n",a,b,c);
}
while(k--){
scanf("%d",&a);
for(int i=;i<a;i++){
scanf("%d",&b);
if(!i)c=b;
if(c!=b)
map[c][b]=map[b][c]=;//printf("***%d %d\n",b,c);
}
}
prime();
}
return ;
}

Connect the Cities(prime)的更多相关文章

  1. hdoj 3371 Connect the Cities

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

  2. Connect the Cities[HDU3371]

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)T ...

  3. Connect the Cities(MST prim)

    Connect the Cities Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  4. HDU 3371 kruscal/prim求最小生成树 Connect the Cities 大坑大坑

    这个时间短 700多s #include<stdio.h> #include<string.h> #include<iostream> #include<al ...

  5. hdu 3371 Connect the Cities

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...

  6. Connect the Cities(prim)用prim都可能超时,交了20几发卡时过的

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...

  7. HDU 3371 Connect the Cities(prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...

  8. Connect the Cities(hdu3371)并查集(附测试数据)

    Connect the Cities Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Other ...

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

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

随机推荐

  1. python基础之元组、文件操作、编码、函数、变量

    1.集合set 集合是无序的,不重复的,主要作用: 去重,把一个列表变成集合,就可以自动去重 关系测试,测试两组数据的交集,差集,并集等关系 操作例子如下: list_1 = [1,4,5,7,3,6 ...

  2. why cpp is a shitty language

    // the below is a standard template for any of my writings about c++ cpp_is_a_shitty_language_as { t ...

  3. js实现shell排序

    //shell排序配插入排序function shell_insert_sort(arr){ var gap = arr.length; do{ gap = parseInt(gap/3) + 1; ...

  4. qt捕获全局windows消息(使用QAbstractNativeEventFilter,然后注册这个类)

    qt  如何捕获全屏的鼠标事件,这个帖子上面主要讲述了下嵌入式qt怎么抓取系统级消息,不过从这篇文章中我也看到了希望,有个回复说winEventFilter支持这种方式,然后我就顺着这个线索找到了na ...

  5. ASP.NET jQuery 随笔 显示RadioButtonList成员选中的内容和值

    通过jQuery来获取RadioButtonList成员内容. <%@ Page Language="C#" AutoEventWireup="true" ...

  6. docker log 文件 清理

    1 查看docker log 文件位置  docker inspect *** 2 定时清理 runcate -s 0 /var/lib/docker/containers/*/*-json.log

  7. 推荐几个精致的web UI框架及常用前端UI框架

    1.Aliceui Aliceui是支付宝的样式解决方案,是一套精选的基于 spm 生态圈的样式模块集合,是 Arale 的子集,也是一套模块化的样式命名和组织规范,是写 CSS 的更好方式. git ...

  8. [转]RJ45接口说明

    [转]http://blog.csdn.net/dog0138/article/details/7016351 1.前言 常见的RJ45接口有两类: 用于以太网网卡.路由器以太网接口等的DTE类型,可 ...

  9. simple简单消息队列

    一:介绍 1.优缺点 简单,但是耦合性较高. 这种模式是生产者与消费者一一对应,就是一个产生者,有一个消费者来消费. 如果,多个消费者想消费一个队列中的消息就不适合了.这种情况在后面会接着介绍. 2. ...

  10. marathon-lb-什么是服务发现?(转)

    摘要: 将容器应用部署到集群时,其服务地址,即IP和端口, 是由集群系统动态分配的.那么,当我们需要访问这个服务时,如何确定它的地址呢?这时,就需要服务发现(Service Discovery)了.本 ...