跳到正文

题解

题解:P1001 A+B Problem

__CrossBow_EXE__约 2 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2026年5月18日
篇幅
约 2 分钟
获赞
7
题目
P1001

版本

  1. v1当前

前言

考虑把题目转化为图上问题:相当于三个节点(记作 1,2,31,2,3),连一条从 11 至 22,权值为 aa 的边;以及一条从 22 至 33,权值为 bb 的边。答案就是从 11 到 33 的最短路。

我们发现边的数量和点的数量很少,考虑 Floyd。时间复杂度为 O(n3)O(n^3),时间还是卡得很死的。

代码

C++
#include<bits/stdc++.h>
#define ll long long
#define endl '\n'
using namespace std;
int a,b;
int G[5005][5005];//邻接矩阵存图
signed main(int argc,char *argv[]){
//	freopen(".in","r",stdin);
//	freopen(".out","w",stdout);
	ios::sync_with_stdio(NULL);
	cin.tie(0),cout.tie(0);
	cin>>a>>b;
	//初始化为极大值
	for(int i=1;i<=5000;i++){
		for(int j=1;j<=5000;j++){
			G[i][j]=2147483647;
		}
	}
	G[1][2]=a;//建边
	G[2][3]=b;
	//Floyd
	for(int k=1;k<=3;k++){
		for(int i=1;i<=3;i++){
			for(int j=1;j<=3;j++){
				G[i][j]=min(G[i][j],G[i][k]+G[k][j]);//松弛
			}
		}
	}
	cout<<G[1][3]<<endl;//直接输出最短路
	return 0;
}
/*
---INFORMATIONS---
TIME:2025/5/10 14:10:44
PROBLEM:P1001
CODE BY __CrossBow_EXE__ Luogu uid967841
*/
原文