跳到正文

题解

题解:P5530 [BalticOI 2002] 双调路径

jsdw约 3 分钟
原文
文章信息与版本

文章信息

发布
2025年6月15日
收录
2025年12月12日
更新
2026年11月6日
篇幅
约 3 分钟
获赞
1
题目
P5530

版本

  1. v1当前

翻译:设存在一条路径 pp,若 ∄ p′\nexists\ p',使得 tp′≤tpt_{p'}\le t_p,且 wp′≤wpw_{p'}\le w_p,那么路径 pp 就是最小路径,现在要求你找到最小路径的个数(若 tp′=tpt_{p'}=t_p,且 wp′−wpw_{p'}-w_p,则视作是一条最小路径)。

我们设 du,xd_{u,x} 为从 ss 到 uu 花费 xx 费用所需的时间,然后直接求即可,具体注释在代码:

C++
#include<bits/stdc++.h>
#define mems(a,b) memset(a,b,sizeof a)
using namespace std;
const int N=110,M=610;
int n,m,S,T;
int h[N],e[M],w[M],t[M],ne[M],idx;
int dist[N][10010],q[N];
bool st[N];
int ans,lst=0x3f3f3f3f;
void add(int a,int b,int c,int d){
	e[idx]=b;
	w[idx]=c;
	t[idx]=d;
	ne[idx]=h[a];
	h[a]=idx++;
}
void spfa(){
	mems(dist,0x3f);
	int hh=0,tt=1;
	q[0]=S;
	st[S]=true;
	for(int i=0;i<=10000;i++) dist[S][i]=0;//自己到自己的时间总是为0
	while(hh!=tt){
		int top=q[hh++];
		if(hh==N) hh=0;
		st[top]=false;
		for(int i=h[top];~i;i=ne[i]){
			int j=e[i];
			for(int k=0;k<=10000-w[i];k++)//枚举费用,注意-w[i],防止越界
				if(dist[j][k+w[i]]>dist[top][k]+t[i]){//说明两方面都要更优
					dist[j][k+w[i]]=dist[top][k]+t[i];
					if(!st[j]){
						q[tt++]=j;
						if(tt==N) tt=0;
						st[j]=true;
					}
				}
		}
	}
}
int main(){
	cin>>n>>m>>S>>T;
	mems(h,-1);
	for(int i=0;i<m;i++){
		int a,b,c,d;
		scanf("%d%d%d%d",&a,&b,&c,&d);
		add(a,b,c,d);
		add(b,a,c,d);
	}
	spfa();
	for(int i=0;i<=10000;i++){//从小到大枚举费用
		if(dist[T][i]>=lst) continue;//费用大,时间也多
		lst=dist[T][i];
		ans++;
	}
	cout<<ans;
	return 0;
}
原文