Poj1511--invitation Cards (spfa+ adjacency table)

Source: Internet
Author: User

Description

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia is aware of this fact. They want to propagate theater and, the most of all, antique comedies. They has 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 have assigned exactly one bus stop and he or she stays there the whole day and gives invitation to P Eople travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing a nd robbery.

The

The transport system is very special:all lines is unidirectional and connect exactly the 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 and stops are given by special tables and are payable on the spot. The lines is planned in such a to, that's round trip (i.e. a journey starting and finishing at the same stop) passes Through a central Checkpoint Stop (CCS) where each passenger have to pass a thorough check including body scan.

All the ACM student members leave the CCS morning. Each volunteer are to move to a predetermined stop to invite passengers. There is as many volunteers as stops. At the end of the day, all students travel back to CCS. You is to write a computer program this helps ACM to minimize the amount of money to pay every day for the transport of T Heir 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 the 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 is 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 is positive integers the sum of which is smaller than 1000000000. You can also assume it's 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 it S 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

The data is very big, Dijlstra also not, can only use the SPFA and the adjacency table passes, is seeking once again, reverses again to beg again adds up on the line

#include <iostream> #include <cstring> #include <string> #include <vector> #include <queue > #include <cstdio> #include <set> #include <cmath> #include <algorithm> #define INF 0x3f3f3f3
F #define MAXN 1000010 #define MOD 1000000007 using namespace std;
struct Edge {int v,w,next;};
Edge edge1[maxn<<1],edge2[maxn<<1];
int HEAD1[MAXN],HEAD2[MAXN],N,M,E,VIS[MAXN],DIS[MAXN];
    void Add (Edge *edge,int *head,int u,int v,int w) {edge[e].v=v;
    Edge[e].w=w;
    Edge[e].next=head[u];
Head[u]=e;
    } Long Long SPFA (Edge *edge,int *head,int u) {memset (vis,0,sizeof (VIS));
    for (int i=1;i<=n;++i) Dis[i]=inf;
    dis[u]=0;
    Queue<int> Q;
    Q.push (U);
        while (!q.empty ()) {U=q.front ();
        Q.pop ();
        Vis[u]=1;
            for (int i=head[u];i!=-1;i=edge[i].next) {int v=edge[i].v,w=edge[i].w; if (W+dis[u]<dis[v]) {dis[V]=w+dis[u];
                    if (!vis[v]) {vis[v]=1;
                Q.push (v);
    }}}} long long ans=0;
    for (int i=2;i<=n;++i) ans+=dis[i];
return ans;
    } int main () {int t;
    scanf ("%d", &t);
        while (t--) {scanf ("%d%d", &n,&m);
        e=0;
        int u,v,w;
        memset (head1,-1,sizeof (HEAD1));
        memset (head2,-1,sizeof (head2));
            while (m--) {scanf ("%d%d%d", &u,&v,&w);
            Add (EDGE1,HEAD1,U,V,W);
            Add (EDGE2,HEAD2,V,U,W);
        e++;
        } Long long ans;
        ANS=SPFA (edge1,head1,1) +SPFA (edge2,head2,1);
    printf ("%i64d\n", ans);
} return 0;
 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.