Hdu 3371 Connect the Cities(最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=3371
其实就是最小生成树,但是这其中有值得注意的地方:就是重边。题目没有告诉你两个城市之间只有一条路可走,所以两个城市之间可能有多条路可以走。
举例: 输入可以包含 1 2 3 // 1到2的成本为3
1 2 5 //1到2的成本为5
因此此时应选成本较低的路。
然后,已经连通的城市之间的连通成本为0。
这题用G++提交得到984ms的反馈,用C++提交则得到484ms的反馈。
很想知道这个时间差是怎么回事。
另外,这题一定还可以优化。
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace std; const int maxn = 500+10;
int dis[maxn][maxn]; //存两地间的距离
bool vis[maxn]; //是否已经加入树
int temp[maxn];
int shortP[maxn];
const int INF = 65523655;
int n,m,k; inline void input() //初始及输入
{
cin>>n>>m>>k;
int i,j;
for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
dis[i][j]=INF;
int p,q,r;
while(m--)
{
scanf("%d%d%d",&p,&q,&r);
if( r<dis[p][q] ) //这一句保证存入的是两地间距离的最小值
dis[p][q]=dis[q][p]=r;
}
int t;
memset(temp,0,sizeof(temp));
while(k--)
{
scanf("%d",&t);
for(i=1;i<=t;i++)
scanf("%d",&temp[i]);
for(i=1;i<=t;i++)
for(j=i+1;j<=t;j++)
dis[ temp[i] ][ temp[j] ]=dis[ temp[j] ][ temp[i] ]=0;
}
memset(vis,false,sizeof(vis));
for(i=1;i<=n;i++)
shortP[i]=INF;
} inline int span() //求最低成本
{
int cost=0;
int pos=1,k;
int count=0;
int i,minC=INF;
vis[1]=true;
while(count<n-1) //判断最小生成树是否已经完成
{
for(i=1;i<=n;i++)
if( !vis[i] && shortP[i] > dis[pos][i] )
shortP[i]=dis[pos][i];
k=-1;
minC=INF;
for(i=1;i<=n;i++)
if( !vis[i] && minC > shortP[i] )
{
minC=shortP[i];
k=i;
}
if(k==-1)
return -1; //判断是否还可以再把一个城市加进来
cost += shortP[k];
vis[k]=true;
pos=k;
count++;
}
return cost;
} int main()
{
int T;
while(cin>>T)
{
int flag;
while(T--)
{
input();
flag = span();
cout<<flag<<endl;
}
}
return 0;
}
Hdu 3371 Connect the Cities(最小生成树)的更多相关文章
- hdu 3371 Connect the Cities(最小生成树)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...
- hdu 3371 Connect the Cities (最小生成树Prim)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...
- HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)
解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...
- 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(并查集+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 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 ...
随机推荐
- 解决word转pdf后图片失真
碰到问题: 将word转pdf后图片出现失真 问题分析: 上述问题必定跟图片类型和所用软件有关,现将不同图片在不同软件下的失真情况汇总,见表1 问题解决:迫不得已,不要使用截图:若必需要用,则word ...
- WPF datagrid 如何隔行变色
<DataGrid AlternationCount="2"> <DataGrid.RowStyle> <Style TargetType=" ...
- select标签操作大全
http://blog.csdn.net/hhhh2012/article/details/8610336
- 解决python2.7.9以下版本requests访问https的问题
在python2.7.9以下版本requests访问https连接后,总会报一些关于SSL warning. 解决法子可以参考:https://urllib3.readthedocs.io/en/la ...
- 帝国cms7.2灵动标签万能教程
学完本文,就完全能掌握帝国模板开发制作啦!这里只介绍sql语句调用方法(方便,快捷!) 灵动标签语法: [e:loop={,24,0}] 模板内容 [/e:loop] 详细解释:黄色部分:条件语句,即 ...
- Spring Boot 属性配置和使用
Spring Boot 属性配置和使用 Spring Boot 允许通过外部配置让你在不同的环境使用同一应用程序的代码,简单说就是可以通过配置文件来注入属性或者修改默认的配置. Spring Boot ...
- elk 添加节点
elk 添加节点: cluster.name: es_cluster node.name: node03 path.data: /elk/elasticsearch/data path.logs: / ...
- 字符串模式匹配KMP算法
一篇不错的博客:http://www.cnblogs.com/dolphin0520/archive/2011/08/24/2151846.html KMP字符串模式匹配通俗点说就是一种在一个字符串中 ...
- 带你轻松玩转Git--图解三区结构
在上篇文章的结尾我们提到了Git 的三区结构,在版本控制体系中有这样两种体系结构,一种是两区结构一种是三区结构.接下来我们通过对Git三区的结构学习来帮助我们更好的去理解并运用Git. 两区结构是其他 ...
- hdu4336 Card Collector 状态压缩dp
Card Collector Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Tota ...