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. Jquery时间快捷控件(Jtime)配置v1.1

    1.插件代码 /** * @title 时间工具类 * @note 本类一律违规验证返回false * @author {boonyachengdu@gmail.com} * @date 2013-0 ...

  2. 2、elasticsearch 的安装和插件的安装

    1.安装Elasticsearch集群 1.下载elasticsearch-2.0.0.tar.gz,执行tar -zxvf elasticsearch-2.0.0.tar.gz解压 2.修改conf ...

  3. Unity 移动MM自签名方式

    在使用Unity接移动MM SDK的时候,最后有一个签名.  主要是把计费文件和版权文件放入APK的根目录.  搞了半天才知道前来这么简单..... 软件使用: apk签名工具apktool

  4. SHDP--Working With HBase (二)之HBase JDBC驱动Phoenix与SpringJDBCTemplate的集成

    Phoenix:Phoenix将SQL查询语句转换成多个scan操作,并编排执行最终生成标准的JDBC结果集.   Spring将数据库访问的样式代码提取到JDBC模板类中,JDBC模板还承担了资源管 ...

  5. GCD 和延时调用

    因为 Playground 不进行特别配置的话是无法在线程中进行调度的,因此本节中的示例代码需要在 Xcode 项目环境中运行.在 Playground 中可能无法得到正确的结果. GCD 是一种非常 ...

  6. MVC5 Entity Framework学习之Entity Framework高级功能

    在之前的文章中,你已经学习了怎样实现每一个层次结构一个表继承. 本节中你将学习使用Entity Framework Code First来开发ASP.NET web应用程序时能够利用的高级功能. 在本 ...

  7. 使用 React 和 Flux 创建一个记事本应用

    React,来自 Facebook,是一个用来创建用户界面的非常优秀的类库.唯一的问题是 React 不会关注于你的应用如何处理数据.大多数人把 React 当做 MV* 中的 V.所以,Facebo ...

  8. kaggle之旧金山犯罪

    kaggle地址 github地址 特点: 离散特征 离散特征二值化处理 数据概览 import pandas as pd import numpy as np # 载入数据 train = pd.r ...

  9. 沃通tomcat jks 安装配置

    废话不多说上代码: <Connector port="443" protocol="org.apache.coyote.http11.Http11NioProtoc ...

  10. dojo.io.script

    dojo.io.script 定义: 跨域访问数据,可以动态的将script标签插入到网页当中. 局限: 1.只支持get方式访问: 2.只支持异步调用. 使用: 1.dojo.io.script.g ...