• POJ1511(最短路大数据处理)


    Invitation Cards
    Time Limit: 8000MS   Memory Limit: 262144K
    Total Submissions: 23357   Accepted: 7675

    Description

    In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery. 

    The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan. 

    All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees. 

    Input

    The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.

    Output

    For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

    Sample Input

    2
    2 2
    1 2 13
    2 1 33
    4 6
    1 2 10
    2 1 60
    1 3 20
    3 4 10
    2 4 5
    4 1 50

    Sample Output

    46
    210
    题意:求1号结点到其余各结点路径之和与其余各结点到1号结点之和的和。
    思路:求其余各结点到1号结点的路径时可将有向边反向,转化为求1号结点到其余各结点的路径之和。注意:该题目的数据量较大,用动态邻接表存储会RE。
    对比了一下 dijkstra 与 spfa算法。
    /*
        dijkstra    1511    Accepted    39744K    1907MS    G++
    */
    #include"cstdio"
    #include"queue"
    #include"vector"
    using namespace std;
    const int MAXN=1000005;
    const int INF=0x3fffffff;
    typedef long long LL;
    typedef pair<int,int> P;
    struct Edge{
        int to,cost,next;
    }es[2][MAXN];
    int V,E;
    int head[2][MAXN];
    LL d[MAXN];
    void add_edge(int u,int v,int cost,int type)
    {
        es[type][E].to=v;
        es[type][E].cost=cost;
        es[type][E].next=head[type][u];
        head[type][u]=E;
    }
    
    LL dijkstra(int s,int type)
    {
        for(int i=1;i<=V;i++)    d[i]=INF;
        
        priority_queue<P,vector<P>,greater<P> > que;
        d[s]=0,que.push(P(0,s));
        
        while(!que.empty())
        {
            P p=que.top();que.pop();
            int v=p.second;
            if(d[v]<p.first)    continue;
            for(int i=head[type][v];i!=-1;i=es[type][i].next)
            {
                Edge e=es[type][i];
                if(d[e.to]>d[v]+e.cost)
                {
                    d[e.to]=d[v]+e.cost;
                    que.push(P(d[e.to],e.to));
                }
            }
        }
        LL ans=0;
        for(int i=1;i<=V;i++)
            ans+=d[i];
        return ans;
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        for(int cas=1;cas<=T;cas++)
        {        
            int P,Q;
            scanf("%d%d",&P,&Q);
            V=P,E=0;
            for(int i=1;i<=V;i++)    head[0][i]=head[1][i]=-1;
            for(int i=0;i<Q;i++)
            {
                int u,v,co;
                scanf("%d%d%d",&u,&v,&co);
                add_edge(u,v,co,0);
                add_edge(v,u,co,1);
                E++;    
            }
            
            LL res=0;
            res+=dijkstra(1,0);
            res+=dijkstra(1,1);
            printf("%I64d
    ",res);
        
        }
        return 0;
    }
    /*
        spfa    1511    Accepted    43676K    1875MS    G++
    */
    #include"cstdio"
    #include"queue"
    using namespace std;
    const int MAXN=1000005;
    const int INF=0x3fffffff;
    typedef long long LL;
    struct Edge{
        int to,cost,next;
    }es[2][MAXN];
    int head[2][MAXN];
    int V,E;
    LL d[MAXN];
    int vis[MAXN];
    LL spfa(int s,int type)
    {
        for(int i=1;i<=V;i++)
        {
            d[i]=INF;
            vis[i]=0;
        }
        queue<int> que;
        vis[s]=1,d[s]=0,que.push(s);
        
        while(!que.empty())
        {
            int v=que.front();que.pop();
            vis[v]=0;
            for(int i=head[type][v];i!=-1;i=es[type][i].next)
            {
                Edge e=es[type][i];
                if(d[e.to]>d[v]+e.cost)
                {
                    d[e.to]=d[v]+e.cost;
                    if(!vis[e.to])
                    {
                        que.push(e.to);
                        vis[e.to]=1;
                    }
                }
            }
        }
        LL ans=0;
        for(int i=1;i<=V;i++)
            ans+=d[i];
        return ans;
    }
    int main()
    {
        int T;
        scanf("%d",&T);
        for(int cas=1;cas<=T;cas++)
        {        
            int P,Q;
            scanf("%d%d",&P,&Q);
            V=P,E=0;
            for(int i=1;i<=V;i++)    head[0][i]=head[1][i]=-1;
            for(int i=0;i<Q;i++)
            {
                int u,v,co;
                scanf("%d%d%d",&u,&v,&co);
                es[0][E].to=v,es[0][E].cost=co,es[0][E].next=head[0][u],head[0][u]=E;
                es[1][E].to=u,es[1][E].cost=co,es[1][E].next=head[1][v],head[1][v]=E;
                E++;        
            }
            
            LL res=0;
            res+=spfa(1,0);
            res+=spfa(1,1);
            printf("%I64d
    ",res);
            
        }
        return 0;
    }

    堆优化dijkstra 算法的复杂度为 |E|*log(|V|) ,优势在于处理稀疏图。

  • 相关阅读:
    Coherence的NameService
    监控Coherence成员的加入和离开集群事件
    Coherence的集群成员的离开和加入机制研究
    Coherence对象压缩以及对象大小计算
    Coherence代理的负载均衡
    Oracle Service Bus Socket Adapter调整的参数
    Coherence生产环境异常定位过程
    Automatic Diagnostic Repository
    docker(三)反正我不喜欢敲命令,daocloud.io管理你的docker,安装远程下载工具aria2 迅雷远程下载 xware
    docker(二)不管怎么样,先跑起来。运行一个samba,运行一个hello world。
  • 原文地址:https://www.cnblogs.com/program-ccc/p/5154312.html
Copyright © 2020-2023  润新知