POJ 1251 Jungle Roads
来源:互联网 发布:知道具体位置的软件 编辑:程序博客网 时间:2024/05/20 02:24
依然是裸的一个最小生成树 输入稍有些麻烦
#include <iostream>#include <cstring>#include <string>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <queue>#include <map>#include<set>#include<stack>#define bug puts("bugbugbug");using namespace std;typedef long long ll;const int maxm=1000005;int f[1005];int n,m;struct node{ int x, y, v; bool operator<(const node &b)const { if(this->v!=b.v) return (this->v<b.v); if(this->x!=b.x) return (this->x<b.x); return (this->y<b.y); }} e[maxm];int find(int x){ return f[x]= f[x]==x?x:find(f[x]);}int Kruscal(){ sort(e, e + m); int cnt = n, ans = 0; for(int i = 0; i <= n; i++) f[i] = i; for (int i = 0; i < m; i++) { int x = find(e[i].x); int y = find(e[i].y); if (x != y) { f[x] = y; cnt--; ans+= e[i].v; if (cnt == 1) break; } } if(cnt!=1)return -1; return ans;}int main(){ while(scanf("%d",&n)&&n) { m=0; for(int i=0; i<n-1; i++) { char a[10]; int b; scanf("%s%d",a,&b); int x=a[0]-'A'; for(int j=0;j<b;j++) { int c; scanf("%s%d",a,&c); e[m].x=x; e[m].y=a[0]-'A'; e[m++].v=c; } } printf("%d\n",Kruscal()); }}
0 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
- 75 thinkphp 关联模型
- C#调用RESTful API
- springmvc的数据校验
- opencv waitkey使用注意点
- 士兵队列训练问题
- POJ 1251 Jungle Roads
- gstreamer 安装ffmpeg插件
- Caffe Layer结构
- android kl文件
- Linux操作系统-标准IO库(3)
- MyEclipse10 安装
- 夏季“吃苦”应量力而行
- Android控件详解之标签控件
- jQuery ajax jsonp实现跨域请求