地址: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(最小生成树)的更多相关文章

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

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3371 984ms风险飘过~~~ /************************************ ...

  2. hdu 3371 Connect the Cities (最小生成树Prim)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3371 题目不难 稍微注意一下 要把已经建好的城市之间的花费定义为0,在用普通Prim算法就可以了:我没 ...

  3. HDU 3371 Connect the Cities 最小生成树(和关于sort和qsort的一些小发现)

    解题报告:有n个点,然后有m条可以添加的边,然后有一个k输入,表示一开始已经有k个集合的点,每个集合的点表示现在已经是连通的了. 还是用并查集加克鲁斯卡尔.只是在输入已经连通的集合的时候,通过并查集将 ...

  4. hdu 3371 Connect the Cities

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

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

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

  6. HDU 3371 Connect the Cities(并查集+Kruskal)

    题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=3371 思路: 这道题很明显是一道最小生成树的题目,有点意思的是,它事先已经让几个点联通了.正是因为它先 ...

  7. POJ:3371 Connect the Cities(最小生成树)

    http://acm.hdu.edu.cn/showproblem.php?pid=3371 AC代码: /** /*@author Victor /* C++ */ #include <bit ...

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

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

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

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

随机推荐

  1. 《网络编程》Unix 域套接字

    概述 Unix 域套接字是一种client和server在单主机上的 IPC 方法.Unix 域套接字不运行协议处理,不须要加入或删除网络报头,无需验证和,不产生顺序号,无需发送确认报文,比因特网域套 ...

  2. ASP.NET页面之间数据传递的几种方法

    1)Request.QueryString   在ASP时代,这个是较常用的方法,到了ASP.NET,好像用的人不多了,但是不管怎么说,这是一个没有过时,且很值得推荐的方法,因为不管是ASP还是ASP ...

  3. mac的svn之cornerstone简易教程

    链接地址:http://jingyan.baidu.com/article/9989c74612a55af648ecfef2.html 背景: 关于cornerstone的介绍很少: 这里介绍mac的 ...

  4. iOS 图标、图形尺寸? iPhone、iPad、 iPod touch

    链接地址:http://www.zhihu.com/question/20248971 著作权归作者所有.商业转载请联系作者获得授权,非商业转载请注明出处.作者:刘剑链接:http://www.zhi ...

  5. .net format 中 大括号{}处理

    1.string string.format(string format,object arg0) 错误:因为方法中使用{n}做占位符号了,所以其他需要括号{}的地方,就需要{{}}

  6. 05-C语言运算符

    目录: 一.进制转换 二.常量 三.sizeof 四.运算符 五.赋值运算符 六.自增减运算符 七.关系运算符 八.逻辑运算符 九.取址寻址运算符 回到顶部 一.进制转换 1 进制转换是人们利用符号来 ...

  7. 数组length属性的一些特性

    ~~·数组的length属性是可读写的 var colors = ["blue","red","green"];colors.length ...

  8. centos6.5 搭建php5.5+mysql5.5+apache2.4

    本文总结了Linux下 root.常用查找命令.卸载软件方法(见二.安装PHP5.5).配置软件源(见二).安装软件(见二)与高版本替换软件(见三.安装MySQL)的方法. 迁移网站,机器上原本已有p ...

  9. Objective-c中@interface、@implementation、@protocal

    以下 void print(); }; class AC{ }; 这时候,AI和AC是独立存在,AC不会因为没有和AI建立关系而编译错误,将AC做以下修改后,AI才和AC建立了关系,AC必须实现AI中 ...

  10. Android滑动事件冲突

    首先,我们假设这样一个场景:一个ViewPager里面嵌套一个ViewPager,内部滑动方向和外部滑动方向一样时,该怎么解决这一冲突呢? 针对滑动冲突这里给出两种解决方案:外部拦截法,内部拦截法. ...