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 ...
随机推荐
- Android 手势锁的实现 为了让自己的应用程序的安全,现在
转载请注明出处:http://blog.csdn.net/lmj623565791/article/details/36236113 今天偶遇以github上gesturelock关于手势锁的一个样例 ...
- std::map中函数用法集合
1 STL的map表里有一个erase方法用来从一个map中删除掉指令的节点 2 eg: 3 map<string,string> mapTest; 4 typedef map< ...
- 2.一步一步学c#(二):核心c#
简单的控制台应用程序. 代码(它有把某条消息写到屏幕上的一个类组成) 编译并运行程序得到结果: 在上面的代码示例中,前几行代码与名称空间有关,名称空间是把相关类组合在一起的方式.namespace关键 ...
- [01] Preparation - Sitecore Installment
Sitecore CMS 是一套内容管理系统商业软件,其底层平台依托于微软.net技术.由于最近的一个项目采用了这个平台,所以有机会接触到了这个产品. 虽然接触该产品已有一段时间,但总感觉对这个产品缺 ...
- HDU1171:Big Event in HDU(多重背包分析)
通过分析,要使A>=B并且差值最小.所以只要使sum/2的容量下,B最大就Ok了 #include<iostream> #include<cstdio> #include ...
- Linux系统之UpStart
子贡问为仁.子曰:“工欲善其事,必先利其器.居是邦也,事其大夫之贤者,友其士之仁者.”——孔子(春秋)<论语·卫灵公> [工欲善其事,必先利其器] 掌握一门技术,知道其发展历程是非常重要的 ...
- (Problem 53)Combinatoric selections
There are exactly ten ways of selecting three from five, 12345: 123, 124, 125, 134, 135, 145, 234, 2 ...
- Webx pull service
1.概述 pull service的功能是将对象置入模板中.被pull service放到模板中的对象,不需要应用程序的干预即可直接使用.如果模板没有用到某个对象,则不会产生创建该对象的开销.看起来, ...
- LintCode-字符串查找
题目描述: 对于一个给定的 source 字符串和一个 target 字符串,你应该在 source 字符串中找出 target 字符串出现的第一个位置(从0开始).如果不存在,则返回 -1. 说明 ...
- ubutu下的几个命令
nginx重启命令 (ps:意为将nginx重启) /usr/local/nginx/sbin/nginx -s reload 给new目录权限设置为777 (ps:意思为将wwwroot/new目录 ...