最大生成树+map实现技巧
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<vector>
#include<cstring>
#include<map>
#include<set>
#include<queue>
#include<bitset>
#include<utility>
#include<functional>
#include<iomanip>
#include<sstream>
#include<ctime>
#include<cassert>
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define pw(x) (1ll << (x))
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
#define rep(i,l,r) for(int i=(l);i<(r);i++)
#define per(i,r,l) for(int i=(r);i>=(l);i--)
#define FOR(i,l,r) for(int i=(l);i<=(r);i++)
#define debug1(a) cout << #a << " = " << a << endl;
#define debug2(a,b) cout << #a << " = " << a << endl;\
cout << #b << " = " << b << endl;
#define debug3(a,b,c) cout << #a << " = " << a << endl;\
cout << #b << " = " << b << endl;\
cout << #c << " = " << c << endl;
#define debug4(a,b,c,d)\
cout << #a << " = " << a << endl;\
cout << #b << " = " << b << endl;\
cout << #c << " = " << c << endl;\
cout << #d << " = " << d << endl;
#define eps 1e-9
#define PIE acos(-1)
#define cl(a,b) memset(a,b,sizeof(a))
#define fastio ios::sync_with_stdio(false);cin.tie(0);
#define lson l , mid , ls
#define rson mid + 1 , r , rs
#define ls (rt<<1)
#define rs (ls|1)
#define INF 0x3f3f3f3f
#define lowbit(x) (x&(-x))
#define sqr(a) a*a
#pragma GCC optimize(2)
using namespace std;
typedef double db;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> pii; const int maxn=2e5;
int u[maxn],v[maxn],w[maxn],p[maxn],r[maxn];
int n,m,cnt;
map<string,int>MAP; int cmp(const int i,const int j){return w[i]>w[j];}
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
int kruscal(int t1,int t2)
{
int ans=;
rep(i,,maxn)p[i]=r[i]=i;
sort(r,r+m,cmp);
rep(i,,m){
int e=r[i];
int x=find(u[e]);
int y=find(v[e]);
if(x!=y){
p[x]=y;
if(find(t1)==find(t2)){
ans=w[e];
return ans;
}
}
}
return w[r[m]];
}
void Init()
{
MAP.clear();
cnt=;
}
int main()
{
int cas=;
while(cin>>n>>m,n&&m){
Init();
char s1[],s2[];
rep(i,,m){
scanf("%s%s%d",s1,s2,&w[i]);
if(!MAP.count(s1))MAP[s1]=cnt++;
if(!MAP.count(s2))MAP[s2]=cnt++;
u[i]=MAP[s1];
v[i]=MAP[s2];
// debug3(MAP[s1],MAP[s2],w[i]);
}
scanf("%s%s",s1,s2);
printf("Scenario #%d\n", ++cas);
printf("%d tons\n\n", kruscal(MAP[s1],MAP[s2]));
// printf("Scenario #%d\n%d tons\n\n",++cas,kruscal(MAP[s1],MAP[s2]));
}
return ;
}
最大生成树+map实现技巧的更多相关文章
- map,实现技巧,id
cf #include<iostream> #include<cstdio> #include<algorithm> #include<vector> ...
- 总结golang之map
总结golang之map 2017年04月13日 23:35:53 趁年轻造起来 阅读数:18637 标签: golangmapgo 更多 个人分类: golang 版权声明:本文为博主原创文章, ...
- Go语言语法说明
Go语言语法说明 go语言中的go func(){}() 表示以并发的方式调用匿名函数func 深入讲解Go语言中函数new与make的使用和区别 前言 本文主要给大家介绍了Go语言中函数new与ma ...
- 用golang开发系统软件的一些细节
用golang开发系统软件的一些细节 作者:张富春(ahfuzhang),转载时请注明作者和引用链接,谢谢! cnblogs博客 zhihu Github 公众号:一本正经的瞎扯 (本文的pdf版本) ...
- 调试技巧 —— 如何利用windbg + dump + map分析程序异常
调试技巧 —— 如何利用windbg + dump + map分析程序异常 逗比汪星人2011-09-04上传 调试技巧 —— 如何利用windbg + dump + map分析程序异常 http ...
- 【转】Java学习---Java核心数据结构(List,Map,Set)使用技巧与优化
[原文]https://www.toutiao.com/i6594587397101453827/ Java核心数据结构(List,Map,Set)使用技巧与优化 JDK提供了一组主要的数据结构实现, ...
- poj 3320 技巧/尺取法 map标记
Description Jessica's a very lovely girl wooed by lots of boys. Recently she has a problem. The fina ...
- C++STL中map容器的说明和使用技巧(杂谈)
1.map简介 map是一类关联式容器.它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响.对于迭代器来说,可以修改实值,而不能修改key. 2.map的功能 自 ...
- Java核心数据结构(List,Map,Set)原理与使用技巧
JDK提供了一组主要的数据结构实现,如List.Map.Set等常用数据结构.这些数据都继承自 java.util.Collection 接口,并位于 java.util 包内. 1.List接口 最 ...
随机推荐
- [转载]SSD原理与实现
[转载]SSD原理与实现 这里只mark一下,对原论文讲解的很好的博文 https://zhuanlan.zhihu.com/p/33544892 这里有一个关于SSD的很好的程序实现,readme里 ...
- java引用传递,值传递
2个interger的引用对象传给一个swap方法在方法内部进行交换 1.1 java中方法参数传值方式 java中方法传参数都是值传递的,只不过根据参数的类型是引用类型还是非引用类型 引用类型传递的 ...
- java集合之hashMap,初始长度,高并发死锁,java8 hashMap做的性能提升
众所周知,HashMap是一个用于存储Key-Value键值对的集合,每一个键值对也叫做Entry.这些个键值对(Entry)分散存储在一个数组当中,这个数组就是HashMap的主干. HashMap ...
- Delphi 类类型
- Physical Education Lessons CodeForces - 915E (动态开点线段树)
Physical Education Lessons CodeForces - 915E This year Alex has finished school, and now he is a fir ...
- 单元测试框架之unittest(一)
一.单元测试的含义 unittest单元测试框架的设计灵感来源于Junit(Java语言的单元测试框架),它与其他语言的单元测试框架风格相类似,支持自动化测试.为测试共享setUp和shutDown. ...
- Vue学习日记(三)——Vue路由管理vue-router
前言 为了给读者更好的体验,去理解vue-router和下一篇介绍vuex,决定还是来一个实战教程来带大家更加的去深入理解vue-router,在这之前,读者先自行了解和去官网下载npm和node,附 ...
- indexedDB 前端数据库(使用的简单案例)
前端存储 之 indexDB 1.indexedDB是什么? indexedDB是一个非关系型数据库 它不需要我们去写一些特定的SQL语句来对数据库进行操作 它是NoSQL的,数据形式使用的json ...
- SQL拼串--小问题大毛病
用字符串拼接SQL串实现动态SQL语句,经常使用. 不过在拼接SQL串中有许多技巧和小陷阱 1.遗漏""和'' 错误案例: 错误结果: 发现没有数据产生..我还以为是连接错了数据库 ...
- 5、docker容器数据卷: -v添加共享传递容器数据卷
1.是什么 1.docker理念 先来看看Docker的理念:* 将运用与运行的环境打包形成容器运行 ,运行可以伴随着容器,但是我们对数据的要求希望是持久化的* 容器之间希望有可能共享数据 2.保 ...