# dijikistra模板

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
#define el "\n";
#define INF 0x3f3f3f3f
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
const int Max = 1e4 + 3;

int n = 2021;

int gcd(int a,int b)
{
return b == 0 ? a : gcd(b, a % b);
}

int get_minG(int a, int b)
{
return a*b/gcd(a,b);
}

int dis[3000];
int vis[3000];
vector<pair<int,int>> g[3000];
queue<int> q;
int dijikistra()
{
memset(dis, 0x3f, sizeof(dis));
//初始化边
rep(i, 1, n)
{
rep(j, 1, n)
{
if (abs(i - j) <= 21)
{
g[i].push_back({ j,(get_minG(i,j)) });
}
}
}

dis[1] = 0;
q.push(1);
while(!q.empty())
{//计算更新邻接表
int v = q.front();
q.pop();
for (auto it : g[v])
{
int u = it.first;
int cost = it.second;
if (dis[u] > dis[v] + cost)
{//更新路径
dis[u] = dis[v] + cost;
if (!vis[u])
{
vis[u] = 1;
q.push(u);
}
}
}
}

return dis[2021];

}
int main()
{
cin.tie(0);
cout.tie(0);

cout<<dijikistra();

return 0;
}

# 并查集模板

#include<bits/stdc++.h>
using namespace std;
const char el = '\n';
typedef long long ll;
#define rep(i,a,b) for (int i = (a);i <= (b);i++)
const int N = 2e5 + 10;

int c[30200];//花费
int fa[30200];//父亲

int find(int x)
{
return fa[x] = (fa[x]==x) ? x : find(fa[x]);
}

int main() {
cin.tie(0);
cout.tie(0);

/*
n 表示通知给每个人的花费
m 行给出
人数 学生1 2 3 4
*/
int n , m ;
cin >> n >>m;
rep(i,1,n)
{
cin>>c[i];
fa[i] = i;
}

rep(i,1,m)
{
int x , first,second;
cin >> x;
cin >> first;
int fa1,fa2;
rep(i,2,x)
{
cin >> second;
fa1 = find(first);
fa2 = find(second);
fa[fa1] = fa2;
}
}

set<int> se;
//最小花销
rep(i,1,n)
{
int f = find(i);//找到最终父节点
if(se.find(f) == se.end())
{
se.insert(f);
}
if(c[f] > c[i])
{
c[f] = c[i];
}
}

int ans = 0;
for(auto it : se)
{
ans+=c[it];
}

cout<<ans<<el;
return 0;
}

# 欧几里得

int gcd(int a,int b)
{
return b==0?a:gcd(b,a%b);
}

# 快速幂

int mypow(int n)
{
if (n == 0)
{
return 1;
}
if (n == 1)
{
return 2;
}
int r = 1;
if (n % 2 == 1)
{
n--;
r = 2;
}
return mypow(n / 2) * mypow(n / 2)*r;
}

# 各种STL库用迭代器遍历

#include<bits/stdc++.h>
using namespace std;

int main() {
//map 遍历
map<int,int> mp;
mp[1] = 2;

for(map<int,int>::iterator it = mp.begin();it!=mp.end();it++)
{
cout<<it->first<<el;
cout<<it->second<<el;
}
//set
set<int> se;
se.insert(3);
for(set<int>::iterator it = se.begin();it!=se.end();it++)
{
cout<<*it<<el;
}
//vector
vector<int> ve;
ve.push_back(4);
for(vector<int>::iterator it = ve.begin();it!=ve.end();it++)
{
cout<<*it<<el;
}
//queue
queue<int> q;
q.push(5);
while(!q.empty())
{
cout<<q.front()<<el;
q.pop();
cout<<q.size()<<el;
}
//stack
stack<int> s;
s.push(6);
while(!s.empty())
{
cout<<s.top()<<el;
s.pop();
}
//添加了pair
vector<pair<int, int> > ves;
ves.push_back({7,8});
pair<int, int> iter;
for(vector<pair<int,int> >::iterator it = ves.begin();it!=ves.end();it++)
{
iter = *it;
cout<<iter.first<<el;
cout<<iter.second<<el;
}

return 0;
}

posted on 2022-04-05 19:01    阅读(12)  评论(0编辑  收藏  举报