1 条题解

  • 1
    @ 2025-8-13 10:59:02
    #include<queue>
    #include<math.h>
    #include<stdio.h>
    #include<iostream>
    #include<vector>
    #include<iomanip>
    #include<string.h>
    #include<algorithm>
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<stack>
    #include<climits>
    #include<fstream>
    #include<string>
    using namespace std;
    #define LL long long
    const int N = 1e5 + 10;
    const int INF = 0x3f3f3f3f;
    struct Edge 
    {
        int to , time;
        Edge (int t , int tm ) : to(t) , time(tm) {}
    };
    struct Node
    {
        int id , time;
        Node( int i , int t ) : id(i) , time(t) {}
        bool operator>( const Node& other ) const
        {
            return time > other.time;
        }
    };
    vector<int> b , c;
    vector<vector<Edge>> adj;
    int getWaitTime( int u , int currentTime )
    {
        int cycle = b[u] + c[u];
        int mod = currentTime % cycle;
        if ( mod < c[u] )
        {
            return 0;
        }
        return cycle - mod;
    }
    int dijkstra( int n )
    {
        vector<int> dist( n + 1 , INT_MAX );
        priority_queue<Node , vector<Node> , greater<Node>> pq;
        dist[0] = 0;
        pq.push( Node( 0 , 0 ) );  
        while ( !pq.empty() )
        {
            Node node = pq.top();
            pq.pop();       
            if ( node.id == n )
            {
                return node.time;
            }
            if ( node.time > dist[node.id] )
            {
                continue;
            }    
            for ( Edge& edge : adj[node.id] )
            {
                int next = edge.to;
                int arrival = node.time + edge.time;  
                if ( next != n )
                {
                    int wait = getWaitTime( next , arrival );
                    arrival += wait;
                }            
                if ( arrival < dist[next] )
                {
                    dist[next] = arrival;
                    pq.push( Node( next , arrival ) );
                }
            }
        }
        return -1;
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(nullptr);    
        int n;
        cin >> n;
        b.resize(n);
        c.resize(n);
        adj.resize( n + 1 ); 
        for ( int i = 0 ; i < n ; ++i )
        {
            cin >> b[i] >> c[i];
        }   
        int m;
        cin >> m;
        for ( int i = 0 ; i < m ; ++i )
        {
            int x , y , t;
            cin >> x >> y >> t;
            adj[x].emplace_back( y , t );
            adj[y].emplace_back( x , t );
        }   
        cout << dijkstra(n) << endl;
        return 0;
    }
    

    信息

    ID
    2848
    时间
    1000ms
    内存
    256MiB
    难度
    10
    标签
    递交数
    99
    已通过
    2
    上传者