zoukankan      html  css  js  c++  java
  • POJ1511 Invitation Cards(链式前向星+SPFA)

    Time Limit: 8000MS   Memory Limit: 262144K
    Total Submissions: 41399   Accepted: 13298

    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


    思路:反向建边+SPFA
    这题RE了两次...后来发现是数组开太小了,结果开始WA了,最后发现是权值数组开太小了..(话说为什么这里不报RE了)

    AC代码:
     1 #include <iostream>
     2 #include <queue>
     3 #include <cstring>
     4 #include <string>
     5 #include <cstdio>
     6 using namespace std;
     7 #define MAXN 1000005
     8 #define inf 0x3f3f3f3f 
     9 typedef long long ll;
    10 const ll INF = 0x3f3f3f3f3f3f3f3f;
    11 
    12 int n, m, u, v;
    13 ll wei;
    14 ll dis[MAXN], dis2[MAXN], w[2 * MAXN];        //注意数组要开两倍,因为两个点可能有正反两条边 
    15 int h[MAXN], rh[MAXN], e[2 * MAXN], ne[2 * MAXN], idx;
    16 bool vis[MAXN];
    17 inline int read() {
    18     int s = 0, w = 1;
    19     char ch = getchar();
    20     while (ch<'0' || ch>'9') { if (ch == '-')w = -1; ch = getchar(); }
    21     while (ch >= '0'&&ch <= '9') s = s * 10 + ch - '0', ch = getchar();
    22     return s * w;
    23 }
    24 
    25 void add(int *h, int u, int v, ll wei){
    26 
    27     e[idx] = v; w[idx] = wei; ne[idx] = h[u]; h[u] = idx ++;
    28     
    29 }
    30 
    31 ll spfa(int *h, ll *dis){
    32     ll ans = 0;
    33     int start = 1;
    34     memset(vis, 0, sizeof vis);
    35     for(int i = 1; i <= n; ++ i){
    36         dis[i] = INF;
    37     }
    38     
    39     queue<int> q;
    40     int cur;
    41     q.push(start);
    42     dis[start] = 0;
    43     while(!q.empty()){
    44         cur = q.front();
    45         q.pop();
    46         
    47         
    48         vis[cur] = false;
    49         for(int i = h[cur]; i != inf; i = ne[i]){
    50             int j = e[i];    //e[i]中的值是to,也就是这条边的终点 
    51             
    52             if(dis[j] > dis[cur] + w[i]){
    53                 dis[j] = dis[cur] + w[i];
    54                 if(!vis[j]){
    55                     q.push(j);
    56                     vis[j] = true;
    57                 }
    58             }
    59             
    60             
    61         }
    62         
    63         
    64     }
    65     for(int i = 1; i <= n; ++ i)
    66         ans += dis[i];
    67     
    68     
    69     return ans;
    70 }
    71 int main(){
    72     int T;
    73     ll ans;
    74     T = read();
    75     while(T --){
    76         ans = 0;
    77         idx = 0;
    78         memset(h, 0x3f, sizeof h);
    79         memset(rh, 0x3f, sizeof rh);
    80         n = read();
    81         m = read();
    82         
    83         for(int i = 0; i < m; ++ i){
    84             u = read();
    85             v = read();
    86             wei = read();
    87             add(h, u, v, wei);
    88             add(rh, v, u, wei);
    89         }
    90         
    91         ans = spfa(h, dis);
    92         ans += spfa(rh, dis2);
    93         printf("%lld\n", ans);
    94     }
    95     
    96     
    97     return 0;
    98 }
    ac code
    
    
    
     
     
  • 相关阅读:
    HDU 2188 悼念512汶川大地震遇难同胞——选拔志愿者
    博弈论小结
    HDU 2149 Public Sale
    有上下界限制的网络流-总结
    loj #117. 有源汇有上下界最小流
    jquery中not的用法[.not(selector)]
    Assert随笔
    Maps.newHashMapWithExpectedSize(2)
    java1.8操作日期
    控制input只输入数字--- onkeyup="value=value.replace(/[^d]/g,'')"
  • 原文地址:https://www.cnblogs.com/daremo/p/14111797.html
Copyright © 2011-2022 走看看