• HDU 1535 SPFA 前向星存图优化


    Invitation Cards

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
    Total Submission(s): 2972    Accepted Submission(s): 1385


    Problem 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
     
    Source
     
     
    题意: 一个单向图  求1到给点的最短距离 和各个点到1的最短距离之和
    题解:正向反向 两次建图 前向星存图     前向星 传送门 http://blog.csdn.net/acdreamers/article/details/16902023  
    struct node

    {
    int pr;//同起点的上一条边 
    int to;//单向边的终点
    int w;//边的权值
    } edge1[MAXN],edge2[MAXN];// 存边

    加边操作

    void add1(int a,int b,int c) //a-->b
    {
    nedge1++;//初始为零
    edge1[nedge1].to=b;
    edge1[nedge1].w=c;
    edge1[nedge1].pr=pre1[a];
    pre1[a]=nedge1;//以a为起点的下一条边的位置(位置其实就是边的序号) pre1数组初始为0;

                                //(发现这里是逆向的 一层一层的往上找 指导 pre1数组的值为0 ) 遍历的时候
    }

    SPFA 姿势

     
      1 #include<iostream>
      2 #include<cstring>
      3 #include<cstdio>
      4 #include<queue>
      5 #define MAXN 1000001
      6 #define inf 100000000
      7 using namespace  std;
      8 struct node
      9 {
     10     int pr;
     11     int to;
     12     int w;
     13 }edge1[MAXN],edge2[MAXN];
     14 int pre1[MAXN],pre2[MAXN];
     15 int dis1[MAXN],dis2[MAXN];
     16 int nedge1=0;
     17 int nedge2=0;
     18 int n;
     19 int p,q;
     20 int be,ed,we;
     21 queue<int >qq;
     22 int now;
     23 int vis[MAXN];
     24 void add1(int a,int b,int c)
     25 {
     26     nedge1++;
     27     edge1[nedge1].to=b;
     28     edge1[nedge1].w=c;
     29     edge1[nedge1].pr=pre1[a];
     30     pre1[a]=nedge1;
     31 }
     32 void add2(int a,int b,int c)
     33 {
     34     nedge2++;
     35     edge2[nedge2].to=b;
     36     edge2[nedge2].w=c;
     37     edge2[nedge2].pr=pre2[a];
     38     pre2[a]=nedge2;
     39 }
     40 void spfa1()
     41 {
     42      for(int gg=1;gg<=p;gg++)
     43   {
     44        vis[gg]=0;
     45        dis1[gg]=inf;
     46   }
     47     vis[1]=1;
     48     dis1[1]=0;
     49     qq.push(1);
     50     while(!qq.empty())
     51     {
     52         now=qq.front();
     53         vis[now]=0;
     54         qq.pop();
     55         for(int k=pre1[now];k!=0;k=edge1[k].pr)
     56         {
     57             int mm=edge1[k].to;
     58             if(dis1[now]+edge1[k].w<dis1[mm])
     59             {
     60                 dis1[mm]=dis1[now]+edge1[k].w;
     61                 if(!vis[mm])
     62                 {
     63                     vis[mm]=1;
     64                     qq.push(mm);
     65                 }
     66             }
     67         }
     68     }
     69 }
     70 void spfa2()
     71 {
     72   for(int gg=1;gg<=p;gg++)
     73   {
     74        vis[gg]=0;
     75        dis2[gg]=inf;
     76   }
     77     vis[1]=1;
     78     dis2[1]=0;
     79     qq.push(1);
     80     while(!qq.empty())
     81     {
     82         now=qq.front();
     83         qq.pop();
     84         vis[now]=0;
     85         for(int k=pre2[now];k!=0;k=edge2[k].pr)
     86         {
     87             int mm=edge2[k].to;
     88             if(dis2[now]+edge2[k].w<dis2[mm])
     89             {
     90                 dis2[mm]=dis2[now]+edge2[k].w;
     91                  if(!vis[mm])
     92                 {
     93                     vis[mm]=1;
     94                     qq.push(mm);
     95                 }
     96             }
     97         }
     98     }
     99 }
    100 int main()
    101 {
    102     scanf("%d",&n);
    103     for(int i=1;i<=n;i++)
    104     {
    105         scanf("%d %d",&p,&q);
    106         nedge1=0;nedge2=0;
    107         for(int gg=1;gg<=p;gg++)
    108         {
    109             pre1[gg]=0;
    110             pre2[gg]=0;
    111         }
    112         for(int j=1;j<=q;j++)
    113         {
    114             scanf("%d %d %d",&be,&ed,&we);
    115             add1(be,ed,we);
    116             add2(ed,be,we);
    117         }
    118         spfa1();
    119         spfa2();
    120         int sum=0;
    121         for(int e=1;e<=p;e++)
    122             sum=sum+dis1[e]+dis2[e];
    123         printf("%d
    ",sum);
    124     }
    125     return 0;
    126 }
    View Code
     
  • 相关阅读:
    移动端页面滚动性能调优
    外盘期货交易安全吗
    Vuetify的pagination功能踩坑
    oh-my-zsh自定义主题
    SSM整合
    Spring底层学习 1
    Node.js+Express day01
    JS+DOM conclusions
    tables+form+validation+Semantic HTML
    mybatis-利用association和collection实现一对一和一对多
  • 原文地址:https://www.cnblogs.com/hsd-/p/5244555.html
Copyright © 2020-2023  润新知