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 机器学习——数据预处理标准化StandardScaler模型

    from sklearn.preprocessing import StandardScaler #数据预处理标准化StandardScaler模型 def test_StandardScaler() ...

  2. 题解【SP1043】 GSS1 - Can you answer these queries I

    题目描述 You are given a sequence \(A_1, A_2, ..., A_n(|A_i|≤15007,1≤N≤50000)\). A query is defined as f ...

  3. go基础_defer

    defer defer是go语言中的关键字 特点:FILO 作用:保障一些申请的资源最终得以释放 func main() { defer fmt.Println("line-1") ...

  4. nginx autoindex 配置目录浏览功能

    Nginx打开目录浏览功能 yum install httpd-tools -y cd /usr/local/openrestry/nginx/conf/ htpasswd -c passwd adm ...

  5. mount命令实际操作样例

    本篇文章主要介绍了如何在Linux(CentOS 7)命令行模式安装VMware Tools,具有一定的参考价值,感兴趣的小伙伴们可以参考一下. 本例中为在Linux(以CentOS 7为例)安装VM ...

  6. Javascript自增、自减运算符

    JavaScript自增.自减运算符与表达式语法 var i++; var-- 声明变量 i-- 变量名 ++ -- 自增运算符 JavaScript自增.自减运算符与表达式 JavaScript自增 ...

  7. 迅速看一下jdk8

    一直在看java并发的感觉说的有点多,就看点简单的放松一下吧!这次来简单说一下jdk8,很久没用,都陌生了,仔细看看还挺有意思的,让我们大脑转化一个角度来写代码:因为我们现在平常大部分用jdk7写代码 ...

  8. JAVA(4)之关于项目部署在tomcat

    关于项目部署的报错问题一直是找不到项目 在重装几次tomcat9和tomcat7后找到了原因,关键原因是访问路径不正确,项目名拼写错误. 排除问题的思路如下(控制变量法) 工作方法和思路 列出步骤,从 ...

  9. 解决<%@taglib prefix="s" uri="/struts-tags"%>显示找不到

    问题: jsp中使用<%@taglib prefix="s" uri="/struts-tags"%>显示找不到 解决方法: 在web.xml中插入 ...

  10. 【题解】Rusty String [CF827E]

    [题解]Rusty String [CF827E] 传送门:\(\text{Rusty String}\) \(\text{[CF827E]}\) [题目描述] 多组数据,每组数据给出一个由 \(V, ...