[Explanations] PAT 1003 Emergency

The main idea: Dijkstra + DP

If done most shorted count, this question is equivalent to a double experience.

That is, when running the Dijkstra, excluding a \ (t_i \) as the first \ (I \) the minimum number of short bar when the points, a \ (rs_i \) as the first \ (I \) points shortest point the maximum value and the right

Note the number of maintenance to be classified is not \ (dis \) update, the updated value before overwriting with the new value.

Also, the point number \ (0 \) ~ \ (. 1-n-\) , edges are bidirectional edge, make no mistake

Part of the code:


struct wtf{ 
    int t,r,rs;
}r[505];

struct node{
    int u,d;
    friend bool operator<(const node &a,const node &b){
        return a.d>b.d;
    }
};

void Dijkstra(){
    for(int i=0;i<=n-1;i++) dis[i]=inf;
    dis[c1]=0, r[c1].t = 1;
    priority_queue<node>q;
    node x;
    x.u=c1,x.d=0;
    q.push(x);
    while(!q.empty()) {
        node fr = q.top();
        q.pop();
        int u = fr.u, d = fr.d;
        if(d != dis[u]) continue;
        for(int i = head[u]; i != -1; i = e[i].next) {
            int v = e[i].to, w = e[i].w;
            if(dis[u] + w <= dis[v]) {
                bool flag = 0;
                if(dis[u] + w < dis[v])
                    r[v].t = r[u].t,
                    flag = 1;
                else if(dis[u] + w == dis[v])
                    r[v].t = r[v].t + r[u].t;
                    
                r[v].rs = max(r[u].rs + r[v].r, r[v].rs);
                dis[v] = dis[u] + w;
                if(flag) {
                    node y;
                    y.u = v, y.d = dis[v];
                    q.push(y);
                }
            }
        }
    }
}

PS: to help people see when they change the code title, the third year in the first knock Code ,,, Middle School continue to increase (tui) oil (fei)!

Guess you like

Origin www.cnblogs.com/yizimi/p/12014384.html