跳到正文

个人记录

qwq?

Fake_Cry约 5 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年1月7日
篇幅
约 5 分钟

版本

  1. v2当前
  2. v1

基础算法

二维前缀和

P1387 最大正方形
C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1000+5;
int a[N][N],pre[N][N];
void solve()
{
	int n,m;
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			cin>>a[i][j];
			pre[i][j]=pre[i-1][j]+pre[i][j-1]-pre[i-1][j-1]+a[i][j];
		}
	}
	int ans=0;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=m;j++){
			for(int l=1;l<=min(n,m);l++){
				int p=i+l-1,q=j+l-1;
				if(p>n||q>m||pre[p][q]-pre[p][j-1]-pre[i-1][q]+pre[i-1][j-1]!=l*l) break;
				if(ans<l) ans=l;
			}
		}
	}
	cout<<ans;
}
signed main(){
	cin.tie(0)->sync_with_stdio(0);
	cout.tie(0);
	int _=1;
	//cin>>_;
	while(_--) solve();
	return 0;
}

模版

图论

P3371 【模板】单源最短路径(弱化版)

优先队列+最短路

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e4+6;
typedef pair<int,int>PII;
int n,m,s,dist[N],st[N];
vector<PII>g[N];
signed main(){
    cin>>n>>m>>s;
    while(m--){
    	int a,b,c;cin>>a>>b>>c;
    	g[a].push_back({b,c});
	}
	priority_queue<PII,vector<PII>,greater<PII> >q;
	memset(dist,0x3f,sizeof dist);
	dist[s]=0;
	q.push({dist[s],s});
	while(q.size()){
		int x=q.top().second;
		q.pop();
		if(st[x])continue;
		st[x]=1;
		for(auto t:g[x]){
			int e=t.first;
			int w=t.second;
			if(dist[e]>dist[x]+w){
				dist[e]=dist[x]+w;
				q.push({dist[e],e});
			}
		}
	}
	for(int i=1;i<=n;i++){
		if(dist[i]>=0x3f3f3f3f)dist[i]=(1<<31)-1;
		cout<<dist[i]<<" ";
	}
	return 0;
}
P4779 【模板】单源最短路径(标准版)
C++
#include<bits/stdc++.h>
#define int long long
const int N = 1e5 + 10;
using namespace std;
typedef pair<int, int> PII;
vector<PII>g[N];
int dist[N], st[N];
signed main()
{
	int n, m, s;
	cin >> n >> m >> s;
	while (m--) {
		int a, b ,c;
		cin >> a >> b >> c;
		g[a].push_back({ b,c });
	}
	priority_queue<PII, vector<PII>, greater<PII>>q;
	for (int i = 1;i <= n;i++) dist[i] = 2e18;
	dist[s] = 0;
	q.push({ dist[s],s });
	while (q.size()) {
		auto t = q.top();
		q.pop();
		int ds = t.first, v = t.second;
		if (st[v] == 1) continue;
		st[v] = 1;
		for (auto it : g[v]) {
			int x = it.first, d = it.second;
			if (dist[v] + d < dist[x]) {
				dist[x] = dist[v] + d;
				q.push({ dist[x],x });
			}
		}
	}
	for (int i = 1;i <= n;i++) cout << dist[i] << " ";
	return 0;
}
原文