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

· · 题解

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

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

#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;
}