zoukankan      html  css  js  c++  java
  • nvitation Cards POJ

     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

    往返最短路实质就是邻接表倒置 再跑一遍单源最短路

    此题的数据范围十分之大

    另外需要注意的是 即使题目保证了数据范围 但还是要用long long

    附上代码

    #include<cstdio>
    #include<algorithm>
    #include<queue>
    #include<string>
    #include<iostream>
    #include<list>
    #include<stack>
    #include<deque>
    #include<cstring>
    #include<cmath>
    #include<map>
    using namespace std;
    int n,m;
    struct node
    {
        int to;
        int w;
        bool operator > (const node tmpnode)const
        {
            return w>tmpnode.w;
        }
    }a1[1000005],a2[1000005];
    priority_queue<node ,vector<node >, greater<node > >q;
    int dis1[1000005],dis2[1000005];
    int first1[1000005],first2[1000005];
    int nex1[1000005],nex2[1000005];
    int vis[1000005];
    void dijk1()
    {
        memset(vis,0,sizeof(vis));
        node tmp;
        int temp;
        tmp.to=1,tmp.w=0;
        q.push(tmp);
        dis1[1]=0;
        while(q.size())
        {
            temp=q.top().to;
            q.pop();
            if(vis[temp]==1) continue;
            vis[temp]=1;
            for(int i=first1[temp];i!=-1;i=nex1[i])
            {
                if(dis1[temp]+a1[i].w<dis1[a1[i].to])
                {
                    dis1[a1[i].to]=dis1[temp]+a1[i].w;
                    tmp.w=dis1[a1[i].to];
                    tmp.to=a1[i].to;
                    q.push(tmp);
                }
            }
        }
    }
    void dijk2()
    {
        memset(vis,0,sizeof(vis));
        node tmp;
        int temp;
        tmp.to=1,tmp.w=0;
        q.push(tmp);
        dis2[1]=0;
        while(q.size())
        {
            temp=q.top().to;
            q.pop();
            if(vis[temp]==1) continue;
            vis[temp]=1;
            for(int i=first2[temp];i!=-1;i=nex2[i])
            {
                if(dis2[temp]+a2[i].w<dis2[a2[i].to])
                {
                    dis2[a2[i].to]=dis2[temp]+a2[i].w;
                    tmp.w=dis2[a2[i].to];
                    tmp.to=a2[i].to;
                    q.push(tmp);
                }
            }
        }
    }
    int main()
    {
        int t;
        scanf("%d",&t);
        int temp1,temp2,temp3;
        while(t--)
        {
            memset(dis1,0x3f,sizeof(dis1));
            memset(first1,-1,sizeof(first1));
            memset(nex1,-1,sizeof(nex1));
            memset(dis2,0x3f,sizeof(dis2));
            memset(first2,-1,sizeof(first2));
            memset(nex2,-1,sizeof(nex2));
            scanf("%d%d",&n,&m);
            for(int i=1;i<=m;i++)
            {
                scanf("%d%d%d",&temp1,&temp2,&temp3);
                a1[i].to=temp2;
                a1[i].w=temp3;
                nex1[i]=first1[temp1];
                first1[temp1]=i;
    
                a2[i].to=temp1;
                a2[i].w=temp3;
                nex2[i]=first2[temp2];
                first2[temp2]=i;
            }
            dijk1();
            dijk2();
            long long ans=0;
            for(int i=1;i<=n;i++)
                ans+=dis1[i],ans+=dis2[i];
            printf("%lld
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    hadoop 学习(一)ubuntu14.04 hadoop 安装
    Java开发笔记(三十四)字符串的赋值及类型转换
    Java开发笔记(三十三)字符包装类型
    Java开发笔记(三十二)字符型与整型相互转化
    Java开发笔记(三十)大小数BigDecimal
    Java开发笔记(二十九)大整数BigInteger
    Java开发笔记(二十八)布尔包装类型
    Java开发笔记(二十七)数值包装类型
    Java开发笔记(二十六)方法的输出参数
    Java开发笔记(二十五)方法的输入参数
  • 原文地址:https://www.cnblogs.com/caowenbo/p/11852315.html
Copyright © 2011-2022 走看看