zoukankan      html  css  js  c++  java
  • HDU1535——Invitation Cards(最短路径:SPAF算法+dijkstra算法)

    Invitation Cards

    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~P的站点, 有Q条公交车路线,公交车路线只从一个起点站直接到达终点站,是单向的,每条路线有它自己的车费。

        有P个人早上从1出发,他们要到达每一个公交站点, 然后到了晚上再返回点1。 求所有人来回的最小费用之和。

    解题思路:

        求两边最短路,第二遍把边反向。求总和就行了。

        PS:这个题数据比较多和大,使用dijkstra算法和SPFA算法会超时,需要优化。具体细节看备注。

    Code1(dijkstra算法+邻接表):

     1 #include<cstdio>
     2 #include<cstring>
     3 #include<climits>
     4 #include<algorithm>
     5 #define N 1000000
     6 using namespace std;
     7 int a[N+10],b[N+10],c[N+10],n,m,k;
     8 int dis[N+10],vis[N+10],first[N+10],next[N+10];
     9 void dijkstra(int pos,int *a,int *b,int *c)
    10 {
    11     int i,j,min;
    12     for(i=1;i<=n;i++)
    13         dis[i]=INT_MAX;
    14     memset(vis,0,sizeof(vis));
    15     vis[pos]=1;
    16     i=first[pos];
    17     while(i!=-1){
    18         dis[b[i]]=c[i];
    19         i=next[i];
    20     }
    21     dis[pos]=0;
    22     for(i=1;i<n;i++){
    23         min=INT_MAX;
    24         for(j=1;j<=n;j++)
    25             if(!vis[j]&&dis[j]<min){
    26                 min=dis[j];
    27                 pos=j;
    28             }
    29         vis[pos]=1;
    30         j=first[pos];
    31         while(j!=-1){
    32             if(!vis[b[j]]&&dis[pos]+c[j]<dis[b[j]])
    33                 dis[b[j]]=dis[pos]+c[j];
    34             j=next[j];
    35         }
    36     }
    37 }
    38 void jiantu(int *a,int *b,int *c)
    39 {
    40     int i;
    41     memset(first,-1,sizeof(first));
    42     for(i=1;i<=m;i++){
    43         next[i]=first[a[i]];
    44         first[a[i]]=i;
    45     }
    46 }
    47 int main()
    48 {
    49     int T,i,sum;
    50     scanf("%d",&T);
    51     while(T--){
    52         scanf("%d%d",&n,&m);
    53         for(i=1;i<=m;i++)
    54             scanf("%d%d%d",&a[i],&b[i],&c[i]);
    55         jiantu(a,b,c);
    56         dijkstra(1,a,b,c);
    57         sum=0;
    58         for(i=2;i<=n;i++)
    59             sum+=dis[i];
    60         jiantu(b,a,c);
    61         dijkstra(1,b,a,c);
    62         for(i=2;i<=n;i++)
    63             sum+=dis[i];
    64         printf("%d
    ",sum);
    65     }
    66     return 0;
    67 }

    Code2(SPFA+优化):

     1 #include<stdio.h>
     2 #include<limits.h>
     3 #include<iostream>
     4 #include<string>
     5 #include<queue>
     6 #define MAXN 1000000
     7 using namespace std;
     8 struct e
     9 {
    10     int begin;
    11     int end;
    12     int dis;
    13 } edge1[MAXN+10],edge2[MAXN+10];
    14 int dis[MAXN+10],first[MAXN+10];
    15 bool vis[MAXN+10];
    16 int T,S,D,N,k,M;
    17 void SPFA(int begin,struct e edge[])
    18 {
    19     for (int i=1; i<=N; i++)
    20     {
    21         dis[i]=INT_MAX;
    22         vis[i]=0;
    23     }
    24     queue <int> Q;
    25     Q.push(begin);
    26     dis[begin]=0;
    27     while (!Q.empty())
    28     {
    29         begin=Q.front();
    30         Q.pop();
    31         vis[begin]=0;
    32         for (int i=first[begin]; edge[i].begin==begin; i++) //可以只遍历begin==edge[i].begin的edge
    33             if (dis[edge[i].end]>dis[begin]+edge[i].dis)
    34             {
    35                 dis[edge[i].end]=dis[begin]+edge[i].dis;
    36                 if (!vis[edge[i].end])
    37                 {
    38                     Q.push(edge[i].end);
    39                     vis[edge[i].end]=1;
    40                 }
    41             }
    42     }
    43 }
    44 void init(struct e edge[]) //first存各个顶点作为结点时的第一个下标
    45 {
    46     memset(first,0,sizeof(first));
    47     first[edge[1].begin]=1;
    48     for (int i=2;i<=M;i++)
    49         if (edge[i-1].begin!=edge[i].begin) first[edge[i].begin]=i;
    50 }
    51 bool cmp(struct e a,struct e b)
    52 {
    53     return a.begin<b.begin;
    54 }
    55 int main()
    56 {
    57     int T;
    58     cin>>T;
    59     while (T--)
    60     {
    61         scanf("%d %d",&N,&M);
    62         int x1,x2,x3;
    63         for (int i=1; i<=M; i++)
    64         {
    65             scanf("%d %d %d",&x1,&x2,&x3); //cin跑了2600ms scanf只要1300ms
    66             //cin>>x1>>x2>>x3;
    67             edge1[i].begin=x1,edge1[i].end=x2,edge1[i].dis=x3;
    68             edge2[i].begin=x2,edge2[i].end=x1,edge2[i].dis=x3;
    69         }
    70         sort(edge1+1,edge1+M+1,cmp); //按begin顶点排序
    71         sort(edge2+1,edge2+M+1,cmp);
    72         init(edge1);
    73         SPFA(1,edge1);
    74         int cnt=0;
    75         for (int i=1; i<=N; i++)
    76             cnt+=dis[i];
    77         init(edge2);
    78         SPFA(1,edge2);
    79         for (int i=1; i<=N; i++)
    80             cnt+=dis[i];
    81         printf("%d
    ",cnt);
    82     }
    83     return 0;
    84 }
  • 相关阅读:
    p1373【奶牛的卧室】
    p1248【交错匹配】(DP)
    QBXT模拟赛T3
    NOIP冲刺班的考试总结
    欧拉回路的一些东西
    一道dp题目
    Blocks
    玩具取名
    Y的积木
    游荡的奶牛
  • 原文地址:https://www.cnblogs.com/Enumz/p/3860916.html
Copyright © 2011-2022 走看看