hdu6075 2019CCPC network trials 1004 path

The meaning of problems: Given a weighted directed graph, q group asked every ask to have all the road map, the k-th smaller path weight

Problem-solving ideas: because only the largest k 5e4, consider violence a small front maxk search path and recording the weights array, you can then O (1) query.

Specific implementation: ideas can make use of Dijkstra's shortest path search violence, which uses the known shortest update reveals a new shortest. First of all edges are put into a multiset inside, then each of the first element in the multiset out, as new answers, and then use it to update a new shortest, so continued to spread, then you can get an answer.

However, this may TLE or MLE, then consider Gaga optimization, first of all we just need a little before maxk path, multiset can be limited to less than maxk, so as not to MLE, and then we can start to each node the adjacent table edge by weight from small to large, so that when the enumeration of the new path if the weight is greater than the maximum value in the multiset can break straight away, so as not to TLE up.

AC Code:

#include<bits/stdc++.h>
using namespace std;

typedef long long ll;
const int maxn=5e4+5;
const int MAXK=5e4+5;
int n,m,q,k;

struct Edge{
    int id,u,v;
    ll w;
    bool operator<(const Edge& b)const{return w<b.w;}
}; 

multiset<Edge>s;

vector<Edge>G[maxn];
ll ans[MAXK];
int tot,maxk,qry[maxn];

void init(){
    for(int i=1;i<=n;i++)sort(G[i].begin(),G[i].end());
    int cnt=0;
    while(true){
        
        //cout<<"S:\n";for(auto i:s)cout<<i.u<<" "<<i.v<<" "<<i.w<<"\n";
        
        
        ans[++cnt]=s.begin()->w;
        Edge e=*s.begin(),tmp;
        s.erase(s.begin());
        if(cnt>=maxk)break;
        
        int psz=(int)s.size();
        
        int sz=(int)G[e.v].size();
        
        if((int)s.size()+sz<=maxk){
            for(int i=0;i<sz;i++){
                Edge t=G[e.v][i];
                tmp.id=++tot;tmp.u=e.u;tmp.v=t.v;tmp.w=e.w+t.w;
                s.insert(tmp);
            }
        }
        else{
            for(int i=0;i<sz;i++){
                Edge t=G[e.v][i];
                if((int)s.size()<=maxk){
                    tmp.id=++tot;tmp.u=e.u;tmp.v=t.v;tmp.w=e.w+t.w;
                    s.insert(tmp);
                }
                else{
                    Edge last=*(--s.end());
                    if(last.w>e.w+t.w){
                        s.erase(last);
                        tmp.id=++tot;tmp.u=e.u;tmp.v=t.v;tmp.w=e.w+t.w;
                        s.insert(tmp);
                    }
                    else break;
                }
            }
        }
    }
}

int main()
{
//#ifndef ONLINE_JUDGE
//  freopen("in.txt","r",stdin);
//#endif
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d %d %d",&n,&m,&q);
        
        for(int i=1;i<=n;i++)G[i].clear();
        s.clear();tot=0;
        
        int u,v;
        ll w;
        Edge tmp;
        for(int i=1;i<=m;i++){
            scanf("%d %d %lld",&u,&v,&w);
            tmp.id=++tot;tmp.u=u;tmp.v=v;tmp.w=w;
            G[u].push_back(tmp);
            s.insert(tmp);
        }
        maxk=0;
        for(int i=1;i<=m;i++){
            scanf("%d",&qry[i]);
            maxk=max(maxk,qry[i]);
        }
        init();
        for(int i=1;i<=m;i++){
            printf("%lld\n",ans[qry[i]]);
        }
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/zengzk/p/11403194.html