A. Relay

A. Relay

There are a total of n players (numbered 1−n) participating in the relay, and any number of players are allowed to race at the same time on the track. Before the start of the race, all traffic policemen waited for the start at the starting line.
At t=0, the runner numbered 1 starts the race, and returns to the starting point after finishing a lap after L1 seconds. When player i finishes a lap, he will signal Mi players to start the relay. (The player may be signaled multiple times, only the earliest signal) Each player only runs one lap.
The total time of the relay is the time for the last runner to finish the race. Find the total time of the relay.


A single integer n in the first line of Input means that there are n players.
In the next n lines, each line starts with 2 integers Li, Mi, followed by Mi integers, indicating the number of the player.


An integer in one line of Output represents the total time of the relay.

Samples
Input Copy
5
4 2 2 4
3 3 1 3 4
7 1 5
4 2 3 5
1 0
Output
14
Hint
[Data Range and Convention]

For 30% of the data: n≤10.
For 60% of the data: n≤300.
For 100% data: n≤1000.
Source
Shiguang Middle School 2017 Changle Summer Camp 6th set of
questions:
At the same time, any number of runners are allowed to race at the same time.  Pay attention to this sentence, he actually runs as long as someone gives him a signal to let him run. (Have not run)
Ask how long it will take to run all.

Idea:
I misread the meaning of the question at the beginning, and the sample I understood was made up. Actually,
I will explain the example first.
First, 1 receives the signal, walks for 4 seconds, then transmits the signal to 2 and 4, then 2 and 4 run together, 2 finishes first, and 2 transmits the signal to 1 (walked but not received), 3 and 4 (already Walk or not), and then 4 finishes running, and pass it to 2 (already gone), 3 (already gone) and 5 (already gone), so he can't transmit it, just wait for 3 to finish. After the sample is explained, it should be almost understandable.
Insert picture description here
So the person who can transmit the signal will go straight away, knowing that the last person is over. So we use the smallest time to deliver information each time. This will not make random reception. Because the time is short, it must come first, and the information must be transmitted first, the signal will be transmitted first, and the first will not need to go.
Basically, the priority queue is used to maintain the minimum value and go to the end.

#include<bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=2e5+7;
ll n,sum;
ll t[maxn],vis[maxn];
ll dp[1101][1010];
#define pii pair<ll,ll>
void bfs() {
    
    
    priority_queue<pii,vector<pii>,greater<pii> >q;
    pii p;
    p= {
    
    t[1],1};
    vis[1]=1;
    q.push(p);
    while(!q.empty()) {
    
    
        pii top=q.top();
        q.pop();

        ll ans=top.first;
        ll x=top.second;

        sum=max(ans,sum);
        for(int i=1; i<=dp[x][0]; i++) {
    
    
            if(vis[dp[x][i]]==0) {
    
    
                vis[dp[x][i]]=1;
                p= {
    
    ans+t[dp[x][i]],dp[x][i]};
                q.push(p);
            }
        }
    }
}
int main() {
    
    
    cin>>n;
    for(int i=1; i<=n; i++) {
    
    
        cin>>t[i];
        ll m;
        cin>>m;
        for(int j=1; j<=m; j++) {
    
    
            ll y;
            scanf("%lld",&y);
            dp[i][++dp[i][0]]=y;
        }
    }
    bfs();
    cout<<sum<<endl;
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_45911397/article/details/110433458