HDU 1535 Invitation Cards "SPFA + chain-forward star"

Source: Internet
Author: User
Invitation Cards Time limit:10000/5000 MS (java/others) Memory limit:65536/65536 K (java/others)
Total submission (s): 2232 Accepted Submission (s): 1082


Problem 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 transport system is very special:all lines be unidirectional and connect exactly. 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 of 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 is paid each day by ACM S 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/*
Ask 1 to all points of distance and + all points to 1 distance and
Map can be 1 to all point distance, reverse mapping can be all points to 1 of the distance
*/
Make SPFA algorithm for the first time, refer to other people's income.
For code, the way to initialize a write-chain forward star and maximum value to 0x3f3f3f3f can be taken.

#include <cstdio> #include <cstring> #include <queue> #define N 1000002 #define INF 0x3f3f3f3f using
namespace Std;
int n,m,cnt1,cnt2,head1[n],head2[n]; 
int dis[n],vis[n]; struct Edge {int to,w,next;}

E1[n],e2[n];
     void Init () {cnt1=cnt2=0;
     memset (head1,-1,sizeof (HEAD1));
memset (head2,-1,sizeof (head2));
    } void Addedge1 (int i,int j,int W) {e1[cnt1].to=j;
    E1[cnt1].w=w;
    E1[cnt1].next=head1[i];
head1[i]=cnt1++;
    } void Addedge2 (int i,int j,int W) {e2[cnt2].to=j;
    E2[cnt2].w=w;
    E2[cnt2].next=head2[i];
head2[i]=cnt2++; 
        } void Spfa1 (int s) {for (int i=1; i<=n; i++) {dis[i]=inf;
    vis[i]=0;
   } queue<int>q; 
    while (!q.empty ()) Q.pop ();
    Dis[s]=0,vis[s]=1;
    Q.push (s);
        while (!q.empty ()) {int X=q.front ();
        Q.pop ();
        vis[x]=0;
            for (int k=head1[x]; k!=-1; k=e1[k].next) {int t=e1[k].to; if (dis[t]>dis[x]+e1[K].W) {DIS[T]=DIS[X]+E1[K].W;
                    if (!vis[t]) {vis[t]=1;
                Q.push (t);
}}}} return;
        } void Spfa2 (int s) {for (int i=1; i<=n; i++) {dis[i]=inf;
    vis[i]=0;
   } queue<int>q;
    while (!q.empty ()) Q.pop ();
    Q.push (s);
    Dis[s]=0,vis[s]=1;
        while (!q.empty ()) {int X=q.front ();
        Q.pop ();
        vis[x]=0;
            for (int k=head2[x]; k!=-1; k=e2[k].next) {int t=e2[k].to;
                if (DIS[T]&GT;DIS[X]+E2[K].W) {DIS[T]=DIS[X]+E2[K].W;
                    if (!vis[t]) {vis[t]=1;
                Q.push (t);
}}}} return;
    } int main () {int t,sum,a,b,c;
    scanf ("%d", &t); 
        while (t--) {init (); scanf ("%d%d", &n,&m);
        while (m--) {scanf ("%d%d%d", &a,&b,&c);
            Addedge1 (A,B,C);
        Addedge2 (B,A,C);
        } spfa1 (1);
        sum=0;
        for (int i=2; i<=n; i++) {sum+=dis[i];
        } SPFA2 (1);
        for (int i=2; i<=n; i++) {sum+=dis[i];
    } printf ("%d\n", sum);
} 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.