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 ...
随机推荐
- 使用Docker搭建Spark集群(用于实现网站流量实时分析模块)
上一篇使用Docker搭建了Hadoop的完全分布式:使用Docker搭建Hadoop集群(伪分布式与完全分布式),本次记录搭建spark集群,使用两者同时来实现之前一直未完成的项目:网站日志流量分析 ...
- JAVA(4)之关于项目部署在tomcat
关于项目部署的报错问题一直是找不到项目 在重装几次tomcat9和tomcat7后找到了原因,关键原因是访问路径不正确,项目名拼写错误. 排除问题的思路如下(控制变量法) 工作方法和思路 列出步骤,从 ...
- super this 关键字
super 关键字的三种用法: 1.在子类成员方法中,访问父类成员变量 2.在子类成员方法中,访问父类成员方法 3.在子类构造方法中,访问父类构造方法 this 关键字的三种用法: 1.在本类的成员方 ...
- Yii2.0 引入外部js css
<script src="<?= Yii::$app->request->baseUrl . '/js/jquery-2.1.1.min.js'?>" ...
- LFTP命令笔记
安装 因为在OpenWrt命令行下scp传输文件很慢(只有2.5MB/s不到), 于是改用FTP下载. lftp是OpenWrt下的FTP客户端软件. 如果固件中未安装的话, 需要自己安装, 其依赖于 ...
- SEO 建站过程复盘
今天在A5看到一篇关于一个牛站的复盘,整个建站过程值得借鉴. https://www.admin5.com/article/20200218/943930.shtml
- matplotlib显示AttributeError: 'module' object has no attribute 'verbose'
解决办法:file-settings-tools-python scientific,将show plots in toolwindow前面的对号去掉即可.
- ArrayList、Vector和LinkedList
List接口特点 1.有序的 collection. 2.可以对列表中每个元素的插入位置进行精确地控制. 3.可以根据元素的索引访问元素,并搜索列表中的元素. 4.列表通常允许重复的元素. 5.允许存 ...
- DMVPN基础配置
DMVPN基础拓扑: 配置步骤: 1. 基本IP地址配置实现网络可达 2. 配置GRE多点隧道(mGRE)和NHRP(下一跳解析协议) 3. 配置EIGRP路由协议 4. 配置 ...
- POJ3662 Telephone Lines (dijkstra+二分)
Farmer John wants to set up a telephone line at his farm. Unfortunately, the phone company is uncoop ...