hdoj 3371 Connect the Cities
Connect the Cities
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 12903 Accepted Submission(s):
3549
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.
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.
if it’s impossible, just output -1.
- #include<stdio.h>
- #include<string.h>
- #define MAX 550
- #define INF 0x3f3f3f
- int city;
- int visit[MAX],map[MAX][MAX],low[MAX];
- void prime()
- {
- int j,i,min,mincost=0,next;
- memset(visit,0,sizeof(visit));
- for(i=1;i<=city;i++)
- {
- low[i]=map[1][i];
- }
- visit[1]=1;
- for(i=1;i<city;i++)
- {
- min=INF;
- for(j=1;j<=city;j++)
- {
- if(!visit[j]&&min>low[j])
- {
- next=j;
- min=low[j];
- }
- }
- if(min==INF)
- {
- printf("-1\n");
- return ;
- }
- mincost+=min;
- visit[next]=1;
- for(j=1;j<=city;j++)
- {
- if(!visit[j]&&low[j]>map[next][j])
- low[j]=map[next][j];
- }
- }
- printf("%d\n",mincost);
- }
- int main()
- {
- int n,m,j,i,k,q,p,c,t,x,y,l;
- int a[MAX];
- scanf("%d",&n);
- while(n--)
- {
- scanf("%d%d%d",&city,&m,&k);
- for(i=1;i<=city;i++)
- {
- for(j=1;j<=city;j++)
- {
- if(i==j)
- map[i][j]=0;
- else
- map[i][j]=INF;
- }
- }
- for(i=1;i<=m;i++)
- {
- scanf("%d%d%d",&p,&q,&c);
- if(map[p][q]>c)
- map[p][q]=map[q][p]=c;
- }
- while(k--)
- {
- scanf("%d",&t);
- for(l=1;l<=t;l++)
- scanf("%d",&a[l]);
- for(i=1;i<t;i++)
- {
- for(j=i+1;j<=t;j++)
- {
- map[a[i]][a[j]]=map[a[j]][a[i]]=0;
- }
- }
- }
- prime();
- }
- return 0;
- }
hdoj 3371 Connect the Cities的更多相关文章
- hdu 3371 Connect the Cities
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=3371 Connect the Cities Description In 2100, since th ...
- HDU 3371 Connect the Cities(prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3371 Problem Description In 2100, since the sea leve ...
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- 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 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 其实就是最小生成树,但是这其中有值得注意的地方:就是重边.题目没有告诉你两个城市之间只有一条路可走, ...
- HDU 3371 Connect the Cities(并查集+Kruskal)
题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...
- POJ:3371 Connect the Cities(最小生成树)
http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
随机推荐
- [杂题]FZU2190 非提的救赎
中文题,题意不多说. 本来感觉很像dp 其实只要从上到下维护单调性就好了 坑是......这个oj......用cin很容易TLE...... //#include <bits/stdc++.h ...
- Shell中调用、引用、包含另一个脚本文件的三种方法
脚本 first (测试示例1) first#!/bin/bashecho 'your are in first file' 方法一:使用source #!/bin/bashecho 'your ar ...
- sqlmap批量扫描burpsuite请求日志记录
sqlmap可以批量扫描包含有request的日志文件,而request日志文件可以通过burpsuite来获取, 因此通过sqlmap结合burpsuite工具,可以更加高效的对应用程序是否存在SQ ...
- python脚本工具 - 4 获取系统当前时间
#! /usr/bin/python import time current_time = time.strftime("%Y-%m-%d %H:%M") print curren ...
- nigix以及相关
nginx+php的配置 php与nginx整合 http://www.thinkphp.cn/topic/13082.html [入门篇]Nginx + FastCGI 程序(C/C++) 搭建高性 ...
- 理解TCP/IP协议
TCP/IP协议是Transmission Control Protocol/Internet Protocol的简写,中译名为传输控制协议/因特网互联协议. 单从TCP/IP协议这个名称看,好多人误 ...
- 常用PHP框架功能对比表
自接触PHP开发以来,已使用了不少框架,虽然对每个框架都没有专研至深,但对每一款所使用的PHP框架功能都有一些了解的.至此,本人将大家常用的一些PHP框架功能整理成表,希望对大家在选择PHP框架时,可 ...
- C# 自定义光标 WaitCursor
一种: 把图像文件放到项目的文件夹中 1 如果图像文件是.cur格式: Cursor cur=new Cursor(文件名); this.cursor=cur; 两句话 就完事 2 如果图像文件是其他 ...
- 画图------Brush
SolidBrush继承自Brush,表示单色的笔刷.Brushes 类则继承自System.Object.它提供了一些标准颜色的画笔,包含静态的只读属性,这些属性返回由属性名指示的颜色的 Brush ...
- 第二部分 MediaPlayer的接口与架构
第二部分 MediaPlayer的接口与架构 2.1 整体框架图 MediaPlayer的各个库之间的结构比较复杂,可以用下图的表示 在各个库中,libmedia.so位于核心 ...