krustral算法求最少结点数的最小生成树,用优先队列实时排序,优先选择已经被选中的中心~

#include<bits/stdc++.h>
using namespace std;
const int maxn=;
int N,M,x,y;
struct edge {
string name;
int u;
int v;
int w;
}Edge[maxn];
int father[maxn];
int num[maxn];
vector<edge> vi;
unordered_set<string> st;
int findfather (int x) {
int a=x;
while (x!=father[x]) x=father[x];
while (a!=father[a]) {
int z=a;
a=father[a];
father[z]=x;
}
return x;
}
bool cmp1 (edge a,edge b) {
if (a.w!=b.w) return a.w<b.w;
else return st.count(a.name);
}
bool cmp2 (edge a,edge b) {
if (a.name!=b.name) return a.name<b.name;
else return a.w<b.w;
}
bool operator < (edge a,edge b) {
if (a.w!=b.w) return a.w>b.w;
else return !st.count(a.name);
}
priority_queue<edge> q;
int main () {
scanf ("%d %d",&N,&M);
for (int i=;i<N;i++) father[i]=i,num[i]=;
for (int i=;i<M;i++) {
cin>>Edge[i].u>>Edge[i].v>>Edge[i].name>>Edge[i].w;
q.push(Edge[i]);
}
sort (Edge,Edge+M,cmp1);
int ans=;
while (!q.empty()) {
edge now=q.top();
q.pop();
int u=now.u;
int v=now.v;
int faU=findfather(u);
int faV=findfather(v);
if (faU!=faV) {
father[faU]=faV;
num[faV]+=num[faU];
ans+=now.w;
vi.push_back(now);
st.insert(now.name);
}
}
sort (vi.begin(),vi.end(),cmp2);
printf ("%d %d\n",st.size(),ans);
for (int i=;i<vi.size();i++) {
printf ("%d %d ",vi[i].u,vi[i].v);
cout<<vi[i].name;
printf (" %d\n",vi[i].w);
}
return ;
}

PAT T1024 Currency Exchange Centers的更多相关文章

  1. POJ1860 Currency Exchange(bellman-ford)

    链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...

  2. 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 19881   Accepted: 711 ...

  3. Currency Exchange(Bellman-ford)

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 21349   Accepted: 765 ...

  4. poj1860 bellman—ford队列优化 Currency Exchange

    Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 22123   Accepted: 799 ...

  5. 最短路(Bellman_Ford) POJ 1860 Currency Exchange

    题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...

  6. Currency Exchange 分类: POJ 2015-07-14 16:20 10人阅读 评论(0) 收藏

    Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22180 Accepted: 8015 De ...

  7. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 60000/30000K (Java/Other) T ...

  8. poj 1860 Currency Exchange :bellman-ford

    点击打开链接 Currency Exchange Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 16635   Accept ...

  9. POJ 1860 Currency Exchange (最短路)

    Currency Exchange Time Limit:1000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u S ...

随机推荐

  1. Python类属性和类方法

    01. 类的结构 1.1 术语 —— 实例 使用面相对象开发,第 1 步 是设计 类 使用 类名() 创建对象,创建对象 的动作有两步: 1) 在内存中为对象 分配空间 2) 调用初始化方法 __in ...

  2. 论STA | POCV/SOCV 对lib 的要求 (4)

    在芯片制造过程中的工艺偏差由global variation 和local variation 两部分组成. 在集成电路设计实现中,global variation 用PVT 跟 RC-corner ...

  3. 开源协议:LGPL协议、OSGi协议

    本文介绍开源的协议. LGPL 是 GNU Lesser General Public License (GNU 宽通用公共许可证)的缩写形式,旧称 GNU Library General Publi ...

  4. JAVA 爬虫框架webmagic 初步使用Demo

    一想到做爬虫大家第一个想到的语言一定是python,毕竟python比方便,而且最近也非常的火爆,但是python有一个全局锁的概念新能有瓶颈,所以用java还是比较牛逼的, webmagic 官网 ...

  5. 一周搞定模拟电路_P5_基本放电电路记录

    1.原始基本放大电路 2.改变Rb,Rb由200kΩ变为100kΩ 3 4

  6. 解决VMware Workstation下Win2012R2无法安装Hyper-v问题

    有时候我们需要测试Hyper-V但是发现VMware下不能够正常安装,提示:验证过程发现你要安装功能的服务器存在问题.所选功能与所选服务器的当前配置不兼容.无法安装Hyper-V:虚拟机监控程序已在运 ...

  7. [Bug合集] java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent

    出现场景: 在一个Button中定义了onclick属性,值为startChat. 在Activity中定义一个方法. public void startChat(View view){} 运行时,点 ...

  8. win10安装 .net3.5失败解决方法

    #开始 最近需要学习Sql Server 但是发现SQL Server2008r2 版本的安装程序基于.net 电脑没有安装.net3.5 #解决过程 可笑的是我在用离线安装包安装.net3.5的时候 ...

  9. win server 挂载

    新建服务器角色,选择[NFS服务器]. mount -o nolock \\x.x.x.x.x.x\! z:/*链接到*/

  10. 洛谷 P5018 对称二叉树(搜索)

    嗯... 题目链接:https://www.luogu.org/problem/P5018 其实这道题直接搜索就可以搜满分: 首先递归把每个点作为根节点的儿子的数量初始化出来,然后看这个节点作为根节点 ...