Connect the Cities

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other)
Total Submission(s) : 7   Accepted Submission(s) : 5
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
 
Author
dandelion
 
Source
HDOJ Monthly Contest – 2010.04.04



#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
#define INF 0xfffffff
int map[505][505],mark[505],num[505];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int i,j,m,n,k,a,b,c;
scanf("%d%d%d",&n,&m,&k);
for(i=0;i<=n;i++)
for(j=0;j<=n;j++)
map[i][j]=map[j][i]=INF;
for(i=0;i<m;i++)
{
scanf("%d%d%d",&a,&b,&c);
if(c<map[a][b])
map[a][b]=map[b][a]=c;
}
for(i=0;i<k;i++)
{
scanf("%d",&a);
for(j=0;j<a;j++)
scanf("%d",&num[j]);
for(j=0;j<a;j++)
for(int jj=j+1;jj<a;jj++)
{
map[num[j]][num[jj]]=map[num[jj]][num[j]]=0;
}
}
int sum=0,flog;
memset(mark,0,sizeof(mark));
for(i=2;i<=n;i++)
{
int min=INF;
flog=-1;
for(j=2;j<=n;j++)
{
if(!mark[j]&&map[1][j]<min)
{
flog=j;
min=map[1][j];
}
}
if(flog==-1) break;
mark[flog]=1;
sum+=map[1][flog];
for(j=2;j<=n;j++)
{
if(!mark[j]&&map[1][j]>map[flog][j])
map[1][j]=map[flog][j];
}
}
if(i>n)
printf("%d\n",sum);
else printf("-1\n");
}
return 0;
}

Connect the Cities--hdoj的更多相关文章

  1. hdoj 3371 Connect the Cities

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

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

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

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

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

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

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

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

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

  6. hdu3371 Connect the Cities (MST)

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

  7. Connect the Cities[HDU3371]

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

  8. Connect the Cities(MST prim)

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

  9. hdu 3371 Connect the Cities

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

  10. Connect the Cities(prime)

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

随机推荐

  1. Jsp页面报错状态码含义

    原来,全部在HttpServletResponse接口的字段里 状态码 (),表示一个请求已经被接受处理,但还没有完成.  状态码 (),表明HTTP服务器从一个服务器收到了一个无效的响应,当其作为一 ...

  2. 使用composer 实现自动加载

    准备工作:提前安装好composer 1.创建项目目录OOP 2.OOP目录下新建composer.json文件,composer.json是一个空json文件,代码如下: { } 3.打开控制台,进 ...

  3. C#调用Java的WebService出现500 服务器错误

    最近在用C#调用Java写的WebService时,发现老是返回500 服务器错误,到底什么原因一直找不出来, 后来google了以后,找到国外的http://stackoverflow.com站点已 ...

  4. Scala——面向对象和函数式编程语言

    Scala Scala是一门运行时基于JVM的编程语言,具备函数式编程和面向对象特点. 基本知识 basics 任意词均可作为符号名,对于关键词,以反引号包裹,避免使用下划线,避免带美元符的名字. 声 ...

  5. .Net并行计算支持嵌套事务的方法

    问题背景 一年前,我们开始利用.Net 4.0的TPL(Task Parallel Library)并行计算技术对复杂计算的功能节点进行性能优化,这些复杂计算往往会包含大量对数据库的操作.在应用TPL ...

  6. js document 触发按键事件

    // 键盘控制 var keyEvent = (function () { document.onkeydown = function (e) { if (e.keyCode === 38) { // ...

  7. 【转载】JavaWeb之DBUtils QueryRunner类对数据表的增、删、查(8种结果集处理方式)、改操作

    一.使用QueryRunner类,实现对数据表的 insert delete update package com.shuhuadream.queryrunner; import java.sql.C ...

  8. 拓扑排序(Topological Order)

    Date:2019-06-17 14:43:59 算法描述 1.定义队列Q,并把所有入度为0的结点加入队列 2.取队首结点,输出.然后删除所有从它除法的边,并令这些边到达的顶点的入度-1,若某个顶点的 ...

  9. 解决HTML select控件 设置属性 disabled 后无法向后台传值的方法

    大家都知道有时候修改数据的时候我们希望有一些数据是不可以修改的,通常情况下我们会将input框设置为 readonly , 但是 select 控件没有这个属性,需要使用另一个属性 disabled ...

  10. Bootstrap 表单控件状态(禁用状态)

    Bootstrap框架的表单控件的禁用状态和普通的表单禁用状态实现方法是一样的,在相应的表单控件上添加属性“disabled”.和其他表单的禁用状态不同的是,Bootstrap框架做了一些样式风格的处 ...