2 条题解

  • 0
    @ 2026-9-1 21:02:37
    #include<bits/stdc++.h>
    using namespace std;
    #define int long long
    #define lowbit(x) x&-x
    const int INF=0x3f3f3f3f3f3f3f3f;
    const int N=1e5+10;
    const int M=2e6+10;
    struct node{
    	int v,w,k;
    };
    vector<node>g[N];
    int n,m,x,y;
    struct cmp{
    	bool operator()(pair<int,int>u,pair<int,int>v){
    		return u.second>v.second;
    	}
    };
    priority_queue<pair<int,int>,vector<pair<int,int>>,cmp>q;
    int dis[N];
    bool vis[N];
    signed main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
    	cin>>n>>m>>x>>y;
    	for(int i=1;i<=m;i++){
    		int u,v,w,k;
    		cin>>u>>v>>w>>k;
    		g[u].push_back({v,w,k});
    		g[v].push_back({u,w,k});
    	}
    	memset(dis,0x3f,sizeof(dis));
    	q.push({x,0});
    	dis[x]=0;
    	while(!q.empty()){
    		int u=q.top().first;
    		q.pop();
    		if(vis[u])	continue;
    		vis[u]=1;
    		for(auto p:g[u]){
    			int v=p.v;
    			int w=p.w;
    			int k=p.k;
    			int cnt=dis[u];
    			if(cnt%k)	cnt=(dis[u]/k)*k+k;
    			cnt+=w;
    			if(dis[v]>cnt){
    				dis[v]=cnt;
    				q.push({v,cnt});
    			}
    		}
    	}
    	if(dis[y]==INF){
    		cout<<-1;
    		return 0;
    	}
    	cout<<dis[y];
        return 0;
    }
    

    信息

    ID
    2086
    时间
    2000ms
    内存
    1024MiB
    难度
    提高
    标签
    递交数
    2
    已通过
    2
    上传者