跳到正文

个人记录

最短路综合测试总结

xgc88888888约 14 分钟
原文
文章信息与版本

文章信息

发布
2025年7月11日
收录
2025年12月12日
更新
2026年11月16日
篇幅
约 14 分钟

版本

  1. v1当前

1.P2984 [USACO10FEB] Chocolate Giving S

课上讲过原题, ACAC 了

Ac Code

C++
#include<bits/stdc++.h>
using namespace std;
const int N=5e4+7;
const int INF=1e9;
struct edge{
	int v,w;
};
struct node{
	int id,w;
	bool operator < (const node & b)const
	{
		return w>b.w; 
	}
};
int n,m,q;
int dis[N],vis[N];
vector<edge> g[N];
void dijstra(){
	for(int i=1;i<=n;i++){
		dis[i]=INF;
	}
	dis[1]=0;
	priority_queue<node> q;
	q.push({1,0});
	while(!q.empty()){
		node front=q.top();
		int u=front.id;
		q.pop();
		if(vis[u]==1)continue;
		vis[u]=1;
		for(int i=0;i<g[u].size();i++){
			int v=g[u][i].v;
			int w=g[u][i].w;
			if(vis[v]==0&&dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				q.push({v,dis[v]});
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
//	freopen("T1.in","r",stdin);
//	freopen("T1.out","w",stdout);
	cin>>n>>m>>q;
	while(m--){
		int u,v,w;
		cin>>u>>v>>w;
		g[u].push_back({v,w});
		g[v].push_back({u,w});
	}
	dijstra();
	while(q--){
		int u,v;
		cin>>u>>v;
		cout<<dis[u]+dis[v]<<endl;
	}
	return 0;
}

2.U515803 乘车进阶版

讲过类似的题目,也 ACAC 了

C++
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N=1e3+7;
const int INF=1e18;
struct edge{
	int v,w;
};
struct node{
	int id,w;
	bool operator < (const node & b)const
	{
		return w>b.w; 
	}
};
int n,a,b,c;
int mp[N][N];
int vis[N],dis[N][2];
vector<edge> g[N][2];
void dijstra(int cnt,int s){
	for(int i=1;i<=n;i++){
		dis[i][cnt]=INF;
		vis[i]=0;
	}
	dis[s][cnt]=0;
	priority_queue<node> q;
	q.push({s,0});
	while(!q.empty()){
		node front=q.top();
		int u=front.id;
		q.pop();
		if(vis[u]==1)continue;
		vis[u]=1;
		for(int i=0;i<g[u][cnt].size();i++){
			int v=g[u][cnt][i].v;
			int w=g[u][cnt][i].w;
			if(vis[v]==0&&dis[v][cnt]>dis[u][cnt] + w){
				dis[v][cnt]=dis[u][cnt]+w;
				q.push({v,dis[v][cnt]});
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
//	freopen("T2.in","r",stdin);
//	freopen("T2.out","w",stdout);
	cin>>n>>a>>b>>c;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			cin>>mp[i][j];
			g[i][0].push_back({j,mp[i][j]*a});
			g[j][1].push_back({i,mp[i][j]*b+c});
		}
	}
	dijstra(0,1);
	dijstra(1,n);
	int minn=INF;
	for(int i=1;i<=n;i++){
		minn=min(minn,dis[i][0]+dis[i][1]);
	}
	cout<<minn;
	return 0;
}

3.U511744 滑雪场(resort)

用 spfaspfa 本来能拿 9898 分(没看到如果空间 XX 的高度 == 空间 YY 的高度,幸福感则不会改变),只拿了 9292 分

但本题不用 spfaspfa ,时间复杂度较高( O(n∗m)O(n*m) )

需要处理负边权,用 dijkstradijkstra ( O(mlog(n+m))O(mlog(n+m)) )

建边

示例

  • 如图所示

负边权转正边权

示例

  • 原来的最短路 =W1+W2 = W_1+W_2 ,发现所有边权加上一个数字,不可取的,路径的边数不一样只需要让加的东西不影响边的数量

  • 现在的最短路 =W1+W2+(h[2]−h[1])+(h[3]−h2]) = W_1+W_2+(h[2]-h[1])+(h[3]- h2])

  • 可得现在的最短路 ==原来的最短路 +h[3]−h[1]+h[3]-h[1] ,即 h[h[终点]−h[]-h[起点]]

  • 每条边加上 h[h[终点]−h[]-h[起点]]

最长路转最短路

  • 原图求最长路 == 边权取反求最短路

Ac Code

C++
#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int maxn=2e5+7;
const int INF=1e18;
int n,m,h[maxn],dis[maxn];
bool vis[maxn];
struct node{
	int v,w;
};
struct edge{
	int id,w;
	bool operator < (const edge & b)const
	{
		return w>b.w; 
	}
};
vector<node> g[maxn];
void dijkstra(){
	for(int i=1;i<=n;i++){
		dis[i]=INF;
	}
	dis[1]=0;
	priority_queue<edge> q;
	q.push({1,0});
	while(!q.empty()){
		edge front=q.top();
		int u=front.id;
		q.pop();
		if(vis[u]==1)continue;
		vis[u]=1;
		for(int i=0;i<g[u].size();i++){
			int v=g[u][i].v;
			int w=g[u][i].w;
			if(vis[v]==0&&dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				q.push({v,dis[v]});
			}
		}
	}
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>m;
	for(int i=1;i<=n;i++){
		cin>>h[i];
	}
	for(int i=1;i<=m;i++){
		int x,y;
		cin>>x>>y;
        //边权取反+h[终点]-h[起点]
		if(h[x]>h[y]){
			g[x].push_back({y,0});
			g[y].push_back({x,h[x]-h[y]});
		}
		else if(h[x]==h[y]){
			g[x].push_back({y,0});
			g[y].push_back({x,0});
		}
		else{
			g[x].push_back({y,h[y]-h[x]});
			g[y].push_back({x,0});
		}
	}
	dijkstra();
	int maxn=-INF;//求最短路
	for(int i=1;i<=n;i++){
		maxn=max(maxn,h[1]-h[i]-dis[i]);//减去最短路,得到最长路
	}
	cout<<maxn;
	return 0;
}

4.U422724 数组操作

没思路,骗了55 分

思想:同余最短路

数字的之间变化,看成点权编号的变化

  • (v+x)(v+x) % nn 花费是 pp

  • ((v−y)((v-y)%n % nn 花费是 qq

取模公式

  • (a+b)(a+b) % c==(ac==(a % c+bc+b % c)c) % cc
  • (a−b)(a-b) % c==(ac==(a % c−bc-b % c)c) % cc

负数在取模状态下如何转化为整数

  • (a(a % c)=(ac)=(a % c+c)c+c) % cc

Ac Code

C++
#include<bits/stdc++.h>
#define int long long 
using namespace std;
const int maxn=1e6+7;
const int mod=1e9+7;
const int INF=LONG_LONG_MAX;
struct edge{
	int v,w;
};
struct node{
	int id,w;
	bool operator < (const node & b)const
	{
		return w>b.w; 
	}
};
int n,p,x,q,y;
int sum=0;
vector<edge> g[maxn];
int dis[maxn];
bool vis[maxn];
void dijkstra(){
	for(int i=0;i<n;i++){
		dis[i]=INF;
	}
	dis[sum]=0;
	priority_queue<node> q;
	q.push({sum,0});
	while(!q.empty()){
		node front=q.top();
		int u=front.id;
		q.pop();
		if(vis[u]==1){
			continue;
		}
		vis[u]=1;
		for(int i=0;i<g[u].size();i++){
			int v=g[u][i].v;
			int w=g[u][i].w;
			if(vis[v]==0&&dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				q.push({v,dis[v]});
			}
		}
	}
	return;
}
signed main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	cin>>n>>p>>x>>q>>y;
	for(int i=1;i<=n;i++){
		int w;
		cin>>w;
		sum+=w;
		sum%=n;
	}
	for(int i=0;i<n;i++){
		int down=((i-y)%n+n)%n;
		int up=((i+x)%n+n)%n;
        //建加和减两条边
		g[i].push_back({down,q});
		g[i].push_back({up,p});
	}
	dijkstra();
	if(dis[0]==INF){
		cout<<-1;
	}
	else{
		cout<<dis[0];
	}
	return 0;
}

5.U544634 上学

本题重点在于建边

公交车单向建边有误且时间复杂度高,只拿了 2020 分

要用桶记录第 ii 辆公交车上一次停在哪里

Ac Code

C++
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+10;
const int INF=1e9;
struct edge{
	int v,w;
};
struct node{
	int id,w;
	bool operator < (const node & b)const
	{
		return w>b.w; 
	}
};
vector<edge> g[N];
int n,m,s,t,T;
int a[N];
int dis[N],vis[N],pre[N];
void dijstra(){
	for(int i=1;i<=n;i++){
		dis[i]=INF;
	}
	dis[s]=0;
	priority_queue<node> q;
	q.push({s,0});
	while(!q.empty()){
		node front=q.top();
		int u=front.id;
		q.pop();
		if(vis[u]==1)continue;
		vis[u]=1;
		for(int i=0;i<g[u].size();i++){
			int v=g[u][i].v;
			int w=g[u][i].w;
			if(vis[v]==0&&dis[v]>dis[u]+w){
				dis[v]=dis[u]+w;
				q.push({v,dis[v]});
			}
		}
	}
}
int main(){
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
//	freopen("T5.in","r",stdin);
//	freopen("T5.out","w",stdout);
	cin>>n>>m>>s>>t>>T;
	for(int i=1;i<=m;i++){
		cin>>a[i];
	}
	for(int i=1;i<=n;i++){
		int x;
		cin>>x;
        //第一次出现
		if(pre[x]){
			g[pre[x]].push_back({i,a[x]});//建立公交的边
		}
		pre[x]=i;//记录对应种类公交车站的当前末位置
		if(i!=1){
            //建立步行的边
			g[i-1].push_back({i,T});
			g[i].push_back({i-1,T});
		}
	}
	dijstra();
	cout<<dis[t];
	return 0;
}
原文