最短路Dijkstra,spfa,图论二分图算法AYIT---ACM训练(模板版)

在这里插入图片描述
在这里插入图片描述

文章目录

  • 前言
  • A - Dijkstra Algorithm
    • 0x00 算法题目
    • 0x01 算法思路
    • 0x02 代码实现
  • B - 最长路
    • 0x00 算法题目
    • 0x01 算法思路
    • 0x02 代码实现
  • C - 二分图最大匹配
    • 0x00 算法题目
    • 0x01 算法思路
    • 0x02 代码实现
  • D - 搭配飞行员
    • 0x00 算法题目
    • 0x01 算法思路
    • 0x02 代码实现
  • E - The Perfect Stall
    • 0x00 算法题目
    • 0x01 算法思路
    • 0x02 代码实现
  • F - Asteroids
    • 0x00 算法题目
    • 0x01 算法思路
    • 0x02 代码实现
  • G - Til the Cows Come Home
    • 0x00 算法题目
    • 0x01 算法思路
    • 0x02 代码实现
  • H - 拓扑排序
    • 0x00 算法题目
    • 0x01 算法思路
    • 0x02 代码实现
  • 总结


前言

最短路Dijkstra,spfa,图论二分图算法AYIT—ACM训练(模板版)
A — Dijkstra
B — spfa/Dijkstra
C — 二分图
D — 二分图
E — 二分图
F — 二分图
G — Dijkstra
H — Topsort


A - Dijkstra Algorithm

0x00 算法题目

在这里插入图片描述

0x01 算法思路

Dijkstra算法基础模板题

💬 模板演示:

int dijkstra()
{memset(dist,0x3f,sizeof dist);dist[1]=0;for(int i=0;i<n;i++){int t=-1;for(int j=1;j<=n;j++){if(!st[j] && (t==-1 || dist[t] > dist[j]))t=j;}st[t]=true;for(int j=1;j<=n;j++)dist[j]=min(dist[j],dist[t]+g[t][j]);}if(dist[n]==0x3f3f3f3f) return -1;return dist[n];}

0x02 代码实现

朴素版本Dijkstra:

💬 代码演示:

#include<iostream>
#include<cstring>
#include<algorithm>using namespace std;
const int N = 510;
int g[N][N];
bool st[N];
int dist[N];
int n,s,f;int dijkstra()
{memset(dist,0x3f,sizeof dist);dist[s]=0;for(int i=0;i<n;i++){int t=-1;for(int j=1;j<=n;j++)if(!st[j] && (t==-1 || dist[t] > dist[j]))t=j;st[t]=true;for(int j=1;j<=n;j++)dist[j]=min(dist[j],dist[t]+g[t][j]);}if(dist[f]==0x3f3f3f3f) return -1;return dist[f];
}int main()
{cin>>n>>s>>f;for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){int x;cin>>x;if(x==-1) g[i][j]=0x3f3f3f3f;else g[i][j]=x;}}int t =dijkstra();cout<<t<<endl;return 0;
}

🚩 运行结果:
在这里插入图片描述
spfa算法:
💬 代码演示:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>using namespace std;
const int N=110,M=110*110;
int n,s,f;
bool st[N];
int h[N],w[M],ne[M],e[M],idx;
int dist[N];void add(int a,int b,int c)
{e[idx]=b,w[idx]=c,ne[idx]=h[a],h[a]=idx++;
}int spfa()
{memset(dist,0x3f,sizeof dist);dist[s]=0;queue<int> q;q.push(s);while(q.size()){int t = q.front();q.pop();st[t]=false;for(int i=h[t];i!=-1;i=ne[i]){int j=e[i];if(dist[j] > dist[t] + w[i]){dist[j]=dist[t]+w[i];if(!st[j]){q.push(j);st[j]=true;}}}}if(dist[f]==0x3f3f3f3f) return -1;else return dist[f];
}int main()
{cin>>n>>s>>f;memset(h,-1,sizeof h);for(int i=1;i<=n;i++){for(int j=1;j<=n;j++){int x;cin>>x;//if(x==-1) continue;if(x>0) add(i,j,x);}}cout<<spfa()<<endl;return 0;
}

🚩 运行结果:
在这里插入图片描述

B - 最长路

0x00 算法题目

在这里插入图片描述

0x01 算法思路

spfa算法基础模板题

💬 模板演示:

int spfa()
{memset(dist, 0x3f, sizeof dist);dist[1] = 0;queue<int> q;q.push(1);while(q.size()){auto t = q.front();q.pop();st[t]=false;for(int i=h[t];i!=-1;i=ne[i]){int j = e[i];if(dist[j] > dist[t]+w[i]){dist[j]=dist[t]+w[i];if(!st[j]){q.push(j);st[j]=true;}}}}return dist[n];
}

0x02 代码实现

spfa算法:
💬 代码演示:

#include<bits/stdc++.h>
#define endl '\n'using namespace std;
const int N = 1510,INF = 0x3f3f3f3f;
int n,m;
int dist[N];
int g[N][N];
queue<int> q;void spfa()
{memset(dist,-1,sizeof dist);dist[1]=0;q.push(1);while(!q.empty()){int t = q.front();q.pop();for(int j=1;j<=n;j++){if(g[t][j] && dist[j] < dist[t] + g[t][j]){dist[j] = dist[t] + g[t][j];q.push(j);}}}}int main()
{cin>>n>>m;for(int i=1;i<=m;i++){int a,b,c;cin>>a>>b>>c;g[a][b]=max(g[a][b],c);}spfa();cout<<dist[n]<<endl;return 0;
}

🚩 运行结果:
在这里插入图片描述

C - 二分图最大匹配

0x00 算法题目

在这里插入图片描述

0x01 算法思路

二分图模板题

💬 模板演示:

//邻接表
bool find(int x)
{for (int i = h[x]; i != -1; i = ne[i]){int j = e[i];if (!st[j]){st[j] = true;if (match[j] == 0 || find(match[j])){match[j] = x;return true;}}}return false;
}
//邻接矩阵
bool find(int x)
{for(int i=0;i<g[x].size();++i){int j = g[x][i];if(!st[j]){st[j]=true;if(match[j]==0 || find(match[j])){match[j]=x;return true;}}}return false;
}

0x02 代码实现

💬 代码演示:

#include<iostream>
#include<cstring>using namespace std;const int N = 510,M=5e4+10;
int n,m,q;
int h[N],e[M],ne[M],idx;
int match[N];
bool st[N];void add(int a,int b)
{e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}bool find(int x)
{for(int i=h[x];i!=-1;i=ne[i]){int j = e[i];if(!st[j]){st[j]=true;if(match[j]==0 || find(match[j])){match[j]=x;return true;}}}return false;
}int main()
{cin>>n>>m>>q;memset(h,-1,sizeof h);while(q--){int u,v;cin>>u>>v;add(u,v);}int res=0;for(int i=1;i<=n;i++){memset(st,false,sizeof st);if(find(i)) res++;}cout<<res<<endl;return 0;
}

🚩 运行结果:
在这里插入图片描述

D - 搭配飞行员

0x00 算法题目

在这里插入图片描述

0x01 算法思路

二分图模板题

💬 模板演示:

//邻接表
bool find(int x)
{for (int i = h[x]; i != -1; i = ne[i]){int j = e[i];if (!st[j]){st[j] = true;if (match[j] == 0 || find(match[j])){match[j] = x;return true;}}}return false;
}
//邻接矩阵
bool find(int x)
{for(int i=0;i<g[x].size();++i){int j = g[x][i];if(!st[j]){st[j]=true;if(match[j]==0 || find(match[j])){match[j]=x;return true;}}}return false;
}

0x02 代码实现

💬 代码演示:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<bits/stdc++.h>using namespace std;
const int N = 110;int n,m;
int map[N][N];
int match[N];
bool st[N];
vector<int> g[N];
bool find(int x)
{for(int i=0;i<g[x].size();++i){int j = g[x][i];if(!st[j]){st[j]=true;if(match[j]==0 || find(match[j])){match[j]=x;return true;}}}return false;
}int main()
{scanf("%d %d",&n,&m);int a,b;while(cin>>a>>b){g[a].push_back(b);}int res = 0;for(int i=1;i<=m;i++){memset(st,false,sizeof st);if(find(i)) {res++;}}cout<<res;return 0;
}

🚩 运行结果:
在这里插入图片描述

E - The Perfect Stall

0x00 算法题目

在这里插入图片描述

0x01 算法思路

二分图模板题

💬 模板演示:

//邻接表
bool find(int x)
{for (int i = h[x]; i != -1; i = ne[i]){int j = e[i];if (!st[j]){st[j] = true;if (match[j] == 0 || find(match[j])){match[j] = x;return true;}}}return false;
}
//邻接矩阵
bool find(int x)
{for(int i=0;i<g[x].size();++i){int j = g[x][i];if(!st[j]){st[j]=true;if(match[j]==0 || find(match[j])){match[j]=x;return true;}}}return false;
}

0x02 代码实现

💬 代码演示:

#include<algorithm>
#include<bits/stdc++.h>
using namespace std;
const int N = 510,M=5e4+10;
int n,m;
int match[N];
bool st[N];
vector<int> g[N];bool find(int x)
{for(int i=0;i<g[x].size();++i){int j = g[x][i];if(!st[j]){st[j]=true;if(match[j]==0 || find(match[j])){match[j]=x;return true;}}}return false;
}int main()
{while(~scanf("%d%d",&n,&m)){memset(st,false,sizeof st);memset(match,0,sizeof match);for(int i=1;i<=n;i++){g[i].clear();int s;cin>>s;while(s--){int q;cin>>q;g[i].push_back(q);}}int res=0;for(int i=1;i<=n;i++){memset(st,false,sizeof st);if(find(i)) res++;}cout<<res<<endl;}return 0;
}

🚩 运行结果:
在这里插入图片描述

F - Asteroids

0x00 算法题目

在这里插入图片描述

0x01 算法思路

二分图模板题

💬 模板演示:

//邻接表
bool find(int x)
{for (int i = h[x]; i != -1; i = ne[i]){int j = e[i];if (!st[j]){st[j] = true;if (match[j] == 0 || find(match[j])){match[j] = x;return true;}}}return false;
}
//邻接矩阵
bool find(int x)
{for(int i=0;i<g[x].size();++i){int j = g[x][i];if(!st[j]){st[j]=true;if(match[j]==0 || find(match[j])){match[j]=x;return true;}}}return false;
}

0x02 代码实现

💬 代码演示:

#include<iostream>
#include<cstring>
using namespace std;
const int N = 510,M=5e4+10;
int n,m,q;
int h[N],e[M],ne[M],idx;
int match[N];
bool st[N];void add(int a,int b)
{e[idx]=b,ne[idx]=h[a],h[a]=idx++;
}bool find(int x)
{for(int i=h[x];i!=-1;i=ne[i]){int j = e[i];if(!st[j]){st[j]=true;if(match[j]==0 || find(match[j])){match[j]=x;return true;}}}return false;
}int main()
{cin>>n>>m;memset(h,-1,sizeof h);while(m--){int u,v;cin>>u>>v;add(u,v);}int res=0;for(int i=1;i<=n;i++){memset(st,false,sizeof st);if(find(i)) res++;}cout<<res<<endl;return 0;
}

🚩 运行结果:
在这里插入图片描述

G - Til the Cows Come Home

0x00 算法题目

在这里插入图片描述

0x01 算法思路

Dijkstra算法基础模板题

💬 模板演示:

int dijkstra()
{memset(dist,0x3f,sizeof dist);dist[1]=0;for(int i=0;i<n;i++){int t=-1;for(int j=1;j<=n;j++){if(!st[j] && (t==-1 || dist[t] > dist[j]))t=j;}st[t]=true;for(int j=1;j<=n;j++)dist[j]=min(dist[j],dist[t]+g[t][j]);}if(dist[n]==0x3f3f3f3f) return -1;return dist[n];}

0x02 代码实现

💬 代码演示:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<stdbool.h>using namespace std;const int N=1010,inf = 0x3f3f3f3f;int n,m;
bool st[N];
int dist[N];
int g[N][N];int dijkstra()
{memset(dist,inf,sizeof(dist));dist[1]= 0;for(int i=1;i <= n;i++){int t=-1;for(int j=1;j<=n;j++)if(!st[j] && (t==-1 || dist[t] > dist[j]))t=j;st[t]=true;for(int j=1;j<=n;j++)dist[j]=min(dist[j],dist[t]+g[t][j]);}return dist[n];
}int main()
{cin>>m>>n;memset(g,inf,sizeof g);for(int i=0;i<m;++i){int a,b,c;cin>>a>>b>>c; g[a][b]=g[b][a]=min(g[a][b],c);}cout<< dijkstra() <<endl;return 0;
}

🚩 运行结果:
在这里插入图片描述

H - 拓扑排序

0x00 算法题目

在这里插入图片描述

0x01 算法思路

拓扑排序算法基础模板题

💬 模板演示:

bool topsort()
{int hh=0,tt=-1;for (int i = 1; i <= n; i ++ )if (!d[i])q[ ++ tt] = i;while(hh<=tt){int t = q[hh ++ ];for (int i = h[t]; i != -1; i = ne[i]){int j = e[i];if (-- d[j] == 0)q[ ++ tt] = j;}}return tt==n-1;
}

0x02 代码实现

💬 代码演示:

#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>using namespace std;const int N=100010;int n,m;
vector<int> v[N];
int size,d[N];
int ans[N];void topsort()
{priority_queue<int,vector<int>,greater<int> > q;for(int i=1;i<=n;i++)if(!d[i]) q.push(i);while(!q.empty()){int t = q.top();q.pop();ans[size++] = t;for(auto it : v[t]){d[it]--;if(!d[it]) q.push(it);}}
}int main()
{cin>>n>>m;for(int i=0;i<m;i++){int a,b;cin>>a>>b;v[a].push_back(b);d[b]++;}topsort();for(int i=0 ; i<size ;i++)cout<< 'v' << ans[i] <<' ';return 0;
}

🚩 运行结果:
在这里插入图片描述


总结

这次训练很明显涉及到了最短路Dijkstra,spfa,图论二分图算法,以及topsort算法,这次考的比较基础,但是让我意识到了,算法必须熟练记忆模板是很重要的。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/71739.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Ubuntu22使用过程问题记录】

Ubuntu22.04 使用过程问题解决方案 1 系统基本设置 1.1 输入法 增加中文输入 1、Settings -> Region & Language -> Manage Installed Languages -> 选中chinese&#xff0c;然后apply&#xff1b; 2、Settings -> Keyboard -> Input Sources -> 点号…

Ceph入门到精通- 加快日志轮转

提高 Ceph 组件的调试级别可能会产生大量数据。如果您几乎有完整的磁盘&#xff0c;可以通过修改 /etc/logrotate.d/ceph 中的 Ceph 日志轮转文件来加快日志轮转。Cron 作业调度程序使用此文件来调度日志轮转。 先决条件 一个正在运行的 Red Hat Ceph Storage 集群。节点的根…

Ubuntu离线安装Python第三方库

这里以安装PyYAML为例。 首先进入PyYAML官网&#xff0c;下载PyYAML-5.3.1.tar.gz安装包。 解压缩&#xff1a; tar -zxvf PyYAML-5.3.1.tar.gz解压后&#xff0c;生成一个PyYAML-5.3.1目录&#xff0c;进入该目录&#xff0c;执行以下命令进行安装&#xff1a; python set…

MyBatis Plus复合主键问题解析

引言 在数据库设计中&#xff0c;有时候需要使用复合主键来唯一标识表中的一行数据。然而&#xff0c;在使用MyBatis Plus框架时&#xff0c;处理复合主键可能会带来一些挑战和困惑。本文将为您详细介绍MyBatis Plus中复合主键的问题以及解决方案。 复合主键定义 复合主键是…

vue、uniapp中动态添加绑定style、class 9种方法实现

9种方法介绍 直接使用静态class和style属性&#xff1a; 使用场景&#xff1a;当class和style属性是固定不变的时候&#xff0c;可以直接在模板中写死。优点&#xff1a;简单直接&#xff0c;没有额外的计算和逻辑。缺点&#xff1a;无法根据条件动态修改class和style。 使用v…

SpringBatch 事务详解

Spring Batch 是一个强大的批处理框架&#xff0c;它充分利用了 Spring 框架的事务管理机制&#xff0c;以确保批处理任务的数据一致性和完整性。在 Spring Batch 中&#xff0c;事务是一项关键概念&#xff0c;这里将详细解释 Spring Batch 中的事务管理。 一、事务管理器&am…

Discourse 应该保留多少备份

近期&#xff0c;我们在对 Discourse 进行恢复的时候&#xff0c;我们发现新的备份可能会导致不是所有的数据都能恢复到服务上。 这时候我们应该考虑让 Discourse 保留多少备份的问题&#xff1f; 在默认情况下&#xff0c;我们设置 Discourse 的备份是保留 5 个。这是官方的…

ARM/X86工业级数据采集 (DAQ) 与控制产品解决方案

I/O设备&#xff0c;包括信号调理模块、嵌入式PCI/PCIE卡、便携式USB模块、DAQ嵌入式计算机、模块化DAQ系统&#xff0c;以及DAQNavi/SDK软件开发包和DAQNavi/MCM设备状态监测软件。 工业I/O产品适用于各种工业自动化应用&#xff0c;从机器自动化控制、测试测量到设备状态监测…

第 361 场周赛 (AC 1,第二题过了但是考试结束了)

7020.统计对称整数的数目 思路一&#xff1a;指定区间统计对称整数 1.遍历区间2.判断该数对不对称 对称逻辑&#xff1a;首尾同时开始遍历&#xff0c;并且同时累加为两个数&#xff0c;最后判断两个数是否相等 class Solution { public:bool judge(int num){if(num<10) …

剑指 Offer 24. 反转链表

剑指 Offer 24. 反转链表 迭代 class Solution {public ListNode reverseList(ListNode head) {ListNode pre null, cur head;while(cur ! null){ListNode next cur.next;cur.next pre;pre cur;cur next;}return pre;} }递归&#xff08;使用额外参数&#xff09; 和上…

SpringBoot 如何优雅的进行全局异常处理?

在SpringBoot的开发中&#xff0c;为了提高程序运行的鲁棒性&#xff0c;我们经常需要对各种程序异常进行处理&#xff0c;但是如果在每个出异常的地方进行单独处理的话&#xff0c;这会引入大量业务不相关的异常处理代码&#xff0c;增加了程序的耦合&#xff0c;同时未来想改…

QT中QRadioButton实现分组C++

通过对QRadioButton组件进行分组可解决QRadioButton组件的互斥性 实现如下。 假设已设计好UI并且有UI代码情况&#xff1a; 头文件引用&#xff1a; #include <QButtonGroup> 分组功能 &#xff0c;cpp文件代码实现&#xff1a; Your_Project::Your_Project(QWidge…

IDEA Java1.8通过sqljdbc4连接sqlserver插入语句

1. 下载sqljdbc4:https://mvnrepository.com/artifact/com.microsoft.sqlserver.jdbc/sqljdbc4/4.0 下载后在IDEA放入仓库内&#xff0c;可以放在resources下&#xff0c;右键“add as library”。 2. 在控制面板中开启Telnet客户端&#xff0c;默认是不开启的。 若报错“ ja…

4.矩阵的几何意义、变基与迹

文章目录 变基操作与矩阵矩阵的迹几何意义矩阵迹的几条性质 欢迎访问个人网络日志&#x1f339;&#x1f339;知行空间&#x1f339;&#x1f339; 变基操作与矩阵 我们知道空间中一点的坐标可以表示以原点为起点以该点为终点的向量。 以二维平面为例&#xff0c;如下图 选取…

Car Window Control Reset

大众汽车窗口自动升降失效&#xff0c;重置&#xff1a; 扣住5秒&#xff0c;重启汽车&#xff0c;试一下车钥匙&#xff0c;和再重试这个按钮&#xff0c;扣一下试一试

Mysql更新时间列只改日期为指定日期不更改时间

场景 Mysql分表后同结构不同名称表之间复制数据以及Update语句只更新日期加减不更改时间&#xff1a; Mysql分表后同结构不同名称表之间复制数据以及Update语句只更新日期加减不更改时间_霸道流氓气质的博客-CSDN博客 上面通过如下方式实现日期列增加指定天数。 UPDATE bus…

【LeetCode-中等题】47. 全排列 II

文章目录 组合并集问题汇总&#xff1a;题目方法一&#xff1a;递归回溯去重 组合并集问题汇总&#xff1a; 1、子集去重版本 2、组合非去重版本 3、子集非去重版本 题目 相比较46题&#xff1a;不需要去重&#xff1a;【LeetCode-中等题】46. 全排列 需要做出的改变就是&a…

在学习DNS的过程中给我的启发

在国内&#xff0c;关于DNS相关的话题一直络绎不绝&#xff0c;比如DNS根服务器为什么中国没有&#xff0c;还有Anycast BGP实现负载&#xff0c;为什么DNS只有13个&#xff0c;还有DNS over HTTPS 和 DNS over TLS的优劣等等问题&#xff0c;接下来我会找出几个一一说一下其中…

一些很好的网站或博客链接

NLP实操101 (30道NLP考题检验你的NLP实力)&#xff1a;链接 中文NLP必知必会30题&#xff1a;链接 一个NLP模型综述类的文章&#xff1a;浅析Self-Attention、ELMO、Transformer、BERT、ERNIE、GPT、ChatGPT等NLP models 七月在线发布的面试题&#xff1a;自然语言处理面试3…

DockerCompose部署es和kibana

DockerCompose文件 version: 3.1 services:elasticsearch:image: elasticsearch:7.13.3container_name: elasticsearchprivileged: trueports:- "9200:9200"- "9300:9300"environment:- ES_JAVA_OPTS-Xms128m -Xmx1024m #设置使用jvm内存大小- cluster.na…