POJ 2485 Highways
2015-07-08 08:25
169 查看
意甲冠军:给你一个数n,代表n村庄,然后你进入到n行n列数,文章i第一线j代表元素i村庄和j从村,要确定您的沟通n维修所需的村庄最短的最大边
思路:用Kruskal算法求
AC代码:
思路:用Kruskal算法求
AC代码:
#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; #define N 125000 //最多的边数为n*(n-1)/2 int u ,v ,w ,r ; int f[520],str[520][520]; int find(int x) { if(x!=f[x]) f[x]=find(f[x]); return f[x]; } int cmp(const int x,const int y) //间接排序距离 { return w[x]<w[y]; } int main() { int x,y,sum,e,i,j,n,t,maxx; scanf("%d",&t); while(t--) { scanf("%d",&n); int cnt=0; memset(str,0,sizeof(str)); for(i=1;i<=n;i++) for(j=1;j<=n;j++) { int val; scanf("%d",&val); if(val==0) continue; if(!(str[i][j]&&str[j][i])) { u[cnt]=i; v[cnt]=j; w[cnt++]=val; str[i][j]=str[j][i]=1; } } for(i=0;i<=n;i++)f[i]=i; for(i=0;i<=cnt;i++)r[i]=i; sort(r,r+cnt,cmp); sum=0; maxx=0; for(i=0;i<=cnt;i++) { e=r[i]; x=find(u[e]); y=find(v[e]); if(x!=y){ maxx=max(maxx,w[e]); f[x]=y; } } printf("%d\n",maxx); } }
相关文章推荐
- 关于js with语句的一些理解
- 0708学习 选项卡切换
- Win10 Server预览版10147大量截图曝光
- 碎片化时代的软广告服务有哪些?
- java序列化
- 演讲感触
- druid 数据源密码加密配置
- tuple demo
- Win10 Mobile预览版10149慢速版正式推送
- 如何解决KEIL 5 编KEIL4同RTX系统的project解
- 全新E:网站不是之前排名浮动 相比于竞争对手究竟缺少了什么?
- Windows 10 10149手机预览版在慢车道开始推送
- 7.7模拟总结
- Csharp学习第二天
- 【剑指Offer学习】【面试题44:扑克牌的顺子】
- maven常用配置
- Android.os.SystemClock
- 为什么事务日志自动增长会降低你的性能
- C#第五次作业:开源项目“网页正文提取”的理解
- CentOS系统时间与网络同步