PAT T1024 Currency Exchange Centers
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的更多相关文章
- POJ1860 Currency Exchange(bellman-ford)
链接:http://poj.org/problem?id=1860 Currency Exchange Description Several currency exchange points are ...
- 图论 --- spfa + 链式向前星 : 判断是否存在正权回路 poj 1860 : Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 19881 Accepted: 711 ...
- Currency Exchange(Bellman-ford)
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 21349 Accepted: 765 ...
- poj1860 bellman—ford队列优化 Currency Exchange
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22123 Accepted: 799 ...
- 最短路(Bellman_Ford) POJ 1860 Currency Exchange
题目传送门 /* 最短路(Bellman_Ford):求负环的思路,但是反过来用,即找正环 详细解释:http://blog.csdn.net/lyy289065406/article/details ...
- Currency Exchange 分类: POJ 2015-07-14 16:20 10人阅读 评论(0) 收藏
Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 22180 Accepted: 8015 De ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit : 2000/1000ms (Java/Other) Memory Limit : 60000/30000K (Java/Other) T ...
- poj 1860 Currency Exchange :bellman-ford
点击打开链接 Currency Exchange Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 16635 Accept ...
- POJ 1860 Currency Exchange (最短路)
Currency Exchange Time Limit:1000MS Memory Limit:30000KB 64bit IO Format:%I64d & %I64u S ...
随机推荐
- 09day 命令提示符优化及yum优化
export PS1='\[\e[32;1m\][\u@\h \W]\$ \[\e[0m\]' 设置颜色 内容 结束 export PS1='\[\e[30;1m\][\u@\h \W]\$ \[\e ...
- Bug搬运工-CSCvg37458:ISR4K goes into booting loop with "flash:" in boot statement
ISR4K升级的时候要注意了! 很可能会碰到如下的问题: ISR4K goes into booting loop with "flash:" in boot statement ...
- SpringMVC的三种处理器适配器
SpringMVC具有三种处理器适配器,他们分别是BeanNameUrlHandlerMapping.SimpleControllerHandlerAdapter.ControllerClassNam ...
- Reinforcement Learning Algorithm 资源
算法源码: https://github.com/ljpzzz/machinelearning https://github.com/imraviagrawal/Reinforcement-Learn ...
- python之 '随机'
Q:想生成随机数,用哪个库? import random Q:想生成一个随机整数,范围在[0, 100]之内,怎么弄? >>> random.randint(0, 100) 7 Q: ...
- CSS学习(9)块盒模型应用
1.改变宽高范围 默认情况下,width和height设置的是内容盒的宽高 页面重构师:将psd文件(设计稿)制作为静态页面 衡量设计稿尺寸的时候,往往使用的是边框盒 CSS3中 box-sizing ...
- oss创建文件夹
在进行putObject时,第二个参数写成path/your-object即可
- java爬虫出现java.lang.IllegalArgumentException: Illegal character in path at index 31
url地址中出现了空格,使用trim()函数去除空格就好了
- JavaScript可枚举的属性
/* 把P中的可枚举属性复制到o中,并返回o中 如果o和p中含有同名的属性,则覆盖O中的属性 这个函数并不处理getter和setter以及复制属性 */ function extend(o,p){ ...
- 自编C++游戏
背景 周末无聊,于是编了一个类似于cmd的小玩意. 可是越想越不对劲,所以把它改成了一个小游戏. 信息 语言:DEV_C++ 源代码已公布!! 打败GG 版本:1.0(正式版) 版本:1.5(番外) ...