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. PHP基础学习笔记2

    一.数组 1.1 数值数组 1)自动分配 ID 键 ) ); ?> 即二维数组: 100 100 96 20 60 59 40 110 100 上面创建的二维数组自动分配ID键:下面以指定键的方 ...

  2. LINUX 怎么实现root和普通用户的切换及怎么更改root密码

    在linux系统中执行什么命令后可以使$变为#?     先说下$和#在linux系统终端(命令行)中通常代表的什么:$打头的表示这不是在root用户(管理员用户)下执行的命令#打头的和前者相反,即r ...

  3. 清晰架构(Clean Architecture)的Go微服务: 程序设计

    我使用Go和gRPC创建了一个微服务,并将程序设计和编程的最佳实践应用于该项目. 我写了一系列关于在项目工作中做出的设计决策和取舍的文章,此篇是关于程序设计. 程序的设计遵循清晰架构(Clean Ar ...

  4. Catalyst 6500/6000 Switches ARP or CAM Table

    译:https://www.cisco.com/c/en/us/support/docs/switches/catalyst-6500-series-switches/71079-arp-cam-ta ...

  5. MAC Address-Table Move Update Feature

    MAC Address-Table Move Update The MAC address-table move update feature allows the switch to provide ...

  6. Matlab geom3d函数注释

    Matlab geom3d函数解析 geom3d函数库 geom3d库的目的是处理和可视化三维几何原语,如点.线.平面.多面体等.它提供了操作三维几何原语的底层功能,使得开发更复杂的几何算法变得更加容 ...

  7. Spring的核心api和两种实例化方式

    一.spring的核心api Spring有如下的核心api BeanFactory :这是一个工厂,用于生成任意bean.采取延迟加载,第一次getBean时才会初始化Bean Applicatio ...

  8. 使用axios对安卓或者ios低版本兼容性处理

    原因:不支持ES6,无法使用promise 解决办法: 1.安装 es6-promise cnpm install es6-promise --save-dev 2.引入 es6-promise im ...

  9. 基于G6画个xmind出来

    公司产品因为业务发展,出现了一个新的需求:需要去实现知识库的层级知识展示,展示效果通过树图来实现,具体的展示形式可见下图: 其中有几个需要注意点: 节点上的详情icon可以点击,点击展开关闭详情 节点 ...

  10. Flask - Flask高级技巧(Advanced Flask Patterns)

    传送门 来自作者的PPT https://speakerdeck.com/mitsuhiko/advanced-flask-patterns?slide=46