zoukankan      html  css  js  c++  java
  • path(优先队列,dijk拓展)

    Problem Description

    You have a directed weighted graph with n vertexes and m edges. The value of a path is the sum of the weight of the edges you passed. Note that you can pass any edge any times and every time you pass it you will gain the weight.

    Now there are q queries that you need to answer. Each of the queries is about the k-th minimum value of all the paths.

    Input

    The input consists of multiple test cases, starting with an integer t (1≤t≤100), denoting the number of the test cases.
    The first line of each test case contains three positive integers n,m,q. (1≤n,m,q≤5∗104)

    Each of the next m lines contains three integers ui,vi,wi, indicating that the i−th edge is from ui to vi and weighted wi.(1≤ui,vi≤n,1≤wi≤109)

    Each of the next q lines contains one integer k as mentioned above.(1≤k≤5∗104)

    It's guaranteed that Σn ,Σm, Σq,Σmax(k)≤2.5∗105 and max(k) won't exceed the number of paths in the graph.

    Output

    For each query, print one integer indicates the answer in line.

    Sample Input

    
     

    1 2 2 2 1 2 1 2 1 2 3 4

    Sample Output

    
     

    3 3

    Hint

    1->2 value :1 2->1 value: 2 1-> 2-> 1 value: 3 2-> 1-> 2 value: 3

    本题很容易想到使用优先队列没次取出队首元素

    然后拓展临边

    但是由于需要拓展k次,正常的拓展一定是超内存的

    由于我们只需要知道前k小

    所以我们要只需要维持该优先队列的长度是k即可

    所以我们选用multiset并且每次擦除最大的元素

    值得一提的优化就是先将临边排序,然后在无法入队时break

    //#pragma GCC optimize(3,"Ofast","inline")
    #include<bits/stdc++.h>
    using namespace std;
    int MAXK;
    int n,m;
    int flag=0;
    long long ans[50005];
    int qu[50005];
    int inqu[50005];
    int cnt=0;
    vector<pair<long long,int> > G[50005];
    multiset<pair<long long, int> > qe;
    main()
    {
        int t;
        scanf("%d",&t);
        while(t--)
        {
            qe.clear();
            cnt=0;
            int q;
            scanf("%d%d%d",&n,&m,&q);
            for(int i=1; i<=m; i++)
            {
                int tmp1,tmp2;
                long long tmp3;
                scanf("%d%d%lld",&tmp1,&tmp2,&tmp3);
                G[tmp1].push_back(make_pair(tmp3,tmp2));
                qe.insert(make_pair(tmp3,tmp2));
            }
            for(int i=1;i<=n;i++)
            {
                sort(G[i].begin(),G[i].end());
            }
            MAXK=-1;
            for(int i=1; i<=q; i++) scanf("%d",&qu[i]),MAXK=max(MAXK,qu[i]);
            multiset<pair<long long, int> >::iterator it1,it2;
            while(qe.size())
            {
                it1=qe.begin();
                int tmp2;
                long long tmp1;
                tmp1=it1->first;
                tmp2=it1->second;
                qe.erase(it1);
                ans[++cnt]=tmp1;
                for(int i=0; i<G[tmp2].size(); i++)
                {
                    int v=G[tmp2][i].second;
                    long long c=G[tmp2][i].first;
                    if(qe.size()<MAXK-cnt+1) qe.insert(make_pair(c+tmp1,v));
                    else
                    {
                        it2=qe.end();
                        it2--;
                        if(c+tmp1<it2->first)
                        {
                            qe.erase(it2);
                            qe.insert(make_pair(c+tmp1,v));
                        }
                        else
                        {
                            break;
                        }
                    }
                }
                if(cnt>=MAXK) break;
            }
            //for(int i=1;i<=cnt;i++) cout<<ans[i]<<endl;
            for(int i=1; i<=q; i++)
            {
                printf("%lld
    ",ans[qu[i]]);
            }
            for(int i=1; i<=n; i++) G[i].clear();
        }
    }
  • 相关阅读:
    SQL注入绕过——主要是magic_quotes_gpc, is_int(只能跑路,无注入点),以及关键字绕过,WAF绕过
    小葵多功能转换工具——编解码绕过,TODO
    load_file() 常用敏感信息
    crontab 结合 thinkphp3.2
    Docker 小型电脑
    Linux 查找大目录
    phpmyadmin 连接远程数据库
    git 变更 地址
    showdoc可以导出
    showdoc搭建
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852218.html
Copyright © 2011-2022 走看看