POJ:1251 Jungle Roads
来源:互联网 发布:威纶触摸屏软件下载 编辑:程序博客网 时间:2024/04/30 22:05
题意:给你一些村庄和到相连村庄的距离,让你求最小连通所有村庄的路径长度。
思路:最小生成树。
我在这里用map建立了一个村庄名到连续数字的映射,由于映射对应关系,这样就需要连续数字从1开始。
#include <cstdio>#include <map>#include <string>#include <iostream>#include <cstring>#include <algorithm>#define MAXN 1000using namespace std;struct Edge{ int a,b,weight;};int father[MAXN];map<string,int> mp;int find(int p){ return p==father[p]?p:(father[p]=find(father[p]));}bool cmp(Edge x,Edge y){ return x.weight<y.weight;}int main(){ int n; while(scanf("%d",&n)&&n) { mp.clear(); memset(father,0,sizeof(father)); int nn=1,N=0; Edge p[MAXN]; for(int i=0; i<n-1; ++i) { string str,tmp; int m,val; cin>>str>>m; if(!mp[str]) mp[str]=nn++; for(int j=0; j<m; ++j) { cin>>tmp>>val; if(!mp[tmp]) mp[tmp]=nn++; p[N].a=mp[str]; p[N].b=mp[tmp]; p[N++].weight=val; } } for(int i=0;i<=nn;++i) father[i]=i; sort(p,p+N,cmp); int ans=0; for(int i=0;i<N;++i) { int ta=find(p[i].a),tb=find(p[i].b); if(ta!=tb) { father[ta]=tb; ans+=p[i].weight; } } printf("%d\n",ans); } return 0;}
- POJ 1251 Jungle Roads
- poj 1251 Jungle Roads
- poj 1251 Jungle Roads
- POJ-1251 jungle roads
- poj 1251 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- Poj 1251 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- POJ 1251 Jungle Roads
- POJ-1251-Jungle Roads
- POJ 1251 Jungle Roads
- POJ:1251 Jungle Roads
- POJ 1251 Jungle Roads
- POJ 1251 Jungle Roads
- POJ-1251 Jungle Roads
- poj 1251 Jungle Roads
- 排队找零问题
- 接口使用时注意
- android设备的内存空间(RAM)总空间和可用空间大小的获取以及一些思考
- Axure RP Pro 注册码
- 如何正确删除归档日志
- POJ:1251 Jungle Roads
- 一组老照片的解读
- Java中对象序列化的作用是什么
- The type SmsMessage is deprecated
- 有符号数与无符号数的上溢与下溢问题
- 解决mac os下mcss命令报错:env: node\r: No such file or directory
- OpenCV C++ 打开笔记本摄像头出现错误
- 斯坦福大学公开课《编程方法学》之笔记一
- 慢慢爬的蜗牛,新的起点