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 ...
随机推荐
- cuda核函数再调用核函数,多层并行
#include <stdio.h> __global__ void childKernel(int i) { int tid = blockIdx.x*blockDim.x+thread ...
- Gridview编辑时Jquery自动计算自定义列(鼠标离开输入框Jquery计算)
Jquery片段: <script type="text/javascript"> function compute(nbBoxQuantity, lblQuantit ...
- Objective-c 内存管理
与 C 有一点类似,oc 需要使用 alloc 方法申请内存.不同的是,c 直接调用 free 函数来释放内存,而 oc 并不直接调用 dealloc 来释放.整个 oc 都使用对象引用,而且每一 ...
- C++类包含问题(重复包含和相互包含)
一.重复包含头文件 头文件重复包含,可能会导致的错误包括:变量重定义,类型重定义及其他一些莫名其妙的错误.C++提供两种解决方案,分别是#ifndef和#pragma once #ifndef _SO ...
- c风格字符串函数
十一.C 风格字符串 1)字符串操作 strcpy(p, p1) 复制字符串 strncpy(p, p1, n) 复制指定长度字符串 strcat(p, p1) 附加字符串 strncat( ...
- HDU 2108 Shape of HDU
题解:按照输入顺序依次将点连接起来,对于连续的三个点p0,p1,p2,令向量a=p1-p0,b=p2-p1 若是凸多边形,那么b相对于a一定是向逆时针方向旋转的 判断两向量的旋转方向,可以使用向量的叉 ...
- 怎么取消 Windows Server 2012 RDP 限制每个用户只能进行一个会话
在 Windows Server 2008 / 2008 R2 上,如果希望多个远程用户使用同一个账号同时访问服务器的 Remote Desktop(RDP),只需通过管理工具-远程桌面下的“远程桌面 ...
- JS获取DropDownList的value值与text值
<script type="text/javascript" language="javascript"> function SearchChang ...
- 本地存储sessionStorage 、 localStorage 、cookie整理
sessionStorage . localStorage .cookie 的区别 sessionStorage 和 localStorage 是HTML5 Web Storage API 提供的,可 ...
- ajax 基础
<html><head><script type="text/javascript">function showHint(str){var xm ...