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();
        }
    }
  • 相关阅读:
    数据库课程设计心得【3】存储过程的运用
    看BBC研究大脑的科教片中“放松产生灵感”的笔记
    成功干掉“恶心的U盘自动运行病毒免疫目录”!共享方法,让更多的人干掉这东西!
    分享一大堆最新dot net asp.net c# 电子书下载 , 英文原版的。经典中的经典
    SQL学习之 对GROUP BY 和 HAVING 的理解 学习笔记
    关于Theme中.skin与css需要理清的关系
    最近的学习笔记,记录一些通俗易懂的学习类文章。更像是好资料参与索引。
    关于DNN Module开发学习以来的一点总结
    工具发布!QQ空间阅读与备份工具
    被忽视的大道理
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852218.html
Copyright © 2011-2022 走看看