zoukankan      html  css  js  c++  java
  • SPFA算法(2) POJ 1511 Invitation Cards

    原题:

    Invitation Cards
    Time Limit: 8000MS   Memory Limit: 262144K
    Total Submissions: 31230   Accepted: 10366

    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到其他点就是最典型的单源点最短路径问题,又因为最多会有1000000个点和1000000条边,因此采用SPFA无疑是最好的选择。然后考虑从其他点回到1点,这里只要将图中所有的边反向,然后再求一遍点1到其他点的费用和,最后相加两次的费用和即可。

    注意点:因为点很多,所以不用邻接矩阵储存图,而用邻接表储存图。

    AC代码:

    #include<stdio.h>
    #include<stdlib.h>
    #include<stdbool.h>
    #include<string.h>
    int q,n,m;
    int u1[1000001],v1[1000001],w1[1000001],first1[1000001],next1[1000001];
    int u2[1000001],v2[1000001],first2[1000001],next2[1000001];
    int dis[1000001],t[100001];
    bool e[1000001];    
    int temp;
    int tmp;
    int head,tail;
    long long ans1,ans2;
    int main(){
        int i,j;
    
        scanf("%d",&q);
        for(j=1;j<=q;j++){
            scanf("%d%d",&n,&m);
        
            for(i=1;i<=n;i++){              //图的邻接表储存 
                first1[i]=-1;
                first2[i]=-1;
            }
            for(i=1;i<=m;i++){                           //分别储存两种,一个是反向后的图 
                scanf("%d%d%d",&u1[i],&v1[i],&w1[i]);
                next1[i]=first1[u1[i]];
                first1[u1[i]]=i;
            
                v2[i]=u1[i];
                u2[i]=v1[i];
                
                next2[i]=first2[u2[i]];
                first2[u2[i]]=i;
            }
        
            for(i=1;i<=n;i++)                       //SPFA初始化 
             dis[i]=0x7fffffff;
            dis[1]=0;
            memset(e,false,sizeof(e));
            e[1]=true;
            t[1]=1;
            head=0;
            tail=1;
            
            while(head!=tail){                                
                head=(head+1)%10000;                         //头指针下移 
                temp=t[head];                                //temp为队首元素,是一个点 
                e[temp]=false;                               //队列首个元素出队 
                tmp=first1[temp];                            //tmp表示以temp为起点的并且在邻接表中的第一条边 
                while(tmp!=-1){                              //枚举与temp相连的点 
                    if(dis[v1[tmp]]>dis[temp]+w1[tmp]){      
                        dis[v1[tmp]]=dis[temp]+w1[tmp];       //修改 
                        if(!e[v1[tmp]]){
                            e[v1[tmp]]=true;                  //新元素入列 
                            tail=(tail+1)%10000;              //队尾指针下移 
                            t[tail]=v1[tmp];                 
                        }
                    } 
                tmp=next1[tmp];                               //找其他以temp为起点的边,在邻接表中找 
                }
                
            }
            ans1=0;                                           //求和 
            for(i=1;i<=n;i++)
            ans1+=dis[i];
            
            //下同 
            
            for(i=1;i<=n;i++)
             dis[i]=0x7fffffff;
            for(i=1;i<=10001;i++)                             //队列清空 
             t[i]=0;
            dis[1]=0;
            memset(e,false,sizeof(e));
            e[1]=true;
            t[1]=1;
            head=0;
            tail=1;
            
            while(head!=tail){
                head=(head+1)%10000;
                temp=t[head];
                e[temp]=false;
                tmp=first2[temp];
                while(tmp!=-1){
                    if(dis[v2[tmp]]>dis[temp]+w1[tmp]){
                        dis[v2[tmp]]=dis[temp]+w1[tmp];
                        if(!e[v2[tmp]]){
                            tail=(tail+1)%10000;
                            e[v2[tmp]]=true;
                            t[tail]=v2[tmp];
                        }
                    }
                tmp=next2[tmp];
                }
            }
            ans2=0;
            for(i=1;i<=n;i++)
            ans2+=dis[i];
            printf("%lld
    ",ans1+ans2);                           //输出答案 
        }
        return 0;
    } 
     
     
     
     
     
     
  • 相关阅读:
    PHP include寻找文件规则
    go实现聊天系统(三)
    go实现聊天系统(二)
    题解 UVA10298 【Power Strings】
    单源最短路SPFA
    css面试题
    【笔记】随笔记录
    【form】表单提交方式
    【CSS】常用css
    【Appcan】常用随笔
  • 原文地址:https://www.cnblogs.com/uncklesam7/p/8908326.html
Copyright © 2011-2022 走看看